使用Move-Item -Destination将文件移动到新共享

时间:2017-01-17 17:49:42

标签: powershell

我编写了一个脚本,提示用户回答一系列问题。一旦这些问题得到解答,脚本就会(根据提供的答案)确定需要将哪些文件从源路径移动到目标路径。

脚本运行正常,直到我修改它以包含目标路径的用户输入(在此之前我将路径放在脚本中),如您在$endpath变量中所见。

当我运行脚本时,看起来它运行正常,完成,但它没有移动文件。

$earliesttime = "00/00/0000"
$lasttime = "00/00/0000"
$size2 = 0
$user = ""
$sourcepath = $null
$endpath = $null
while ($sourcepath -eq $null) {
    $sourcepath = Read-Host "Enter source file path"
}
Set-Location $sourcepath
while ($endpath -eq $null) {
    $endpath = Read-Host "Enter destination file path"
}
Set-Location $endpath
Write-Host
switch ($what = Read-Host "Do you want to search by owner?") {
    No {
        while ($earliesttime -eq "00/00/0000") {
            Write-Host
            $earliesttime = Read-Host "What is the earliest date of modification?"
        }
        while ($lasttime -eq "00/00/0000") {
            Write-Host
            $lasttime = Read-Host "What is the latest date of modification?"
        }
        Write-Host
        $skip1 = Read-Host "Do you want to search by size?"
        if ($skip1 -match "yes") {
            while ($size2 -eq 0) {
                $size2 = Read-Host "What is the size in KB of the objects you are looking for?"
            }
            if ($size2 -ge 1) {
                Get-ChildItem -Recurse | Where-Object {
                    $_.Length /1KB -ge $size2 -and
                    $_.LastWriteTime -ge $earliesttime -and
                    $_.LastWriteTime -le $lasttime
                } | Move-Item -Destination "$endpath"
            }
        } elseif ($skip1-match "no") {
            Get-ChildItem -Recurse | Where-Object {
                $_.LastWriteTime -ge $earliesttime -and
                $_.LastWriteTime -le $lasttime
            } | Move-Item -Destination "$endpath"
        }
    }
    Yes {
        while ($user -eq "") {
            Write-Host
            $user = Read-Host "What is the name of the owner (i.e. john.smith)?"
        }
        while ($earliesttime -eq "00/00/0000") {
            Write-Host
            $earliesttime = Read-Host "What is the earliest date of modification?"
        }
        while ($lasttime -eq "00/00/0000") {
            Write-Host
            $lasttime = Read-Host "What is the latest date of modification?"
        }
        Write-Host
        $skip1 = Read-Host "Do you want to search by size?"
        if ($skip1 -match "yes") {
            while ($size2 -eq 0) {
                $size2 = Read-Host " What is the size in KB of the objects you are looking for?"
            }
            if ($size2 -ge 1) {
                Get-ChildItem -Recurse | Where-Object {
                    $_.GetAccessControl().Owner -eq $user -and
                    $_.Length /1KB -ge $size2 -and
                    $_.LastWriteTime -ge $earliesttime -and
                    $_.LastWriteTime -le $lasttime
                } | Move-Item -Destination "$endpath"
            }
        } elseif ($skip1 -match "no") {
            Get-ChildItem -Recurse | Where-Object {
                $_.GetAccessControl().Owner -eq $user -and
                $_.LastWriteTime -ge $earliesttime -and
                $_.LastWriteTime -le $lasttime
            } | Move-Item -Destination "$endpath"
        }
    }
}

1 个答案:

答案 0 :(得分:0)

你有一个问题:

while ($sourcepath -eq $null) {
    $sourcepath = Read-Host "Enter source file path"
}
Set-Location $sourcepath
while ($endpath -eq $null) {
    $endpath = Read-Host "Enter destination file path"
}
Set-Location $endpath

因此,您将$ SourcePath设置为当前位置,但在此之后,您将当前位置设置为$ EndPath。

Set-Location相当于cd,因此您基本上将目录最后一分钟更改为要将文件复制到的目录。然后,您尝试将文件从该位置复制到自身。

您希望删除Set-Location $endpath行或将Get-ChildItem更改为Get-ChildItem -LiteralPath $sourcepath -Recurse | ...

相关问题