mora
mora

Reputation: 23

Mapping drive on a remote machine in powershell

I am working on a remote machine from my desktop and I have the following script :

Invoke-Command -computername $name -authentification default -credential $creds1 -scriptblock {net use \share $password2 /user:otherdomain\otheruser}

Then I get A specified logon session does not exist. It may have already been terminated. This thing is frustrating because if I run net use \\share $password2 /user:otherdomain\otheruser directly on the remote machine it works perfectly. Is there a workaround or did I miss something ?

Upvotes: 0

Views: 1251

Answers (1)

Ansgar Wiechers
Ansgar Wiechers

Reputation: 200523

You need to pass the variable $password2 into the script block, otherwise its value will be empty:

Invoke-Command -Computer $name ... -ScriptBlock {
  net use \\host\share $args[0] /user:otherdomain\otheruser
} -ArgumentList $password2

Upvotes: 0

Related Questions