2016-08-14 31 views
0

我想將文件複製到UNC文件名,其中我的用戶有權限,但沒有傳統的驅動器號映射。 PSVersion 5.0.10586.11使用-Credential與複製項失敗

# Get and store the password in an encrypted file. Do this once only. 
# (Get-Credential).Password | ConvertFrom-SecureString | Out-File .\my_secret.txt" 

$user = "me" 
$file = ".\my_secret.txt" 
$cred = New-Object -TypeName System.Management.Automation.PSCredential ` 
-ArgumentList $user, (Get-Content $file | ConvertTo-SecureString) 

Copy-Item -Credential $cred .\list.txt "\\zeplin.nowhere.org\data\docs\log" 

運行腳本似乎表明Copy-Item不支持-Credential。除非必須,否則我寧願不創建與New-PSDrive的新驅動器字母映射。使用Get-Help Copy-Item表明它支持-Credentials參數。將返回以下錯誤消息。

The FileSystem provider supports credentials only on the New-PSDrive cmdlet. Perform the operation again without specifying 
credentials. 
PS C:\Users\me> .\t.ps1 
At C:\Users\me\t.ps1:8 char:1 
+ Copy-Item -Credential $cred .\list.txt "\\zeplin.nowhere.org\data\docs ... 
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ 
    + CategoryInfo   : NotImplemented: (:) [], PSNotSupportedException 
    + FullyQualifiedErrorId : NotSupported 

回答

1

Copy-Item具有支持它的PS提供程序的Credential參數。文件系統提供程序,如所示,不。你將不得不使用New-psdrive。