在 Powershell 中使用用户名/密码连接到网络文件夹 [英] Connecting to a network folder with username/password in Powershell

查看:136
本文介绍了在 Powershell 中使用用户名/密码连接到网络文件夹的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我经常访问 Powershell 中的共享网络文件夹以获取文件等.但是如果共享需要用户名/密码,Powershell 不会提示我输入这些,这与 Windows 资源管理器不同.如果我首先在 Windows 资源管理器中连接到该文件夹​​,Powershell 将允许我进行连接.

I often access shared network folders in Powershell to grab files etc. But if the share requires a username/password, Powershell does not prompt me for these, unlike Windows Explorer. If I connect to the folder first in Windows Explorer, Powershell will then allow me to connect.

如何在 Powershell 中验证自己的身份?

How can I authenticate myself in Powershell?

推荐答案

乍一看真的很想使用 New-PSDrive 提供它的凭据.

At first glance one really wants to use New-PSDrive supplying it credentials.

> New-PSDrive -Name P -PSProvider FileSystem -Root \\server\share -Credential domain\user

失败!

New-PSDrive : Cannot retrieve the dynamic parameters for the cmdlet. Dynamic parameters for NewDrive cannot be retrieved for the 'FileSystem' provider. The provider does not support the use of credentials. Please perform the operation again without specifying credentials.

文档指出您可以提供 PSCredential 对象,但如果您仔细观察,该 cmdlet 尚不支持此功能.也许在下一个版本中我猜.

The documentation states that you can provide a PSCredential object but if you look closer the cmdlet does not support this yet. Maybe in the next version I guess.

因此,您可以使用 net useWScript.Network 对象,调用 MapNetworkDrive 函数:

Therefore you can either use net use or the WScript.Network object, calling the MapNetworkDrive function:

$net = new-object -ComObject WScript.Network
$net.MapNetworkDrive("u:", "\\server\share", $false, "domain\user", "password")

<小时>

编辑 PowerShell 3.0 中的 New-PSDrive

显然,对于较新版本的 PowerShell,New-PSDrive cmdlet 可以使用凭据映射网络共享!


Edit for New-PSDrive in PowerShell 3.0

Apparently with newer versions of PowerShell, the New-PSDrive cmdlet works to map network shares with credentials!

New-PSDrive -Name P -PSProvider FileSystem -Root \\Server01\Public -Credential user\domain -Persist

这篇关于在 Powershell 中使用用户名/密码连接到网络文件夹的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆