背景:假设我使用本地计算机上的以下powershell脚本自动映射一些网络驱动器。
$net = $(New-Object -ComObject WScript.Network);
$net.MapNetworkDrive("p:", "\\papabox\files");
$net = $(New-Object -ComObject WScript.Network);
$net.MapNetworkDrive("q:", "\\quebecbox\files");
## problem -- this one does not work because my username/password
## is different on romeobox
$net = $(New-Object -ComObject WScript.Network);
$net.MapNetworkDrive("r:", "\\romeobox\files");
问题:我如何修改脚本,使我也可以连接到romeobox,即使我在romeobox上的用户名/密码与其他两个机器的用户名/密码不同?
发布于 2009-10-07 10:30:47
$net = new-object -ComObject WScript.Network
$net.MapNetworkDrive("r:", "\\romeobox\files", $false, "domain\user", "password")
应该能行得通,
仁慈,
丹
发布于 2009-10-07 15:28:43
如果您需要一种方法来存储密码,而无需将其以纯文本形式放入脚本或数据文件中,则可以使用DPAPI来保护密码,以便您可以将其安全地存储在文件中,并在以后以纯文本形式检索它,例如:
# Stick password into DPAPI storage once - accessible only by current user
Add-Type -assembly System.Security
$passwordBytes = [System.Text.Encoding]::Unicode.GetBytes("Open Sesame")
$entropy = [byte[]](1,2,3,4,5)
$encrytpedData = [System.Security.Cryptography.ProtectedData]::Protect( `
$passwordBytes, $entropy, 'CurrentUser')
$encrytpedData | Set-Content -enc byte .\password.bin
# Retrieve and decrypted password
$encrytpedData = Get-Content -enc byte .\password.bin
$unencrytpedData = [System.Security.Cryptography.ProtectedData]::Unprotect( `
$encrytpedData, $entropy, 'CurrentUser')
$password = [System.Text.Encoding]::Unicode.GetString($unencrytpedData)
$password
发布于 2012-11-22 20:54:58
来这里寻找如何使用PowerShell映射驱动器?
PowerShell3.0提供了一种更简单的方法。已使用-persist
选项更新了New-PSDrive
。例如。
New-PSDrive -Name U -PSProvider FileSystem -Root \\yourserver\your\folder -Credential yourdomain\username -Persist
在过去,New-PSDrive
只影响当前的PowerShell会话。可以说,-persist
会导致映射注册到操作系统。请参阅New-PSDrive
要回答原始问题,您可以使用不同的凭据。使用-Credential
改变域\用户名会导致Windows提示输入密码。另一种方法是传递一个PSCredential对象,如下例所示。有关更多详细信息,请参阅Get-Credential。
PS C:\> $User = "mydomain\username"
PS C:\> $PWord = ConvertTo-SecureString -String "mypassword" -AsPlainText -Force
PS C:\> $Credential = New-Object -TypeName System.Management.Automation.PSCredential -ArgumentList $User, $PWord
PS C:\> New-PSDrive -Name U -PSProvider FileSystem -Root \\domain\some\folder -Credential $Credential -Persist
https://stackoverflow.com/questions/1530733
复制相似问题