0

Trying to improve my PowerShell abilities. Sorry for the noob script. I am working on a loop that will perform the following against a list of servers:

  • Removes old folder
  • Creates new directory
  • Copy a directory from one host to others on the serverlist
  • Remove old folderB
  • Rename the files in the copied directory

I know this is probably inefficient and may be the wrong loop. When ran, the commands all work successfully BUT it does not move on to the other servers in the list. I imagine I am doing something wrong, any ideas?

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

Thanks for you input!!

4

1 回答 1

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
}

现在打自己一巴掌;)

于 2013-06-13T16:12:10.743 回答