Powershell远程文件复制无效

时间:2017-05-09 14:29:53

标签: powershell

我正在尝试从一个源服务器远程复制几个IIS服务器上的文件。

sourcepath是UNC路径,如\\server\c$\path\destinationpath是本地文件夹c:\data\destination\

奇怪的是,当我在本地服务器上运行它时,这将完美地运行。

 $cmd = $sqlConnection.CreateCommand()
 $cmd.CommandText ="SELECT * from infrastructure"
 $Serverinfo = $cmd.ExecuteReader()
 try
 {
     while ($Serverinfo.Read())
     {
       $servername = $Serverinfo.GetValue(1)
       $location = $Serverinfo.GetValue(2)
       #Invoke-Command -ComputerName $servername  { new-item -Path $Using:destinationpath -name $Using:versionnumber -Itemtype directory }
       Invoke-Command -ComputerName $servername  { Copy-Item -Path $Using:sourcepath -destination $Using:destinationpath }
       #Invoke-Command -ComputerName $servername {Import-Module WebAdministration ; New-WebApplication -force  -Site "Default Web Site" -Name $Using:versionnumber -PhysicalPath $Using:destinationpath$Using:versionnumber }
     }
 }

1 个答案:

答案 0 :(得分:0)

如果没有catch块,并且其他人说的源和目标路径已定义,那么您发布的内容是不完整的。

但是,即使你提到以上三种限制,我在这里也可以看到这是一个可能的原因。

您将面临凭据委派问题。您正在使用PSRP远程连接到一台服务器并将一个文件复制到该计算机,并且您从UNC路径获取源文件,这需要进行一些身份验证。

我可以给你两个更好的选择,当然第一次可能是正确的解决方案。

如果您使用的是PS 5.o或更高版本,则可以使用-ToSession cmdlet的Copy-Item参数。

$Session  =    New-PSSession -ComputerName $ServerName
Copy-Item -SourcePath \\\Server\c$\path -Destination c:\data\Destination-ToSession $Session

了解更多信息Get-Help Copy-Item

编辑:

第二个:

Copy-Item -Path <Sourcepath> -DestinationPath \\destinataionserver\c$\folder

通过访问目标系统的共享文件夹,从源到目标。