Author Posts

September 7, 2016 at 2:53 am

I have a script that is replacing old server names with new ones for mapped drives. It is working fine for most drives. However, it is not mapping the drive for hidden drives. You may ask why I'm using "net use" to delete the mappings. That is because I was receiving errors when piping results from Get-PSDrive and then creating the new drive again – using net use to delete the drive is the only way I could find to get rid of all traces. That is not why I am here though. I am having a hard time mapping hidden shares (shares with $ at the end). An example is below. The error I receive is "New-PSDrive : The network resource type is not correct." It seems like the only difference between this one (one that is not working) and one that is working is the dollar sign. Any ideas how I can get this to work?

$OldServer3 = "\\OLDSERVER\SHARE$"
$NewServer3 = "\\NEWSERVER\SHARE$"

$Drives3 = Get-WmiObject win32_mappedlogicaldisk | ? {$_.ProviderName -like "$($OldServer3)*" } 

If ($Drives3) {

    Foreach ($drive in $Drives3) { 
            
            "Replacing '$OldServer3' with '$NewServer3' for drive $($drive.DeviceID)" | Out-File $Log -Force -Append
            Net use /delete /yes $drive.DeviceID
            $Name = (($drive.DeviceID) -replace ":", "")
            $NewRoot = (($drive.ProviderName) -replace [regex]::Escape($OldServer3), $NewServer3)
            New-PSDrive -Name $Name -PSProvider FileSystem -Root $NewRoot -Persist -Scope global -Confirm:$false -ea SilentlyContinue
    }
}


	
			
	

September 7, 2016 at 3:06 am

Jeff,

You'll need to escape the dollar-sign with a backtick. Check out the "Escaping a variable" section in the about_Escape_Characters help: https://technet.microsoft.com/en-us/library/hh847755.aspx

$OldServer3 = "\\OLDSERVER\SHARE`$"
...

I hope that helps.

-Daniel

September 7, 2016 at 12:06 pm

If there are no variables in $oldserver3 or $newserver3, you can also enclose the path in single quotes, which will take the literal text inside of the quotes, even if there is a reserved character like a $ inside them.

September 7, 2016 at 12:36 pm

Daniel,

This does not appear to be working. The $NewRoot variable shows the dollar sign, not sure if that is the issue. I am using a back-tick before the dollar sign on both the old server and new server path. However, the same error happens.

September 7, 2016 at 12:42 pm

Even if changing the path directly for the "New-PSDrive" part, it doesn't work.

Does not work (New-PSDrive : The network resource type is not correct):

New-PSDrive -Name $Name -PSProvider FileSystem -Root "\\NEWSERVER\SHARE$" -Persist -Scope global -Confirm:$false

Does not work (New-PSDrive : The network resource type is not correct):

New-PSDrive -Name $Name -PSProvider FileSystem -Root "\\NEWSERVER\SHARE`$" -Persist -Scope global -Confirm:$false
  • This reply was modified 2 years ago by  Jeff.

September 7, 2016 at 1:39 pm

Turns out it was the new server – The new server share is not hidden, but the old one was. Sorry for the confusion! Dollar sign works just fine in the variable.

September 7, 2016 at 1:42 pm

That's odd. When you use -Persist, PowerShell actually calls an underlying Win32 API, and just passes all the information to it. Your first example should definitely not work, unless you switch to single-quotes, because of the $. I'm wondering if the string is just being passed literally in the second example, and the backtick is causing a problem. Could you try this using single quotes around the UNC? And also verify that this works using NET USE?