Mikhail R
Mikhail R

Reputation: 421

Mount password protected network drive via PowerShell

I try to mount network drive by using this command

New-PSDrive –Name “C” –PSProvider FileSystem –Root “\\re\Pro\Al\A\V Machines\Vagrant machines” –Persist

But when you mount this drive it asks you to enter your domain name and password.

I tried to use this command:

New-PSDrive -Name "P" -PSProvider FileSystem -Root “\\re\Pro\Al\A\V Machines\Vagrant machines” -Credential user\domain -Persist

And now I see modal window where I see two field, and "Password" field is empty.

Is there any ability to mount drive automatically by using credentials (I have username and password)?

Upvotes: 0

Views: 8706

Answers (2)

Zalatschenko
Zalatschenko

Reputation: 1

An other Option would be to add your Credentials to the Windows Vault:

#######################################
#Variables:
$target = <domain>
$user = <username>
$Password = <Password>
#######################################
If($Password) 
{ 
    [string]$result = cmdkey /add:$target /user:$UserName /pass:$Password 
} 
Else 
{ 
    [string]$result = cmdkey /add:$target /user:$UserName  
} 
If($result -match "The command line parameters are incorrect") 
{ 
    Write-Error "Failed to add Windows Credential to Windows vault." 
} 
ElseIf($result -match "CMDKEY: Credential added successfully") 
{ 
    Write-Host "Credential added successfully." 
} 
#######################################################

Upvotes: 0

Trey Nuckolls
Trey Nuckolls

Reputation: 591

You can pass the credentials to the new-drive by putting them into a pscredential object.

$secpasswd = ConvertTo-SecureString “PlainTextPassword” -AsPlainText -Force
$mycreds = New-Object System.Management.Automation.PSCredential (“username”, $secpasswd)
New-PSDrive –Name “P” –PSProvider FileSystem –Root “\\re\Pro\Al\A\V Machines\Vagrant machines” –Persist -Credential $mycreds

You might consider saving the credential set in a locally encrypted file (not the best but better than plaintext) if you plan on using this in a saved script.

Link > Use current Powershell credentials for remote call

Upvotes: 1

Related Questions