JQuery
JQuery

Reputation: 907

creating a mapped drive from variables

I had all this working but must have changed something because it doesn't now! I want to pass in variables and then from them create a mapped drive.

$d = "O:";
$s = "\\Server\Folder";
$u = "/user:DOMAIN\UserId password";
net use $d $s $u;

When I run that now, powershell just hangs. I don't get any errors or nothing it just "runs" but doesn't complete. I have to hit the stop script button. If I check net use there is no entry for it in there.

if I manually type net use O: \\Server\Folder /user:DOMAIN\UserId password then it works and creates the mapped drive.

I have tried escaping the backslash as per below:

$d = "O:";$s = "\\\\Server\Folder";$u = "/user:DOMAIN\\UserId password";

also building the string like (with and without the escaped backslashes.):

$n = $d+' '+$s+' '+$u;
net use n;

this method advises the network cannot be found, again, with and without the escaped backslashes. How ever If I echo $n and paste the code it creates the drive so I am totally at a loss!

Upvotes: 0

Views: 959

Answers (1)

TobyU
TobyU

Reputation: 3908

Calling a CMD command within a PowerShell script you should use Invoke-Expression.

$d = "O:";
$s = "\\Server\Folder";
$u = "/user:DOMAIN\UserId password";

Invoke-Expression "C:\Windows\System32\net.exe use $d $s $u"

Upvotes: 1

Related Questions