2

I have a problem where my mapped network drive becomes unavailable after a restart, trying various solutions to make it persistent [/persistent:yes when re-mapping via PowerShell, etc.]:

  • net use Z: "\\10.0.1.145\xyz" connects the drive fine but doesn't persist past a restart

I wrote a script to automate this for me:

  • If ( !(Test-Path -Path "\\10.0.1.145\") ) {
       net use Z: "\\10.0.1.145\xyz" /persistent:yes
    }
    
    • The odd thing is:
      Test-Path -Path "\\\10.0.1.145\\" = false
      Test-Path -Path "\\\10.0.1.145\xyz" = true

  • net use Z: "\\10.0.1.145\xyz" /persistent:yes gets the same result as the two Test-Path commands, with the only difference being Z: is accessible via CMD (Z: has been accessible via Explorer all along)

How can I map Z: persistently to 10.0.1.145 or reliably automate this if the former doesn't work?

3
  • is there a reason to NOT use New-PSDrive? in my experience, using that with -Persist and -Scope Global results in a drive map that stays thru reboots.
    – Lee_Dailey
    Commented Dec 17, 2021 at 22:31
  • The reason was only my unawareness. Just read over the docs and it sounds like it could solve the problem. Based on the command I used above, would you be able to tell me what the New-PSDrive command would be to map the drive?
    – Hewe
    Commented Dec 20, 2021 at 19:38
  • i think something like this would work >>> New-PSDrive -Name 'k' -PSProvider FileSystem -Root '\\MySysName\D$\Temp' -Scope 'Global' -Persist <<< ///// for more detail, take a look at Get-Help New-PSDrive -Examples.
    – Lee_Dailey
    Commented Dec 20, 2021 at 20:13

3 Answers 3

0

When Windows 10 logs on, there is a timing issue which causes it to attempt to map the network drives before the network is available, so all network mappings are disabled.

To fix:

  • Run Start > Edit Group Policy (or gpedit.msc)
  • Navigate to Computer Configuration > Administrative Templates > System > Logon
  • Set "Always Wait for the Network at Computer Startup and Logon" to "Enabled"
  • Click on Apply/OK.
4
  • Thank you for your reply. I followed the instructions and the drive seems to persist. However, when I open VS Code the program still doesn't have access to the drive. Any idea why programs wouldn't be able to see the drive?
    – Hewe
    Commented Dec 20, 2021 at 19:35
  • Does Explorer see and display its contents?
    – harrymc
    Commented Dec 20, 2021 at 19:43
  • Yes, it shows folders and I can traverse them correctly
    – Hewe
    Commented Dec 20, 2021 at 19:58
  • If you delete and recreate the share mapping, does it work?
    – harrymc
    Commented Dec 20, 2021 at 20:18
0

I had a similar issue and was able to workaround by adding the credentials to Generic Credentials in Credential Manager:

cmdkey /generic:10.0.1.145 /user:yourUser /pass:yourPass

0

This solves the problem for me1..

  1. Create C:\Scripts\NetworkDriveMap.ps1 script (create Scripts folder if not exist),
  2. Create C:\ProgramData\Microsoft\Windows\Start Menu\Programs\Startup\NetworkDriveMap.cmd script,
  3. Done.

.cmd script will be executed at Windows Startup, it will set the permissions and call .ps1 script, which will try to reconnect with the shared network (check %Temp%\StartupLogs.txt file after the reboot).

Here are the scripts

NetworkDriveMap.ps1

$i=3

while($True){

    $error.clear()

    $MappedDrives = Get-SmbMapping |where -property Status -Value Unavailable -EQ | select LocalPath,RemotePath

    foreach( $MappedDrive in $MappedDrives)

    {
        try {

            New-SmbMapping -LocalPath $MappedDrive.LocalPath -RemotePath $MappedDrive.RemotePath -Persistent $True

        } catch {

            Write-Host "There was an error mapping $MappedDrive.RemotePath to $MappedDrive.LocalPath"
        }
    }

    $i = $i - 1

    if($error.Count -eq 0 -Or $i -eq 0) {break}

    Start-Sleep -Seconds 30
}

NetworkDriveMap.cmd

PowerShell -Command "Set-ExecutionPolicy RemoteSigned -Scope CurrentUser" >> "%TEMP%\StartupLog.txt" 2>&1
PowerShell -File "%SystemDrive%\Scripts\NetworkDriveMap.ps1" >> "%TEMP%\StartupLog.txt" 2>&1

1 The source of this work is taken from this article, with minor edits (paths fixes, quotes).

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .