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

发布于 2024-08-07 03:38:58 字数 576 浏览 9 评论 0原文

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

Background: Assume I use the following powershell script from my local machine to automatically map some network drives.

$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");

Question: How do I modify the script so that I can also connect to romeobox, even though my username/password on romeobox is different from that of the other two boxes?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(7

枫林﹌晚霞¤ 2024-08-14 03:38:58
$net = new-object -ComObject WScript.Network
$net.MapNetworkDrive("r:", "\\romeobox\files", $false, "domain\user", "password")

应该能解决问题,

仁慈,

$net = new-object -ComObject WScript.Network
$net.MapNetworkDrive("r:", "\\romeobox\files", $false, "domain\user", "password")

Should do the trick,

Kindness,

Dan

窝囊感情。 2024-08-14 03:38:58

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

使用PowerShell3.0有一种更简单的方法。 New-PSDrive 已更新为 -persist 选项。例如,

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

在过去,New-PSDrive 仅影响当前的 PowerShell 会话。 -persist 导致映射向操作系统注册,就像以前一样。请参阅 New-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  

Came here looking for how to map drives using PowerShell?

There's a simpler way with PowerShell3.0. New-PSDrive has been updated with the -persist option. E.g.

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

In the past, New-PSDrive affected only the current PowerShell session. -persist causes the mapping to be registered with the O/S, as it were. See New-PSDrive

To answer the original question, you can vary the credentials used. Using -Credential to vary the domain\username causes Windows to prompt for a password. Another alternative is to pass a PSCredential object as in the example below. See Get-Credential for more detail.

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  
一场信仰旅途 2024-08-14 03:38:58

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

If you need a way to store the password without putting it in plain text in your script or a data file, you can use the DPAPI to protect the password so you can store it safely in a file and retrieve it later as plain text e.g.:

# 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
因为看清所以看轻 2024-08-14 03:38:58

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

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

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

I found this easy one liner worked in my case (little "out of the box" thinking ;) )

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

And as an added bonus, you get an nice exitcode to report off of. Hope that helps.

や莫失莫忘 2024-08-14 03:38:58
$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")
$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")
勿挽旧人 2024-08-14 03:38:58

如果您打算使用本机 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 等。

If you are set on using powershell native (if there is such a thing), then you can use:

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

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

There are some additional nuggets like Remove-SMBMapping, etc.

顾冷 2024-08-14 03:38:58

例如,对于使用 PIN 的电子邮件帐户,域为“MicrosoftAccount”,因此用户值为“MicrosoftAccount\

The domain is "MicrosoftAccount" for email accounts using a PIN for example, so the user value is "MicrosoftAccount\[email protected]" in the PowerShell command.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文