在Powershell中使用用户名/密码连接到网络文件夹

时间:2008-11-19 19:27:27

标签: powershell

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

如何在Powershell中验证自己?

3 个答案:

答案 0 :(得分:60)

乍一看,确实想要使用New-PSDrive提供凭据。

> 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还是不支持这个。也许在下一个版本我想。

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

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

编辑New-PSDrive in PowerShell 3.0

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

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

答案 1 :(得分:40)

这不是PowerShell特定的答案,但您可以先使用“NET USE”对共享进行身份验证:

net use \\server\share /user:<domain\username> <password>

然后在PowerShell中做任何你需要做的事情......

答案 2 :(得分:9)

现在

PowerShell 3 supports this out of the box

如果您遇到PowerShell 2,则基本上必须使用旧版net use命令(如前所述)。