Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations strongm on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Wscript.network and Get-cred

Status
Not open for further replies.

Rockafly

IS-IT--Management
Nov 10, 2008
1
IE
Ok so I'm trying to map a network drive with a user name and password.

The issue I'm having is that the Get-creditial.password is not accepted as a parameter in Wscirpt.network.mapnetwork drive. This is the code I'm using


$cred = get-credential
$domain = "t2.local"
$user = $cred.UserName
$pwd = $cred.Password
$net = New-Object -com WScript.Network
$drive = "J:"
$path = "\\server\shared"
#$net.RemoveNetworkDrive($drive)
$net.mapnetworkdrive ($drive, $path, "true", $user, $pwd)

Now all works expect the $pwd variable. I'm guessing this is becuase Get-cred returns a system.security.securestring.

Is there a way to convert this?
 
You have to pass the username and password in clear text.

You do that by using $cred.GetNetworkCredential().UserName and $cred.GetNetworkCredential().Password

Code:
$cred = get-credential
$domain = "t2.local"
$user =  $cred.GetNetworkCredential().UserName
$pwd = $cred.GetNetworkCredential().Password
$net = New-Object -com WScript.Network
$drive = "J:"
$path = "\\server\shared"
#$net.RemoveNetworkDrive($drive)
$net.mapnetworkdrive ($drive, $path, "true", $user, $pwd)
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top