0

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!

1 Answer 1

1

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"
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.