New-PSDrive Mapping Not Working With Hidden Network Share

This topic contains 6 replies, has 4 voices, and was last updated by Profile photo of Don Jones Don Jones 3 weeks, 2 days ago.

Viewing 7 posts - 1 through 7 (of 7 total)
  • Author
    Posts
  • #53623
    Profile photo of Jeff
    Jeff
    Participant

    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
        }
    }
    
    #53627
    Profile photo of Daniel Krebs
    Daniel Krebs
    Participant

    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

    • This reply was modified 3 weeks, 2 days ago by Profile photo of Daniel Krebs Daniel Krebs.
    #53712
    Profile photo of Edmond Yee
    Edmond Yee
    Participant

    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.

    #53717
    Profile photo of Jeff
    Jeff
    Participant

    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.

    #53721
    Profile photo of Jeff
    Jeff
    Participant

    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 3 weeks, 2 days ago by Profile photo of Jeff Jeff.
    #53728
    Profile photo of Jeff
    Jeff
    Participant

    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.

    #53732
    Profile photo of Don Jones
    Don Jones
    Keymaster

    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?

Viewing 7 posts - 1 through 7 (of 7 total)

You must be logged in to reply to this topic.