Search code examples
azureazure-storage-files

Azure file share mapping on user startup fails when VM StoppedDeallocated


I am trying to automatically map Azure file share when VM starts up. The VM is set to login automatically and the script is on the user start up folder. The credentials are stored persistently. cmdkey /add:.file.core.windows.net /user: /pass: It works if the VM is rebooted but fails if it is shutdown completely with System error 1231. If I RDP and run the script it works. Does the shutdown cause any corruption?

@ECHO OFF 
net use Z: /delete
net use Z: \\share.file.core.windows.net\fileshare

timeout /t 10 /nobreak
START  "" "Z:" 
START "MyService" Z:\service\MyService.exe 

Solution

  • I have found a solution and the main cause of error was the actual network not being available when the script runs during log on. I was able to map it by catching the error message and retrying in a loop. I logged the errors and it seems to map correctly after few attempts. The final script is as following.

    @ECHO OFF 
    
    if not exist Z:\ (
    timeout /t 5 /nobreak
    
    :loop
    net use Z: \\share.file.core.windows.net\fileshare
    
    if errorlevel 1 (
    echo "Mapping failed"
    timeout /t 5
    goto loop
    ) else (
    echo "Mapping Successful" 
    )
    
    ) else (
    echo "Mapped already"
    )
    
    START  "" "Z:" 
    START "MyService" Z:\service\MyService.exe