尝试提高我的PowerShell功能。对不起noob脚本。 我正在开发一个循环,它将对服务器列表执行以下操作:
我知道这可能是低效的,可能是错误的循环。运行时,命令全部成功运行但是它不会移动到列表中的其他服务器。我想我做错了什么,有什么想法吗?
$strComputers = get-content c:\temp\serverlist3.txt
$source = "\\server\OS\Temp\WSUS\"
$destination = "\\$strComputer\OS\temp\wsus"
$renamea = "\\$strComputer\OS\TEMP\WSUS\clientwsusInstall-win2008-new.cmd"
$renameb = "\\$strComputer\OS\TEMP\WSUS\clientwsusInstall-win2003-new.cmd"
$destinationc = "\\$strComputer\OS\temp\Wsus-new"
# Run through the Array of Computers
##remove old wsus dir
foreach ($strComputer in $strComputers) {
rm -r $destination
new-item -type directory -path $destination
robocopy $source $destination
rm -r $destinationc
rni -path $renamea clientwsusInstall-win2008.cmd
rni -path $renameb clientwsusInstall-win2003.cmd
}
感谢您输入!!
答案 0 :(得分:3)
问题很简单。
您应该在循环中分配变量,而不是循环。
试试这个:
$strComputers = get-content c:\temp\serverlist3.txt
$source = "\\MGWSUS1\OS\Temp\WSUS\"
# Run through the Array of Computers
##remove old wsus dir
foreach ($strComputer in $strComputers) {
# Only now can the variables be assigned correctly on each iteration.
# ------------------------------------------------------------------
$destination = "\\$strComputer\OS\temp\wsus"
$renamea = "\\$strComputer\OS\TEMP\WSUS\clientwsusInstall-win2008-new.cmd"
$renameb = "\\$strComputer\OS\TEMP\WSUS\clientwsusInstall-win2003-new.cmd"
$destinationc = "\\$strComputer\OS\temp\Wsus-new"
rm -r $destination
new-item -type directory -path $destination
robocopy $source $destination
rm -r $destinationc
rni -path $renamea clientwsusInstall-win2008.cmd
rni -path $renameb clientwsusInstall-win2003.cmd
}
现在拍打自己;)