-2

I am trying to take a screen saver file I've made and copy it to all of our desktops and laptops \system32 folder. I created a computers text file and found this script, but I keep getting this error. Any help would be appreciated.

Running this in Powershell 3.0 on a 2012 Server logged in as an admin.

$computers = gc "\\server\share\scripts\computers.txt"
$source = "\\share\scripts\MySlideshow.scr"
$dest = "C:\Windows\System32"
foreach ($computer in $computers) {
    if (test-Connection -Cn $computer -quiet) {
        Copy-Item $source -Destination \\$computer\$dest -Recurse
    } else {
        "$computer is not online"
    }
}

Error:

Copy-Item : The given path's format is not supported.
At C:\users\tech\desktop\scripts\screen.ps1:6 char:9
+         Copy-Item $source -Destination \\$computer\$dest -Recurse
+         ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
    + CategoryInfo          : NotSpecified: (:) [Copy-Item], NotSupportedException
    + FullyQualifiedErrorId : System.NotSupportedException,Microsoft.PowerShell.Commands.CopyItemCommand
JasonMArcher
  • 14,195
  • 22
  • 56
  • 52
EricCC
  • 1
  • 1
  • shouldn't that be `$source = '\\server01\share\scripts\MySlideshow.scr'`? – TheMadTechnician Jun 18 '14 at 16:37
  • `\\$computer\$dest` won't work, because it isn't a valid pathname. (If the computer is `\\Test`, it would result in `\\Test\C:\Windows\System32`, which is illegal. You can't have an embedded `:` in a UNC pathname.) – Ken White Jun 18 '14 at 16:38

1 Answers1

2

The resulting UNC format for you destination is invalid. You're passing

"\\computer\c:\windows\system32"

when you should be passing

"\\computer\c$\windows\system32"

Try quoting the -destination parameter like this too:

Copy-Item $source -Destination "\\$computer\$dest" -Recurse

You'll also need to use single-quotes when assigning to $dest to prevent powershell from trying to expand the dollar sign as a variable sigil.

$dest = 'c$\windows\system32'

Debug your script by using copy-item -whatif ... to ensure that you're passing the correct parameters.

x0n
  • 51,312
  • 7
  • 89
  • 111