SimonS
SimonS

Reputation: 1973

mapping a networkdrive in PS 2.0 with encoded credentials

I need to map drives and I have an encoded PW which I need to connect to DMZ drives.

I'm using the following code to map the drives:

[Byte[]]$key = (1..16)
$pw = Get-Content .\LocalAdminCred.txt | ConvertTo-SecureString -key $key

# some other stuff

[string]$pwencoded = ConvertFrom-SecureString $pw -Key $key
$Map = New-Object -comobject Wscript.Network
$Map.MapNetworkDrive($_.letter,$_.path,$false,$_.username,$pwencoded)

What your eyes catches first is the $pwencoded Variable - I had to use a string for PW, that's why I needed to decode the Secure.String to a normal string, otherwise i would have had a type mismatch. now the problem is that .MapNetworkDrive needs the password in plain text like 'password' and mine is an encoded key and not a plain text.

To the question: Is it possible to use a encrypted password or even better a secure string to map my network drive this way? I really don't want to use plain text.

First I wanted to do the mapping with New-PSDrive but since the script must work for PS 2.0 I can't use that because the -persist Parameter doesn't exist there.

Upvotes: 0

Views: 420

Answers (1)

E.V.I.L.
E.V.I.L.

Reputation: 2166

I don't think this is the answer you're looking for but Dave Wyatt has a few blog posts about using secure strings and credentials.

... I also added an -AsPlainText switch to the ConvertFrom-SecureString command, in case you want to get the plain text back.

The only two ways I know how to map drives are to use plain text credentials. The only other way would be to run the script as an account that has permissions to the network location; that way you don't have to authenticate with a password.

Upvotes: 1

Related Questions