Powershell 使用“移动项目-目标”将文件移动到新共享
我编写了一个脚本,提示用户回答一系列问题。回答这些问题后,脚本将确定(基于提供的答案)需要将哪些文件从源路径移动到目标路径 在我修改脚本以包含目标路径的用户输入(在此之前,我将路径放入脚本中)之前,脚本的一切都很好,您将在Powershell 使用“移动项目-目标”将文件移动到新共享,powershell,Powershell,我编写了一个脚本,提示用户回答一系列问题。回答这些问题后,脚本将确定(基于提供的答案)需要将哪些文件从源路径移动到目标路径 在我修改脚本以包含目标路径的用户输入(在此之前,我将路径放入脚本中)之前,脚本的一切都很好,您将在$endpath变量中看到这一点 当我运行脚本时,它看起来运行正常,完成了,但没有移动文件 $earliesttime = "00/00/0000" $lasttime = "00/00/0000" $size2 = 0 $user = "" $sourcepath = $nu
$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"
}
}
}
这里有一个问题:
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
设置位置相当于cd
,因此您基本上是在最后一分钟将目录更改为要将文件复制到的目录。然后尝试将文件从该位置复制到自身
您可能希望删除
Set Location$endpath
行,或者将Get ChildItems更改为Get ChildItem-LiteralPath$sourcepath-Recurse |…
您遇到了一个问题:
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
设置位置相当于cd
,因此您基本上是在最后一分钟将目录更改为要将文件复制到的目录。然后尝试将文件从该位置复制到自身
您可能希望删除
Set Location$endpath
行,或者将Get ChildItems更改为Get ChildItem-LiteralPath$sourcepath-Recurse |…
,非常感谢。事实上,我自己也在想这件事。我真的很感激。你好,肖恩,我该怎么做?非常感谢。事实上,我自己也在想这件事。我真的很感激。你好,肖恩,我该怎么做?