none
Mount VHD from Network Location VB Script

    Question

  • Hello, Would someone be able to help me please? I am trying to create a VB Script which will do the following: connect to a network share supplying the username and password mount a VHD which exists on that network share and assign the mounted VHD a drive letter run a backup command (i already know this bit) dismount the VHD and close the network share. thank you very much for any help.
    Monday, March 21, 2011 9:07 PM

Answers

  • Hi there Steve,

    You will need to use the NET USE command from the command line to map your drives and use the Diskpart tool to mount and detach your Virtual disks. Diskpart can be scripted to call a batch file by using the /s switch I've made some examples below:

    connect to a network share supplying the username and password 
     Net use Z: \\server\share /User:domainname\username password123

    mount a VHD which exists on that network share
    and assign the mounted VHD a drive letter

     diskpart /s Z:\script.txt

    {Contents of script.txt file}
     select vdisk file=Z:\Vdiskname.vhd
     attach vdisk
     assign

    run a backup command (i already know this bit)
    dismount the VHD

     diskpart /s Z:\script2.txt

    {Contents of script.txt file}
     select vdisk file=Z:\Vdiskname.vhd
     detach vdisk
     
    close the network share
     Net use Z: /d

     As you want it in VB Script I'd take a look here for the drive mapping. You can call your diskpart command lines using the windows scripting shell in your vbscript.

    Hope this helps.

     

    Sunday, March 27, 2011 8:14 AM
    Moderator
  • Hi Steve,

    I've given you windows command shell code. It's not best practice to mix your code.

    Firstly, we'll deal with the drive mappings. Use these VBScript routines.

    Set objNetwork = CreateObject("WScript.Network") 
    
    'connect to a network share supplying the username and password
    objNetwork.MapNetworkDrive "V:" , "\\machineX\C$" , false, "domain\Username", "strPassword"
    
    'close the network share
    objNetwork.RemoveNetworkDrive "v:" 
    
    • Marked as answer by Steve Mills Sunday, April 03, 2011 12:27 AM
    Thursday, March 31, 2011 3:14 PM
    Moderator
  • Try this:

    Put the command shell routine back in for mounting the VHD as you said it worked when run manually. Something like this:

    strCommand = "c:\Windows\System32\Diskpart.exe" & Chr(34) & " /s " & Chr(34) & "D:\Scripts\Backup Scripts\script.txt"
    Set objCmd = objWshShell.run(strCommand)
    
    

    It should prompt for elevation and run.

    Let me know how you get on please.

    • Marked as answer by Steve Mills Sunday, April 03, 2011 12:27 AM
    Friday, April 01, 2011 10:21 PM
    Moderator

All replies

  • Hi there Steve,

    You will need to use the NET USE command from the command line to map your drives and use the Diskpart tool to mount and detach your Virtual disks. Diskpart can be scripted to call a batch file by using the /s switch I've made some examples below:

    connect to a network share supplying the username and password 
     Net use Z: \\server\share /User:domainname\username password123

    mount a VHD which exists on that network share
    and assign the mounted VHD a drive letter

     diskpart /s Z:\script.txt

    {Contents of script.txt file}
     select vdisk file=Z:\Vdiskname.vhd
     attach vdisk
     assign

    run a backup command (i already know this bit)
    dismount the VHD

     diskpart /s Z:\script2.txt

    {Contents of script.txt file}
     select vdisk file=Z:\Vdiskname.vhd
     detach vdisk
     
    close the network share
     Net use Z: /d

     As you want it in VB Script I'd take a look here for the drive mapping. You can call your diskpart command lines using the windows scripting shell in your vbscript.

    Hope this helps.

     

    Sunday, March 27, 2011 8:14 AM
    Moderator
  • Hi,

    Thank you for your reply and example script. I have customised it and refined it but i get some execution problems at the points where i am running the commands. if i run these commands at a command prompt i get no problems at all, as soon as i put it in vbscript i do. i suspect its something to do with the spaces i have in the command, but i cant for the life of me remember how to pass the commands correctly in VB. Below is the full script that i have.

     

    Set objWshShell = CreateObject("WScript.shell")

    'connect to a network share supplying the username and password
    strCommand = "Net use V: \\machineX\C$ /User:MachineX\Steve MyLongPassword1234"

    objWshShell.Run strCommand, WindowStyle_Hidden, True


    'mount a VHD which exists on that network share
    'and assign the mounted VHD a drive letter

    strCommand = "diskpart /s D:\Scripts\Backup Scripts\script.txt"
    objWshShell.Run strCommand, WindowStyle_Hidden, True


    'run a backup command (i already know this bit)
    'dismount the VHD

    strCommand = "diskpart /s D:\Scripts\Backup Scripts\script2.txt"
    objWshShell.Run strCommand, WindowStyle_Hidden, True

     
    'close the network share
    strCommand = "Net use V: /d"
    objWshShell.Run strCommand, WindowStyle_Hidden, True

    Set objWshShell = Nothing

    WScript.Quit

    Thursday, March 31, 2011 1:11 PM
  • Hi Steve,

    I've given you windows command shell code. It's not best practice to mix your code.

    Firstly, we'll deal with the drive mappings. Use these VBScript routines.

    Set objNetwork = CreateObject("WScript.Network") 
    
    'connect to a network share supplying the username and password
    objNetwork.MapNetworkDrive "V:" , "\\machineX\C$" , false, "domain\Username", "strPassword"
    
    'close the network share
    objNetwork.RemoveNetworkDrive "v:" 
    
    • Marked as answer by Steve Mills Sunday, April 03, 2011 12:27 AM
    Thursday, March 31, 2011 3:14 PM
    Moderator
  • Next we'll deal with the VHD

    I havn't tested this myself but it will put you in the right direction.

    Option Explicit
    
    Dim WMIService
    Dim VHDService
    Dim VHD 
    
    'Specify the VHD to be mounted
    VHD = "V:\Windows.vhd" 
    
    'Get instance of 'virtualization
    ' WMI service on the local computer
    Set WMIService = GetObject("winmgmts:\\.\root\virtualization") 
    
    'Get the MSVM_ImageManagementService
    Set VHDService = WMIService.ExecQuery("SELECT * FROM Msvm_ImageManagementService").ItemIndex(0) 
    
    'Mount the VHD
    VHDService.Mount(VHD)
    
    'UnMount the VHD
    VHDService.UnMount(VHD)
    
    

     

    Thursday, March 31, 2011 3:24 PM
    Moderator
  • The problems in your script is privilage elevation. Diskpart requires elevated privilages to run.
    Thursday, March 31, 2011 3:25 PM
    Moderator
  • Hi,

    I am having problems with the virtualization bit, I forgot to mention that I am doing this on Windows 7, not sure if 7 has the virtualisation stuff, i thought it should do... but i dont know, unless there is some other WMI class im supposed to connect to for virtualization?? or does it look like i will have to use diskpart? and with regards to that you said about it needing elevation, well i run the vbs from an elevated command line, and it does the net use part fine, it mounts the VHD fine, it runs the backup fine.... but i get a problem with the dettaching part, it just doesnt seem to detach the VHD, yet it will get past that line of code and disconnect the network share with the next net use command.

    thanks

    Friday, April 01, 2011 8:21 PM
  • Try this:

    Put the command shell routine back in for mounting the VHD as you said it worked when run manually. Something like this:

    strCommand = "c:\Windows\System32\Diskpart.exe" & Chr(34) & " /s " & Chr(34) & "D:\Scripts\Backup Scripts\script.txt"
    Set objCmd = objWshShell.run(strCommand)
    
    

    It should prompt for elevation and run.

    Let me know how you get on please.

    • Marked as answer by Steve Mills Sunday, April 03, 2011 12:27 AM
    Friday, April 01, 2011 10:21 PM
    Moderator
  • Hello again,

    I have the following code now and works except for the detaching of VHD.

     

    Set objWshShell = CreateObject("WScript.shell")

    'connect to a network share supplying the username and password
    strCommand = "Net use V: \\machinex\share /User:machinex\user Password1 "
    objWshShell.Run strCommand, WindowStyle_Hidden,True


    'mount a VHD which exists on that network share
    'and assign the mounted VHD a drive letter

    strCommand = "diskpart /s " & Chr(34) & "D:\Scripts\Backup Scripts\script.txt" & Chr(34)
    objWshShell.Run strCommand, WindowStyle_Hidden,True


    'run a backup command
    strCommand = "WBADMIN START BACKUP -backupTarget:Z: -include:d: -allCritical -quiet"

    objWshShell.Run strCommand, WindowStyle_Hidden,True


    strCommand = "diskpart /s " & Chr(34) & "D:\Scripts\Backup Scripts\script2.txt" & Chr(34)
    objWshShell.Run strCommand, WindowStyle_Hidden,True

     
    'close the network share
    strCommand = "Net use V: /d /yes"
    objWshShell.Run strCommand, WindowStyle_Hidden,True

    Set objWshShell = Nothing

    WScript.Echo "A recent automatic backup has finished"

    WScript.Quit

    Drive Z in the backup command is the mounted VHD, the backup runs successfully. the entire script is ran from an elevated command prompt using cscript "scriptname.vbs"

    diskpart script.txt works fine, as does the following backup command, but when it gets to diskpart script2.txt it just will not dettach it, however if i remove the backup command and simply run it so it attaches, then detaches (sleeping a little in the middle so i dont miss the action) it seems to run fine. i am convinced this is probably due to elevation as wbadmin is a different process than diskpart, but am confused as to why diskpart works fine first, then hands off to wbadmin, but wont hand back to diskpart... its all in the same script so i thought it would all elevate together when executed?

     

    thanks

    Friday, April 01, 2011 11:48 PM
  • Perhaps the drive is not detaching as it is still in use. You can put a sleep or do while loop in there to pause until your backup ends.

    Mark all the above as answers that have helped if your scripts working now, please.

    Saturday, April 02, 2011 9:11 PM
    Moderator
  • yeah i put some sleeps in there but still no joy. any further suggestions? your answers have helped so far so thank you, but i will leave this thread open for a little while longer until i have exhuasted every avenue and then mark answers accordingly if that's ok.

    cheers.

    Saturday, April 02, 2011 9:15 PM
  • Try this Steve,

    'run a backup command
    strCommand = "WBADMIN START BACKUP -backupTarget:Z: -include:d: -allCritical -quiet"
    
    Set objSetupCmd = objWshShell.Exec(strCommand)
    Do While objSetupCmd.Status = 0
    	WScript.Sleep(100)
    Loop
    
    
    Saturday, April 02, 2011 10:14 PM
    Moderator
  • No Luck :( dont know what to do now.... it works fine without the backup command in there, its just after that runs it suddenly dont like to execute the detach command, i cant even detach it manually either because the net use command closes the share so it throws an error, i have to re-establish the network share, detach it manually, then remove the network share.
    Saturday, April 02, 2011 11:06 PM
  • The day i grow a brain cell will be the day i will be totally amazed at myself. so sorry for any head scratching.......there was one wrong letter in my script2.txt. what an idiot i am.

    thank you very much for your help and not losing your patience with me on this, i shall kick myself for hours and hours over this, its kept me awake for a very long time!

    cheers.

    Sunday, April 03, 2011 12:26 AM