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

Powershell 针对服务器列表运行的循环格式是否正确?,powershell,Powershell,试图提高我的PowerShell能力。对不起,这是noob脚本。 我正在处理一个循环,该循环将针对服务器列表执行以下操作: 删除旧文件夹 创建新目录 将目录从一台主机复制到serverlist上的其他主机 拆下旧折页B 重命名复制目录中的文件 我知道这可能是低效的,可能是错误的循环。运行时,所有命令都能成功运行,但不会移动到列表中的其他服务器。我想我做错了什么,有什么想法吗 $strComputers = get-content c:\temp\serverlist3.txt $source

试图提高我的PowerShell能力。对不起,这是noob脚本。 我正在处理一个循环,该循环将针对服务器列表执行以下操作:

  • 删除旧文件夹
  • 创建新目录
  • 将目录从一台主机复制到serverlist上的其他主机
  • 拆下旧折页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
}
谢谢你的输入

问题很简单

您应该在循环中分配变量,而不是在循环外

试试这个:

$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
}
现在打你自己;)

问题很简单

您应该在循环中分配变量,而不是在循环外

试试这个:

$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
}
现在打你自己;)