正确的循环格式是针对服务器列表运行的吗?

时间:2013-06-13 15:53:43

标签: powershell

尝试提高我的PowerShell功能。对不起noob脚本。 我正在开发一个循环,它将对服务器列表执行以下操作:

  • 删除旧文件夹
  • 创建新目录
  • 将目录从一个主机复制到服务器列表中的其他主机
  • 删除旧文件夹B
  • 重命名复制目录中的文件

我知道这可能是低效的,可能是错误的循环。运行时,命令全部成功运行但是它不会移动到列表中的其他服务器。我想我做错了什么,有什么想法吗?

$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
}

感谢您输入!!

1 个答案:

答案 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
}

现在拍打自己;)