如何在 Powershell 中的 Copy-Item cmdlet 的文件路径中包含变量
How to Include a Variable in the File Path of a Copy-Item cmdlet in Powershell
我目前正在尝试将文件从我网络上的一台计算机复制到我网络上所有当前在线的计算机。我的代码是这样的:
Copy-Item -path '\PTFGW-061403573\C$\ProgramData\Microsoft\Windows\Start
Menu\Programs\StartUp\startupScriptV3.ps1' -Destination
'\$onlineComputers\C$\ProgramData\Microsoft\Windows\Start Menu\Programs\StartUp'
目前我遇到了这个错误
Copy-Item : The network path was not found
At line:1 char:1
+ Copy-Item -path '\PTFGW-061403573\C$\ProgramData\Microsoft\Windows\S ...
如果我 运行 每台计算机都通过堆路径,它就可以工作,但我希望能够使用我的变量来使我的代码更加模块化和未来。目前我的变量 ($onlineComputers) 拥有大约 78 台计算机,但这在未来肯定会改变。我怎样才能让它工作,有没有更好的方法来做到这一点?我不是用它当前的设置来调用我的变量中的每个单独的项目吗?如果不是,我将如何去做?
如评论所述,在尝试将文件复制到计算机之前,您需要一个循环来测试计算机是否在线。另外,我想在你的代码中 $onlineComputers
是计算机名称的集合,你不能用它来构建目标路径。
尝试:
$fileToCopy = '\PTFGW-061403573\C$\ProgramData\Microsoft\Windows\Start Menu\Programs\StartUp\startupScriptV3.ps1'
# $sb is the OU location to search in as DistinguishedName like "OU=Computers,DC=DOMAIN,DC=LOCAL"
(Get-ADComputer -SearchBase $sb -Filter *).Name | ForEach-Object {
# put the computername from the $_ automatic variable in a variable of your own, because when
# you hit the catch block, inside there this $_ is the Exception object and no longer the computer name.
$computer = $_
if (Test-Connection -ComputerName $computer -Count 1 -Quiet) {
$destination = "\$computer\C$\ProgramData\Microsoft\Windows\Start Menu\Programs\StartUp"
try {
# -ErrorAction Stop makes sure also non-terminating errors are handeled in the catch block
Copy-Item -Path $fileToCopy -Destination $destination -Force -ErrorAction Stop
}
catch {
Write-Warning "Could not copy to computer '$computer': $($_.Exception.Message)"
}
}
else {
Write-Warning "Computer '$computer' is off-line"
}
}
如果需要,您可以扩展 -Filter
以仅查找名称中包含特定已知字符串的计算机,例如 -Filter "Name -like 'ptfg*-061*'"
。使用 -Filter *
可为您提供 -SearchBase
中指定的 OU 中的所有计算机
我目前正在尝试将文件从我网络上的一台计算机复制到我网络上所有当前在线的计算机。我的代码是这样的:
Copy-Item -path '\PTFGW-061403573\C$\ProgramData\Microsoft\Windows\Start
Menu\Programs\StartUp\startupScriptV3.ps1' -Destination
'\$onlineComputers\C$\ProgramData\Microsoft\Windows\Start Menu\Programs\StartUp'
目前我遇到了这个错误
Copy-Item : The network path was not found
At line:1 char:1
+ Copy-Item -path '\PTFGW-061403573\C$\ProgramData\Microsoft\Windows\S ...
如果我 运行 每台计算机都通过堆路径,它就可以工作,但我希望能够使用我的变量来使我的代码更加模块化和未来。目前我的变量 ($onlineComputers) 拥有大约 78 台计算机,但这在未来肯定会改变。我怎样才能让它工作,有没有更好的方法来做到这一点?我不是用它当前的设置来调用我的变量中的每个单独的项目吗?如果不是,我将如何去做?
如评论所述,在尝试将文件复制到计算机之前,您需要一个循环来测试计算机是否在线。另外,我想在你的代码中 $onlineComputers
是计算机名称的集合,你不能用它来构建目标路径。
尝试:
$fileToCopy = '\PTFGW-061403573\C$\ProgramData\Microsoft\Windows\Start Menu\Programs\StartUp\startupScriptV3.ps1'
# $sb is the OU location to search in as DistinguishedName like "OU=Computers,DC=DOMAIN,DC=LOCAL"
(Get-ADComputer -SearchBase $sb -Filter *).Name | ForEach-Object {
# put the computername from the $_ automatic variable in a variable of your own, because when
# you hit the catch block, inside there this $_ is the Exception object and no longer the computer name.
$computer = $_
if (Test-Connection -ComputerName $computer -Count 1 -Quiet) {
$destination = "\$computer\C$\ProgramData\Microsoft\Windows\Start Menu\Programs\StartUp"
try {
# -ErrorAction Stop makes sure also non-terminating errors are handeled in the catch block
Copy-Item -Path $fileToCopy -Destination $destination -Force -ErrorAction Stop
}
catch {
Write-Warning "Could not copy to computer '$computer': $($_.Exception.Message)"
}
}
else {
Write-Warning "Computer '$computer' is off-line"
}
}
如果需要,您可以扩展 -Filter
以仅查找名称中包含特定已知字符串的计算机,例如 -Filter "Name -like 'ptfg*-061*'"
。使用 -Filter *
可为您提供 -SearchBase