copy-item With Alternate Credentials

xsp*_*ydr 37 powershell powershell-2.0

我正在使用powershell v2的CTP.我写了一个脚本,需要在我们的dmz中找到各种网络共享并复制一些文件.但是,我遇到的问题显然是powershell的cmdlet,如copy-item,test-path等,不支持备用凭据......

有人建议如何最好地完成我的任务..?

chi*_*s42 41

我最近遇到过这种情况,在Powershell的最新版本中有一个新的BitsTransfer模块,它允许使用BITS进行文件传输,并支持使用-Credential参数.

以下示例显示如何使用BitsTransfer模块使用指定的PSCredential对象将文件从网络共享复制到本地计算机.

Import-Module bitstransfer
$cred = Get-Credential
$sourcePath = \\server\example\file.txt
$destPath = C:\Local\Destination\
Start-BitsTransfer -Source $sourcePath -Destination $destPath -Credential $cred
Run Code Online (Sandbox Code Playgroud)

另一种处理方法是使用标准的"net use"命令.但是,此命令不支持"securestring"密码,因此在获取凭证对象后,您必须获取密码的解密版本以传递给"net use"命令.

$cred = Get-Credential
$networkCred = $cred.GetNetworkCredential()
net use \\server\example\ $networkCred.Password /USER:$networkCred.UserName
Copy-Item \\server\example\file.txt C:\Local\Destination\
Run Code Online (Sandbox Code Playgroud)

  • 另请注意,这不适用于像`\\ server\C $`这样的位置.见http://serverfault.com/q/512558/172060. (3认同)
  • 我不认为 BITS 传输按照您的建议进行。我正在使用 powershell 2,在使用 BITS 之前我仍然需要创建一个带有“net use”的映射,否则它(非常明显)表示 unc 路径不存在。所以在我的脚本中我这样做:net use to add path...bits...net use /delete (3认同)

wes*_*wes 27

由于PowerShell不支持通过许多cmdlet使用"-Credential"(非常烦人),并且通过WMI映射网络驱动器在PS中证明非常不可靠,我发现通过net use命令预先缓存用户凭据工作得很好:

# cache credentials for our network path
net use \\server\C$ $password /USER:$username
Run Code Online (Sandbox Code Playgroud)

在路径中使用\\ server\C $的任何操作似乎都使用*-item cmdlet.

您还可以在完成后删除共享:

net use \\server\C$ /delete
Run Code Online (Sandbox Code Playgroud)


San*_*nda 16

PowerShell 3.0现在支持FileSystem提供程序上的凭据.要使用备用凭据,只需使用New-PSDrive cmdlet上的Credential参数

PS > New-PSDrive -Name J -PSProvider FileSystem -Root \\server001\sharename -Credential mydomain\travisj -Persist
Run Code Online (Sandbox Code Playgroud)

在此命令之后,您现在可以访问新创建的驱动器并执行其他操作,包括复制或移动文件,如普通驱动器.这是完整的解决方案:

$Source = "C:\Downloads\myfile.txt"
$Dest   = "\\10.149.12.162\c$\skumar"
$Username = "administrator"
$Password = ConvertTo-SecureString "Complex_Passw0rd" -AsPlainText -Force
$mycreds = New-Object System.Management.Automation.PSCredential($Username, $Password)

New-PSDrive -Name J -PSProvider FileSystem -Root $Dest -Credential $mycreds -Persist
Copy-Item -Path $Source -Destination "J:\myfile.txt"
Run Code Online (Sandbox Code Playgroud)


x0n*_*x0n 9

这是一个老问题,但我只是为将来的发现者更新它.

PowerShell v3现在支持使用-Credential参数进行文件系统操作.

希望这有助于其他人寻找相同的解决方案.

  • 似乎只有文件系统提供程序的New-PsDrive支持`-credential`参数.在v3 stil上使用`copy-item`不起作用. (9认同)

Sha*_*evy 6

我会尝试将驱动器映射到远程系统(使用'net use'或WshNetwork.MapNetworkDrive,两种方法都支持凭据),然后使用copy-item.


Ric*_*ard -1

显然 powershell 的 cmdlet(例如 copy-item、test-path 等)不支持备用凭据...

看起来就像他们在这里所做的那样,copy-item 当然包含一个 -Credential 参数。

PS C:\> gcm -syn 复制项
复制项目 [-路径] <字符串[]> [[-目标] <字符串>] [-容器] [-强制] [-过滤器 <字符串>] [-I
ninclude <String[]>] [-排除 <String[]>] [-Recurse] [-PassThru] [-Credential <PSCredential>] [...]

  • TechNet (http://technet.microsoft.com/en-us/library/dd315340.aspx) 表示“随 Windows PowerShell 安装的任何提供程序均不支持此参数。” 所以他们懒得实现它,只是期望任何人创建一个新的提供者来做到这一点。:( (3认同)