Back in September, I wrote a blog entry on connecting to a remote, non-authenticated network share.  Unfortunately, I made a couple of mistakes in my article.  😦

First off, there is no UnmapNetworkDrive method on the WScript.Network object.  I don’t know where I got that from.  I ended up finding the RemoveNetworkDrive method.  The only problem with that method is it will throw an exception if the drive letter doesn’t exist.  So, I ended up searching the PSDrives to see if the mapped drive already exists before unmapping it.  The last trick was to figure out if the resulting object is null.  To see if an object is null, all one has to do is do

Second, there was a missing parameter in the MapNetworkDrive.

Below is the new script

$pwd = convertto-securestring password -asplaintext -force
$cred = new-object System.Management.Automation.PsCredential "domainuser", $pwd
$net = New-Object -com WScript.Network

$mDrive = get-PSDrive | where-object { $_.root.StartsWith("newDriveLetter:") }

if ($mDrive)
{
    $net.RemoveNetworkDrive("newDriveLetter:")
}

$net.MapNetworkDrive("newDriveLetter:", “\123.123.123.123Share”, 0, "domain/user", $cred.GetNetworkCredential().Password)

Sorry for the confusion!

Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s