Powershell:如何使用不同的用户名/密码映射网络驱动器

问题描述 投票:0回答:7

背景: 假设我在本地计算机上使用以下 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 上的用户名/密码与其他两个盒子不同?

configuration powershell networking hard-drive
7个回答
41
投票
$net = new-object -ComObject WScript.Network
$net.MapNetworkDrive("r:", "\\romeobox\files", $false, "domain\user", "password")

应该可以解决问题,

善良,


15
投票

来这里寻找如何使用 PowerShell 映射驱动器?

PowerShell3.0有一个更简单的方法。

New-PSDrive
已更新为
-persist
选项。 例如。

New-PSDrive -Name U -PSProvider FileSystem -Root \\yourserver\your\folder -Credential yourdomain\username -Persist

过去,

New-PSDrive
仅影响当前的PowerShell会话。
-persist
导致映射被注册到操作系统,就像以前一样。 请参阅新 PSDrive

要回答原始问题,您可以更改所使用的凭据。 使用

-Credential
更改域\用户名会导致 Windows 提示输入密码。 另一种替代方法是传递 PSCredential 对象,如下例所示。 有关更多详细信息,请参阅获取凭证

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  

14
投票

如果您需要一种方法来存储密码而不将其以纯文本形式放入脚本或数据文件中,则可以使用 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

2
投票

我发现这个简单的衬里适合我的情况(有点“开箱即用”的想法;))

(Start-Process -FilePath "C:\windows\system32\NET.exe" -ArgumentList "USE I: \Server\Volume /USER:XXXXX password /persistent:yes" -Wait -Passthru).ExitCode

作为额外的好处,您可以获得一个很好的退出代码来报告。希望有帮助。


1
投票
$User = "user"
$PWord = ConvertTo-SecureString -String "password" -AsPlainText -Force
$Credential = New-Object -TypeName System.Management.Automation.PSCredential -ArgumentList $User, $PWord
$net = $(New-Object -ComObject WScript.Network)
$net.MapNetworkDrive("r:", "\\server\share")

1
投票

如果您设置使用 powershell 本机(如果有这样的东西),那么您可以使用:

 New-SMBMapping -LocalPath "V:" -RemotePath $NetworkPath -UserName $UserAndDomain -Password $pass 

请参阅:https://learn.microsoft.com/en-us/powershell/module/smbshare/new-smbmapping?view=win10-ps

还有一些额外的块,例如Remove-SMBMapping等。


0
投票

域为“MicrosoftAccount”,因此用户值为“MicrosoftAccount\[电子邮件受保护]”。

© www.soinside.com 2019 - 2024. All rights reserved.