Powershell:如何使用不同的用户名/密码映射网络驱动器
背景:假设我在本地计算机上使用以下 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(7)
应该能解决问题,
仁慈,
丹
Should do the trick,
Kindness,
Dan
来这里寻找如何使用 PowerShell 映射驱动器?
使用PowerShell3.0有一种更简单的方法。
New-PSDrive
已更新为-persist
选项。例如,在过去,
New-PSDrive
仅影响当前的 PowerShell 会话。-persist
导致映射向操作系统注册,就像以前一样。请参阅 New-PSDrive要回答原始问题,您可以更改凭据用过的。使用
-Credential
更改域\用户名会导致 Windows 提示输入密码。另一种替代方法是传递 PSCredential 对象,如下例所示。有关更多详细信息,请参阅获取凭据。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.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-PSDriveTo 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.如果您需要一种方法来存储密码而不将其以纯文本形式放入脚本或数据文件中,则可以使用 DPAPI 来保护密码,以便您可以将其安全地存储在文件中并在以后以纯文本形式检索,例如:
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.:
我发现这个简单的衬里在我的情况下工作(有点“开箱即用”的想法;))
(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.
如果您打算使用本机 powershell(如果有这样的事情),那么您可以使用:
请参阅: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:
See: https://learn.microsoft.com/en-us/powershell/module/smbshare/new-smbmapping?view=win10-ps
There are some additional nuggets like Remove-SMBMapping, etc.
例如,对于使用 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.