Variables, ForEach, and UNC = Failure

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.

$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
by i255d at 2013-04-09 16:49:41
I tried it this way with little more success:

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

}
by mjolinor at 2013-04-09 16:56:35
I notice you’re doing a -Recuse on Copy-Item.
Do all the subfolders exist on the target server?
by i255d at 2013-04-09 18:53:29
They do now but they may not on all servers in the future for the first time.

Wait, there are no subfolders, it is just a bunch of files.
by MasterOfTheHat at 2013-04-10 06:56:02
Couple of things I noticed:
[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.
by mjolinor at 2013-04-10 09:25:44
[quote]
•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.
by MasterOfTheHat at 2013-04-10 09:40:08
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.
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.
by mjolinor at 2013-04-10 09:54:48
[quote="i255d"]They do now but they may not on all servers in the future for the first time.

Wait, there are no subfolders, it is just a bunch of files.[/quote]

So you don’t really need that -Recurse switch?
by i255d at 2013-04-10 10:02:40
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?