PowerShell中的“net use”,没有指定驱动器

时间:2016-10-12 22:18:42

标签: powershell

使用“net use”,您可以执行以下操作:

net use \\server /user:domian\username

然后它将提示输入密码,并且使用任何程序(cmd,Explorer,Word等)对该服务器进行的任何进一步的CIFS连接将自动使用该凭证。

在PowerShell中有相同的方法吗?我知道New-PsDrive通常提供“净使用”的答案,但它需要一封驱动器号来映射:

PS C:\WINDOWS\system32> New-PsDrive -PSProvider FileSystem -Root \\server -Credential domain\user
cmdlet New-PSDrive at command pipeline position 1
Supply values for the following parameters:
Name: 
New-PSDrive : Cannot bind argument to parameter 'Name' because it is an empty string.
At line:1 char:1
+ New-PsDrive -PSProvider FileSystem -Root \\server -Credential domain ...
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
    + CategoryInfo          : InvalidData: (:) [New-PSDrive], ParameterBindingValidationException
    + FullyQualifiedErrorId : ParameterArgumentValidationErrorEmptyStringNotAllowed,Microsoft.PowerShell.Commands.NewPSDriveCommand

我不想映射驱动器,我只想说“当我连接到此服务器时,请使用此凭据”。有办法吗?

3 个答案:

答案 0 :(得分:0)

cmdlet New-SMBMapping(基本上)在功能上等同于New-PSDrive。但是,不需要等效参数-LocalPath-Name中的New-PSDrive),因此您不应该收到上述错误。

答案 1 :(得分:0)

网络使用* \ server \ share / options

答案 2 :(得分:0)

对于那些不想在脚本中存储纯文本密码,而是要求用户提供凭据的人:

$credential = Get-Credential
New-SmbMapping -RemotePath \\server -UserName $credential.UserName -Password $credential.GetNetworkCredential().Password

来源:https://itluke.online/2019/06/04/how-to-manage-powershell-cmdlets-requiring-plain-text-passwords/