我正在使用powershell v2的CTP。我编写了一个需要访问dmz中的各种网络共享并复制一些文件的脚本。然而,我的问题是明显powershell的cmdlets如copy-item、test-path等不支持备用凭据...
有没有建议如何最好地完成我的任务..?
我正在使用powershell v2的CTP。我编写了一个需要访问dmz中的各种网络共享并复制一些文件的脚本。然而,我的问题是明显powershell的cmdlets如copy-item、test-path等不支持备用凭据...
有没有建议如何最好地完成我的任务..?
Import-Module bitstransfer
$cred = Get-Credential
$sourcePath = \\server\example\file.txt
$destPath = C:\Local\Destination\
Start-BitsTransfer -Source $sourcePath -Destination $destPath -Credential $cred
另一种处理方式是使用标准的"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\
Complete-BitsTransfer
命令来等待其完成,还是它默认是同步的? - jpmc26\\server\C$
这样的位置。请参考 http://serverfault.com/q/512558/172060。 - jpmc26由于PowerShell在许多cmdlet中不支持使用“-Credential”参数(非常令人烦恼),而通过WMI映射网络驱动器被证明在PS中非常不可靠,因此我发现通过net use命令预缓存用户凭据可以很好地解决问题:
# cache credentials for our network path
net use \\server\C$ $password /USER:$username
使用路径中的 \\server\C$ 的所有操作似乎都可以使用 *-item cmdlet 完成。
完成后,您还可以删除共享:
net use \\server\C$ /delete
\\server\C$
样式路径的唯一选择。 - jpmc26Copy-Item
时,它都会提示我输入用户名和密码,这非常令人烦恼。 - Jim AhoPowerShell 3.0 现在支持 FileSystem 提供程序上的凭据。要使用替代凭据,只需在 New-PSDrive cmdlet 上使用 Credential 参数即可。
PS > New-PSDrive -Name J -PSProvider FileSystem -Root \\server001\sharename -Credential mydomain\travisj -Persist
执行此命令后,您现在可以访问新创建的驱动器并执行其他操作,包括像普通驱动器一样复制或移动文件。以下是完整解决方案:
$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"
这是一个老问题,但是我为了未来的查找进行更新。
现在PowerShell v3支持使用-Credential参数进行文件系统操作。
希望这能帮助其他正在寻找同样解决方案的人。
$plaintext_password_file = 'C:\plaintext.txt' # Stores the password in plain text - only used once, then deleted
$encryted_password_file = 'C:\copy_pass.txt' # Stores the password in "safe" encrypted form - used for subsequent runs of the script
# - can only be decrypted by the windows user that wrote it
$file_copy_user = 'OURDOMAIN\A_User'
# Check to see if there is a new plaintext password
if (Test-Path $plaintext_password_file)
{
# Read in plaintext password, convert to a secure-string, convert to an encrypted-string, and write out, for use later
get-content $plaintext_password_file | convertto-securestring -asplaintext -force | convertfrom-securestring | out-file $encryted_password_file
# Now we have encrypted password, remove plain text for safety
Remove-Item $plaintext_password_file
}
# Read in the encrypted password, convert to a secure-string
$pass = get-content $encryted_password_file | convertto-securestring
# create a credential object for the other user, using username and password stored in secure-string
$credentials = new-object -typename System.Management.Automation.PSCredential -argumentlist $file_copy_user,$pass
# Connect to network file location as the other user and map to drive J:
New-PSDrive -Name J -PSProvider FileSystem -Root "\\network\file_directory" -Credential $credentials
# Copy the file to J:
Copy-Item -Force -Verbose -Path "C:\a_file.txt" -Destination "J:\"
$Session = New-PSSession -ComputerName "Server02" -Credential "Contoso\User01"
Copy-Item "D:\Folder002\" -Destination "C:\Folder002_Copy\" -ToSession $Session
net use
命令(正如其他人建议的那样),或者我之前编写的模拟用户身份模块,专门用于解决这个问题。再次将其从死亡中带回来。我通过将.ps1文件包装在批处理文件中并执行Win7,Shift + r.点击RunAs的方式解决了类似凭据问题。如果您愿意,也可以使用PsExec:
psexec.exe /accepteula /h /u user /p pwd cmd /c "echo. | powershell.exe -File script.ps1"
这里有一篇文章,其中有人让它工作了。看起来需要更改注册表。