by i255d at 2013-04-09 16:41:25
I wrote this simple ForEach to distibute modules out to my servers and sometimes it works, but most of the time it doesn’t. There has to be a better way, or I am doing something wrong.by i255d at 2013-04-09 16:49:41$Servers = Get-Content -Path U:\Scripts\UpdateServers.txt
Where {$}
ForEach ($server in $Servers) {
Copy-Item -Path "\serveraaa\c$\scripts\PSWindowsUpdate" -Destination "\$server\C$\Windows\System32\WindowsPowerShell\v1.0\Modules" -Recurse -Force
#Write-Output $server
}
If I un-comment out the Write-Output statement, it list the servers, but I get this error when it doesn’t work. If I play with it for a while, it works again. If I run the Copy-Item command plugging in the name of one of the servers, it works fine too.
Copy-Item : The network path was not found.
At line:4 char:17
+ Copy-Item -Path "\serveraaa\c$\scripts\PSWindowsUpdate" -Dest …
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
+ CategoryInfo : WriteError: (PSWindowsUpdate.psd1:FileInfo) [Copy-Item], IOException
+ FullyQualifiedErrorId : CopyDirectoryInfoItemIOError,Microsoft.PowerShell.Commands.CopyItemCommand
Copy-Item : The network path was not found.
At line:4 char:17
+ Copy-Item -Path "\serveraaa\c$\scripts\PSWindowsUpdate" -Dest …
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
+ CategoryInfo : WriteError: (PSWindowsUpdate.psm1:FileInfo) [Copy-Item], IOException
+ FullyQualifiedErrorId : CopyDirectoryInfoItemIOError,Microsoft.PowerShell.Commands.CopyItemCommand
Copy-Item : The network path was not found.
At line:4 char:17
+ Copy-Item -Path "\serveraaa\c$\scripts\PSWindowsUpdate" -Dest …
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
+ CategoryInfo : WriteError: (Remove-WUOfflineSync.ps1:FileInfo) [Copy-Item], IOException
+ FullyQualifiedErrorId : CopyDirectoryInfoItemIOError,Microsoft.PowerShell.Commands.CopyItemCommand
I tried it this way with little more success:by mjolinor at 2013-04-09 16:56:35Get-Content -Path U:\Scripts\UpdateServers.txt
Where {$}
ForEach-Object { Copy-Item -Path "\serveraaa\c$\scripts\PSWindowsUpdate" -Destination "\$\C$\Windows\System32\WindowsPowerShell\v1.0\Modules" -Recurse -Force
#Write-Output $
}
I notice you’re doing a -Recuse on Copy-Item.by i255d at 2013-04-09 18:53:29
Do all the subfolders exist on the target server?
They do now but they may not on all servers in the future for the first time.by MasterOfTheHat at 2013-04-10 06:56:02
Wait, there are no subfolders, it is just a bunch of files.
Couple of things I noticed:by mjolinor at 2013-04-10 09:25:44
[list][]The way you have it written, you are never passing any objects to the ForEach-Object loop, so I’m assuming you meant to include pipes between those first 3 lines?[/][]The Where {$_} doesn’t actually do anything for you. You are just selecting everything passed to it.[/][]If there are no subfolders below PSWindowsUpdate, then -Recurse is unnecessary.[/][/list]
Have you tried adding -PassThru to the Copy-Item cmdlet? That would at least let you know what was copying and indicate which files you were failing on. Also, you could be getting that path not found error if you are running the script with a user who doesn’t have permissions on the destination folder.
[quote]by MasterOfTheHat at 2013-04-10 09:40:08
•The Where {$} doesn’t actually do anything for you. You are just selecting everything passed to it.
[/quote]
Actually that’s a fairly common method for filtering out blank lines in text file input.
Learn something new every day…by i255d at 2013-04-10 09:48:56
What I am looking for is a more consistent and effective way to accomplish pushing this module out to my servers.by mjolinor at 2013-04-10 09:54:48
The Where {$} eliminates and blank lines in my text file so that only computer names get passed through.
Of course all the information is being piped from one line to the next??? The -Passthrough shows a list of computer names being passed through.
[quote="i255d"]They do now but they may not on all servers in the future for the first time.by i255d at 2013-04-10 10:02:40
Wait, there are no subfolders, it is just a bunch of files.[/quote]
So you don’t really need that -Recurse switch?
I just tried it and if I take out the -recurse, the files don’t copy, I just get the folder.
Also, by adding the -passthrough, I see that it is actual coping the files to the servers in the list, but then it shows the errors afterwards. I wonder why I am getting the errors?