Logo Linux Bash SSH Ubuntu Git Menu
 

PowerShell Variable error

Tags:

powershell

Hello, can you please troubleshot this code

$cSource = "\\LAP02IT-HAYK\c$\test.rtf"
$filepath = "c:\servers.csv" 
$servers = Import-CSV $filepath 

Foreach ($server in $servers) {
     write-host $server
     $target = "\\$server\C$\Users\Public\Desktop"
     Copy-Item -Path \\LAP02IT-HAYK\c$\test.rtf -Destination $target;

}

I got this output

@{servers =ast1eam-kslozpl}
Copy-Item : The network path was not found
At line:8 char:6
+      Copy-Item -Path \\LAP02IT-HAYK\c$\test.rtf -Destination $target;
+      ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
    + CategoryInfo          : NotSpecified: (:) [Copy-Item], IOException
    + FullyQualifiedErrorId : System.IO.IOException,Microsoft.PowerShell.Command 
   s.CopyItemCommand

1 Answers

The $server variable is an object with a servers property that contains the server name.

Make sure you explicitly reference the property by name when you use it in the path:

$target = "\\$($server.servers)\C$\Users\Public\Desktop"
like image 89
Mathias R. Jessen Avatar answered Apr 04 '26 12:04

Mathias R. Jessen