If you want to connect to a network share in PowerShell as a user other then the one which is executing the script, you usually have to prompt the user for credentials then pass these credentials to

New-Object-ComObject WScript.Network

This is annoying as it prevents the script from running without user intervention. I had been struggling for weeks to find a solution to this until two weeks ago when I managed to get the desired functionality from Net Use. Code is below:

# Cache the credentials for our network path.
try
{
   # Map to the path using the credentials provided.
   # $output is used to supress net use messages that
   # were appearing in the output.
   $output = net use "$path" "$pass" /USER:"$user"
}
catch
{
}

As you can see the code does not actually map the drive to an actual drive letter. It is merely creating a temporary connection to the drive so that we can then process what we want. After we have done our processing we can close down the connection with the following code:

$output += net use "$path" /DELETE /yes

The good thing about doing it this way is that I can pass credentials into my script from a XML file or in the actual script (I am using an account with read privs only at the destination for security reasons). Meaning that I don’t have to interact with the script at any point. This snippet forms part of my backup checking system which is run via a scheduled task every day.

If anyone can suggest a better way of doing this please let me know in the comments.

  • http://www.squidoo.com/storage-bench-with-baskets-hq entryway bench

    G’Day! Jason,
    Thanks for the info, For some reason, whenever I put in a retail/manufactured DVD into my Laptop DVD drive, nothing will happen. It’ll whirl around and make the noises as if it is going to do something, but eventually stops and nothing will happen. The funny thing is, it’ll play CD’s and home-made DVD’s (Data DVD’s, video DVD’s I’ve made myself, etc), but not movie DVD’s that you can buy from the store.

    I don’t know when this problem first began, but I do remember it happening to me once before. The solution to fix it was doing a certain things in Device Manager (like Disabling and enabling DMA in IDE Channels), but this time it’s not working at all. I’ve tried other things, like “Microsoft’s Fix It Solution” programs (the DVD and CD one in particular), but it gets to the part where it wants to install “Powershell”, then fails every time. So far, just about everything that I have tried keeps failing.

    Anyone have any solutions that I can try? I’d deeply appreciate any help I can get.

    =======
    Some System Specs~

    Windows Vista Home Premium (SP1)(I plan to upgrade to SP2 eventually, but I don’t think that’ll solve my problem)
    HP Pavilion DV6609wm
    The disc drive is a TSSTcorp CD/DVDW TS-L632D ATA Device
    BTW great blogpost

    • Jason

      It’s worth upgrading to SP2 for the fixes it brings. To be honest you could try downloading something like VLC Media Player as your issue sounds like your machine lacks the codecs to play DVDs.