使用-Credential和Copy-Item失败

时间:2016-08-14 02:59:39

标签: powershell

我正在尝试将文件复制到我的用户有权限的UNC文件名,但没有传统的驱动器号映射。 PSVersion 5.0.10586.11

# 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 个答案:

答案 0 :(得分:1)

Copy-Item具有支持它的PS提供程序的Credential参数。如图所示,FileSystem提供程序没有。你必须使用New-psdrive。