PowerShell 变量错误

PowerShell 变量错误

您好,您能解决一下这个代码的问题吗?

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

}

我得到了这个输出

@{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

变量$server目的具有servers包含服务器名称的属性。

确保在路径中使用该属性时通过名称明确引用该属性:

$target = "\\$($server.servers)\C$\Users\Public\Desktop"

相关内容