Monday, August 16, 2010

VBScripting for Hyper-V – Change Default Paths

After a while without any new articles here are some new things I’ve learned about Hyper-V.

This one it’s for everyone that needs to change the default Data and VHDs default locations.

Set objVWMIService = GetObject("winmgmts:\\.\root\Virtualization")
Set objVSMS = objVWMIService.ExecQuery("SELECT * From Msvm_VirtualSystemManagementService").ItemIndex(0)

Set objVSMSSData = objVWMIService.Get("Msvm_VirtualSystemManagementServiceSettingData").SpawnInstance_()
objVSMSSData.DefaultVirtualHardDiskPath = "D:\VirtualMachines\"
objVSMSSData.DefaultExternalDataRoot = "D:\VirtualMachines\"

Set objInputSet = objVSMS.Methods_("ModifyServiceSettings").InParameters.SpawnInstance_()
objInputSet.SettingData=objVSMSSData.GetText_(1)
Set objOutSet = objVSMS.ExecMethod_("ModifyServiceSettings",objInputSet)

if (objOutSet.ReturnValue = 0) Then
  Wscript.Echo "Changed the default VHD and Data Root"
elseif (objOutSet.ReturnValue <> 4096) Then
  Wscript.Echo "Failed to change the VHD and Data root to new values"
end If

Hope this helps!

No comments:

Post a Comment