Powershell跨网络pc路径复制
我有一些powershell代码来尝试跨网络设备复制映像文件。但是,如果目标部分存在,脚本总是失败。运行脚本的用户具有文件路径的完全权限 但是,Powershell每次都找不到目标文件夹。以下是脚本:Powershell跨网络pc路径复制,powershell,copy,Powershell,Copy,我有一些powershell代码来尝试跨网络设备复制映像文件。但是,如果目标部分存在,脚本总是失败。运行脚本的用户具有文件路径的完全权限 但是,Powershell每次都找不到目标文件夹。以下是脚本: $Servers = (@" PCLIST01 PCLIST02 PCNAME03 PCLIST04 "@ -split "`r`n").Trim() $Source = '\\SERVER\Folder name\subfolder\100 Proje
$Servers = (@"
PCLIST01
PCLIST02
PCNAME03
PCLIST04
"@ -split "`r`n").Trim()
$Source = '\\SERVER\Folder name\subfolder\100 Project Name\moneydreams.jpg'
$Destination = 'C:\ProgramData\Program Folder Name\Subfolder for Program\HTML\' -replace "C:","c$"
$Logfile = "\\SERVER\Folder name\subfolder\100 Project Name\Failed.log"
If (!(Test-Path $Logfile)) { Remove-Item $Logfile}
New-Item $Logfile -ItemType File
ForEach ($Server in $Servers) {
Try {
If (!(Test-Path "$Server\$Destination" )) {
ROBOCOPY "$Source" "\\$Server\$Destination" /LOG:copylog.log
}
Else {
Add-Content $Logfile "Folder does not exist on $Server , \\$Server\$Destination"
}
} Catch {
Add-Content $Logfile "$Server - $($_.Exception.Message)"
}
}
代码在If失败!测试路径$Server\$Destination。
奇怪的是,如果我在$logfile上做一个测试路径,它也会失败,但是文件会用addcontent符号更新!
要注意的是,我确实尝试过使用filepath的back勾号来查看是否存在空格问题,但它并没有解决问题
我不明白为什么它无法找到目标文件夹并进行更新 和都是正确的。您正以完全相反的方式使用测试路径,使用!,最终的目的地路径是错误的
最后,因为您没有添加-ErrorAction停止,所以可能发生的错误可能不会在catch块中结束
尝试:
希望这有帮助
如您所见,我还在各种cmdlet上命名参数,以使代码更易于理解,但也不要盲目依赖参数的位置,因为在该位置很容易出错。为什么要取消对目标文件夹的检查?。。。您还否定了对日志文件的检查,但如果日志文件不存在,则尝试将其删除。您是否查看了在测试路径调用中测试的内容?我看不到您在哪里添加所需的unc前缀。因此,您似乎以PCLIST01\c$\。。。这可能是我有史以来的第一个powershell文件,而不是\\PCLIST01\c$\…因此,我正在寻找我能找到的任何文件。我在下面使用了西奥的信息,并对其进行了轻微的更改,现在可以使用了。谢谢@Theo,这对我来说很有效。我不得不用Copy Item替换ROBOCOPY,因为它在解析源文件名时效果更好。我的下一个改进来源是从文本文件中读取$Servers列表,它将被完成。@Altie我很高兴听到它为您工作。如果你觉得我的答案解决了你的问题,请考虑通过点击来接受答案。✓ 左边的图标。这将帮助其他有类似问题的人更容易找到它。我添加了:$ListofPC='\\place\file.txt',然后我将$servers更改为$servers=Get Content$ListofPC
$Servers = 'PCLIST01','PCLIST02','PCNAME03','PCLIST04'
$Source = '\\SERVER\Folder name\subfolder\100 Project Name\moneydreams.jpg'
# $Destination is a template path. The server name is inserted inside the foreach loop later
$Destination = '\\{0}\c$\ProgramData\Program Folder Name\Subfolder for Program\HTML'
$Logfile = '\\SERVER\Folder name\subfolder\100 Project Name\Failed.log'
$copyLog = '\\SERVER\Folder name\subfolder\100 Project Name\Copylog.log'
# if the log file already exists, remove it
If (Test-Path -Path $Logfile -PathType Leaf) { Remove-Item -Path $Logfile -Force}
foreach ($Server in $Servers) {
# test if the server is not off-line
# in this case i DO want to negate the result.
if (!(Test-Connection -ComputerName $Server -Count 1 -Quiet)) {
Add-Content -Path $Logfile -Value "Server $Server is unreachable"
# skip this server and move on to the next one
continue
}
# insert the server name to the destination
$targetPath = $Destination -f $Server
# $targetPath will be '\\servername\c$\ProgramData\Program Folder Name\Subfolder for Program\HTML'
Try {
# see if you can reach the targetpath on this server
# -ErrorAction Stop ensures the catch block is entered on errors
if (Test-Path -Path $targetPath -PathType Container -ErrorAction Stop) {
ROBOCOPY $Source $targetPath /LOG:$copyLog
}
else {
Add-Content -Path $Logfile -Value "Folder does not exist on $Server , $targetPath"
}
}
catch {
Add-Content -Path $Logfile -Value "$Server - $($_.Exception.Message)"
}
}