dre*_*mac 18 powershell configuration networking hard-drive
背景:假设我使用本地计算机中的以下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");
Run Code Online (Sandbox Code Playgroud)
问题:如何修改脚本以便我也可以连接到romeobox,即使我在romeobox上的用户名/密码与其他两个框的用户名/密码不同?
Dan*_*ott 34
$net = new-object -ComObject WScript.Network
$net.MapNetworkDrive("r:", "\\romeobox\files", $false, "domain\user", "password")
Run Code Online (Sandbox Code Playgroud)
应该做的伎俩,
善良,
担
Kei*_*ill 12
如果您需要一种方法来存储密码而不将其以纯文本形式存储在脚本或数据文件中,则可以使用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
Run Code Online (Sandbox Code Playgroud)
Don*_*rty 10
来这里寻找如何使用PowerShell映射驱动器?
使用PowerShell3.0有一种更简单的方法. New-PSDrive已更新-persist选项.例如
New-PSDrive -Name U -PSProvider FileSystem -Root \\yourserver\your\folder -Credential yourdomain\username -Persist
Run Code Online (Sandbox Code Playgroud)
过去,New-PSDrive仅影响当前的PowerShell会话. -persist导致映射与O/S一起注册.看到 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
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
88639 次 |
| 最近记录: |