问题描述
我正在尝试将文件从本地计算机复制到服务器目标
我的脚本:copy-Item –Path D:\Test.txt –Destination '\\10.10.X.X\c$'
错误:
copy-Item : The network path was not found
At D:\PS_Test_script.ps1:1 char:2
+ copy-Item –Path D:\Test.txt –Destination '\\10.10.X.28X\c$'
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
+ CategoryInfo : NotSpecified: (:) [copy-Item],IOException
+ FullyQualifiedErrorId : System.IO.IOException,Microsoft.PowerShell.Commands.copyitemcommand
服务器有凭据,我猜我必须调用一些东西才能使用凭据。
解决方法
为了简单起见,复制到需要不同凭据才能访问的文件夹共享,您可以使用 New-PSDrive
使用这些凭据映射驱动器
$desiredMappedDrive = 'J'
$desiredMappedDrivePath = '\\10.10.X.X\c$' # Map to administrative C: drive share requires administrator credentials)
$source = 'D:\Test.txt'
$destination = "${desiredMappedDrive}:\temp"
# Get-Credential cmdlet will request that you enter a username and password that has access to the share
New-PSDrive -Name $desiredMappedDrive -PSProvider FileSystem -Root $desiredMappedDrivePath -Credential (Get-Credential)
# after drive is mapped copy file over
Copy-Item -Path $source -Destination $destination -Verbose
,
这是一种使用 PSSession
的完全不同的方法,无需映射任何驱动器:
$targetComputerName = "127.0.0.1"
$Session = New-PSSession $targetComputerName -Credential 'username'
$DestinationPath = "C:\temp"
$source = 'D:\Test.txt'
Copy-Item -Path $source -ToSession $Session -Destination $DestinationPath
$Session | Remove-PSSession
如果您想将其作为脚本执行,您必须创建一个 [SecureString]
并构建一个凭证对象。