Powershell 如何将文件从网络路径复制到每个远程PC的本地appdata
因此,我有一个名为Powershell 如何将文件从网络路径复制到每个远程PC的本地appdata,powershell,Powershell,因此,我有一个名为test.CSV的CSV文件,其中包含PC及其各自的用户;该文件如下所示: 我的脚本是这样的 $list = Import-Csv -Path C:\temp\test.csv ForEach ($Computer in $list) { Copy-Item -Path \\domain.local\SYSVOL\domain.local\scripts\copyfile.txt -Destination \\$list.Computer\C$\Users\$list.U
test.CSV的CSV文件,其中包含PC及其各自的用户;该文件如下所示:
我的脚本是这样的
$list = Import-Csv -Path C:\temp\test.csv
ForEach ($Computer in $list) {
Copy-Item -Path \\domain.local\SYSVOL\domain.local\scripts\copyfile.txt -Destination \\$list.Computer\C$\Users\$list.User\AppData\Local\filedepot\
}
如果我在变量中手动输入PC名称和用户名,Copy项
将起作用
但是只要我使用$list
。它不起作用
如果我使用echo
它会向我显示正确的数据,因此它会提取正确的信息
不知道为什么这不起作用,欢迎提供任何帮助使用对象,无需查找列表两次
$list = Import-Csv -Path C:\temp\test.csv
Foreach($l in $list){
$computer = $l.Computer
$user = $l.User
$sourcepath = "\\domain.local\SYSVOL\domain.local\scripts\copyfile.txt"
$destinationpath = "\\$computer\C$\Users\$user\AppData\Local\filedepot"
If(Test-Path -Path $destinationpath){
Copy-Item -Path $sourcepath -Destination $destinationpath -Force
}Else{
Write-Host "Destination path not found - $destinationpath" -ForegroundColor Red
}
}
这是因为您使用导入的列表作为变量。您需要使用$Computer
变量,而不是$list.Computer
。如果您的csv有一个名为“Computer”的列,则必须在foreach循环中像这样迭代它:foreach($list.Computer中的Computer){…
因此,对其进行总结。当您在$computer.List
中遍历每台计算机时,每台计算机的名称将一次分配给$computer
一个,并将执行您的复制项的表达式。因此,对于$List中的每台$computer
,请执行以下操作(这就是它的表示方式)。-Destination\\$($list.Computer)\C$\Users\$($list.User)\AppData\Local\filedepot\
可以工作,使用两次。您应该在路径周围加上“”引号,以防出现空格。效果很好,感谢您的深入了解!
$list = Import-Csv -Path C:\temp\test.csv
Foreach($l in $list){
$computer = $l.Computer
$user = $l.User
$sourcepath = "\\domain.local\SYSVOL\domain.local\scripts\copyfile.txt"
$destinationpath = "\\$computer\C$\Users\$user\AppData\Local\filedepot"
If(Test-Path -Path $destinationpath){
Copy-Item -Path $sourcepath -Destination $destinationpath -Force
}Else{
Write-Host "Destination path not found - $destinationpath" -ForegroundColor Red
}
}