none
Script AzMan Scopes in Hyper-V?

    Question

  •  Can anyone suggest the way to create AzMan scopes for VMs (I can assign VMs to scopes) with Powershell/WMI/VBScript?
    tony soper
    Tuesday, October 07, 2008 8:16 PM

Answers

  • CreateVMInScope

    Option Explicit

    Dim WMIService
    Dim VMManagementService
    Dim VMName
    Dim VMScope
    Dim VMSystemGlobalSettingData
    Dim Result
    Dim inParameters

    VMName = InputBox("Specify the name for the new virtual machine:")
    VMScope = InputBox("Specify the scope to be used for the new virtual machine:")

    'Get an instance of the WMI Service in the virtualization namespace.
    Set WMIService = GetObject("winmgmts:\\.\root\virtualization")

    'Get a VMManagementService object
    Set VMManagementService = WMIService.ExecQuery("SELECT * FROM Msvm_VirtualSystemManagementService").ItemIndex(0)

    ' Initialize the global settings for the VM
    Set VMSystemGlobalSettingData = WMIService.Get("Msvm_VirtualSystemGlobalSettingData").SpawnInstance_()

    'Set the name and scope
    VMSystemGlobalSettingData.ElementName = VMName
    VMSystemGlobalSettingData.ScopeOfResidence = VMScope

    ' Create the VM
    VMManagementService.DefineVirtualSystem(VMSystemGlobalSettingData.GetText_(1))



    tony soper
    • Marked as answer by tonysoper_MSFT Tuesday, October 07, 2008 10:05 PM
    Tuesday, October 07, 2008 8:18 PM
  • After you have created the scopes, you may need these:
    DisplayVMScopes
    ClearVMScopes
    ChangeVMScope
    ***************

    DisplayVMScopes

    Option Explicit
     
    Dim WMIService
    Dim VMList
    Dim VM
    Dim VMSystemGlobalSettingData
    Dim Message

    'Setup start of message string
    Message = "Virtual Machines and their scope of residence" & chr(10) _
              & "========================================"
     
    'Get instance of 'virtualization' WMI service on the local computer
    Set WMIService = GetObject("winmgmts:\\.\root\virtualization")
     
    'Get all the MSVM_ComputerSystem object
    Set VMList = WMIService.ExecQuery("SELECT * FROM Msvm_ComputerSystem")
       
    For Each VM In VMList
       if VM.Caption = "Virtual Machine" then
           Set VMSystemGlobalSettingData = (VM.Associators_("MSVM_ElementSettingData", "Msvm_VirtualSystemGlobalSettingData")).ItemIndex(0)
           Message = Message & chr(10) & "VM:       " & VM.ElementName
           Message = Message & chr(10) & "Scope:  " & VMSystemGlobalSettingData.ScopeOfResidence
           Message = Message & chr(10)
        end if
    Next

    wscript.echo Message

    **********************

    ClearVMScopes

    Option Explicit
     
    Dim WMIService
    Dim VMList
    Dim VM
    Dim VMSystemGlobalSettingData
    Dim VMManagementService
    Dim Result
     
    'Get instance of 'virtualization' WMI service on the local computer
    Set WMIService = GetObject("winmgmts:\\.\root\virtualization")

    'Get a VMManagementService object
    Set VMManagementService = WMIService.ExecQuery("SELECT * FROM Msvm_VirtualSystemManagementService").ItemIndex(0)
     
    'Get all the MSVM_ComputerSystem object
    Set VMList = WMIService.ExecQuery("SELECT * FROM Msvm_ComputerSystem")
       
    For Each VM In VMList
       if VM.Caption = "Virtual Machine" then
           Set VMSystemGlobalSettingData = (VM.Associators_("MSVM_ElementSettingData", "Msvm_VirtualSystemGlobalSettingData")).ItemIndex(0)
           VMSystemGlobalSettingData.ScopeOfResidence = ""
           Result = VMManagementService.ModifyVirtualSystem(VM.Path_.Path, VMSystemGlobalSettingData.GetText_(1))
        end if
    Next

    ******************************

    ChangeVMScope

    Option Explicit
     
    Dim WMIService
    Dim VM
    Dim VMManagementService
    Dim VMSystemGlobalSettingData
    Dim VMName
    Dim VMScope
    Dim Result
     
    'Setup variables for the VM we are looking for, and the scope to assign it to
    VMName = InputBox("Specify the virtual machine to change scope on:")
    VMScope = InputBox("Specify the new scope to be used:")
     
    'Get an instance of the WMI Service in the virtualization namespace.
    Set WMIService = GetObject("winmgmts:\\.\root\virtualization")
     
    'Get a VMManagementService object
    Set VMManagementService = WMIService.ExecQuery("SELECT * FROM Msvm_VirtualSystemManagementService").ItemIndex(0)
     
    'Get the VM object that we want to modify
    Set VM = (WMIService.ExecQuery("SELECT * FROM Msvm_ComputerSystem WHERE ElementName='" & VMName & "'")).ItemIndex(0)
     
    'Get the VirtualSystemGlobalSettingsData of the VM we want to modify
    Set VMSystemGlobalSettingData = (VM.Associators_("MSVM_ElementSettingData", "Msvm_VirtualSystemGlobalSettingData")).ItemIndex(0)
     
    'Change the ScopeOfResidence property
    VMSystemGlobalSettingData.ScopeOfResidence = VMScope
     
    'Update the VM with ModifyVirtualSystem
    Result = VMManagementService.ModifyVirtualSystem(VM.Path_.Path, VMSystemGlobalSettingData.GetText_(1))


    tony soper
    • Marked as answer by tonysoper_MSFT Tuesday, October 07, 2008 10:05 PM
    Tuesday, October 07, 2008 8:22 PM

All replies

  • CreateVMInScope

    Option Explicit

    Dim WMIService
    Dim VMManagementService
    Dim VMName
    Dim VMScope
    Dim VMSystemGlobalSettingData
    Dim Result
    Dim inParameters

    VMName = InputBox("Specify the name for the new virtual machine:")
    VMScope = InputBox("Specify the scope to be used for the new virtual machine:")

    'Get an instance of the WMI Service in the virtualization namespace.
    Set WMIService = GetObject("winmgmts:\\.\root\virtualization")

    'Get a VMManagementService object
    Set VMManagementService = WMIService.ExecQuery("SELECT * FROM Msvm_VirtualSystemManagementService").ItemIndex(0)

    ' Initialize the global settings for the VM
    Set VMSystemGlobalSettingData = WMIService.Get("Msvm_VirtualSystemGlobalSettingData").SpawnInstance_()

    'Set the name and scope
    VMSystemGlobalSettingData.ElementName = VMName
    VMSystemGlobalSettingData.ScopeOfResidence = VMScope

    ' Create the VM
    VMManagementService.DefineVirtualSystem(VMSystemGlobalSettingData.GetText_(1))



    tony soper
    • Marked as answer by tonysoper_MSFT Tuesday, October 07, 2008 10:05 PM
    Tuesday, October 07, 2008 8:18 PM
  • After you have created the scopes, you may need these:
    DisplayVMScopes
    ClearVMScopes
    ChangeVMScope
    ***************

    DisplayVMScopes

    Option Explicit
     
    Dim WMIService
    Dim VMList
    Dim VM
    Dim VMSystemGlobalSettingData
    Dim Message

    'Setup start of message string
    Message = "Virtual Machines and their scope of residence" & chr(10) _
              & "========================================"
     
    'Get instance of 'virtualization' WMI service on the local computer
    Set WMIService = GetObject("winmgmts:\\.\root\virtualization")
     
    'Get all the MSVM_ComputerSystem object
    Set VMList = WMIService.ExecQuery("SELECT * FROM Msvm_ComputerSystem")
       
    For Each VM In VMList
       if VM.Caption = "Virtual Machine" then
           Set VMSystemGlobalSettingData = (VM.Associators_("MSVM_ElementSettingData", "Msvm_VirtualSystemGlobalSettingData")).ItemIndex(0)
           Message = Message & chr(10) & "VM:       " & VM.ElementName
           Message = Message & chr(10) & "Scope:  " & VMSystemGlobalSettingData.ScopeOfResidence
           Message = Message & chr(10)
        end if
    Next

    wscript.echo Message

    **********************

    ClearVMScopes

    Option Explicit
     
    Dim WMIService
    Dim VMList
    Dim VM
    Dim VMSystemGlobalSettingData
    Dim VMManagementService
    Dim Result
     
    'Get instance of 'virtualization' WMI service on the local computer
    Set WMIService = GetObject("winmgmts:\\.\root\virtualization")

    'Get a VMManagementService object
    Set VMManagementService = WMIService.ExecQuery("SELECT * FROM Msvm_VirtualSystemManagementService").ItemIndex(0)
     
    'Get all the MSVM_ComputerSystem object
    Set VMList = WMIService.ExecQuery("SELECT * FROM Msvm_ComputerSystem")
       
    For Each VM In VMList
       if VM.Caption = "Virtual Machine" then
           Set VMSystemGlobalSettingData = (VM.Associators_("MSVM_ElementSettingData", "Msvm_VirtualSystemGlobalSettingData")).ItemIndex(0)
           VMSystemGlobalSettingData.ScopeOfResidence = ""
           Result = VMManagementService.ModifyVirtualSystem(VM.Path_.Path, VMSystemGlobalSettingData.GetText_(1))
        end if
    Next

    ******************************

    ChangeVMScope

    Option Explicit
     
    Dim WMIService
    Dim VM
    Dim VMManagementService
    Dim VMSystemGlobalSettingData
    Dim VMName
    Dim VMScope
    Dim Result
     
    'Setup variables for the VM we are looking for, and the scope to assign it to
    VMName = InputBox("Specify the virtual machine to change scope on:")
    VMScope = InputBox("Specify the new scope to be used:")
     
    'Get an instance of the WMI Service in the virtualization namespace.
    Set WMIService = GetObject("winmgmts:\\.\root\virtualization")
     
    'Get a VMManagementService object
    Set VMManagementService = WMIService.ExecQuery("SELECT * FROM Msvm_VirtualSystemManagementService").ItemIndex(0)
     
    'Get the VM object that we want to modify
    Set VM = (WMIService.ExecQuery("SELECT * FROM Msvm_ComputerSystem WHERE ElementName='" & VMName & "'")).ItemIndex(0)
     
    'Get the VirtualSystemGlobalSettingsData of the VM we want to modify
    Set VMSystemGlobalSettingData = (VM.Associators_("MSVM_ElementSettingData", "Msvm_VirtualSystemGlobalSettingData")).ItemIndex(0)
     
    'Change the ScopeOfResidence property
    VMSystemGlobalSettingData.ScopeOfResidence = VMScope
     
    'Update the VM with ModifyVirtualSystem
    Result = VMManagementService.ModifyVirtualSystem(VM.Path_.Path, VMSystemGlobalSettingData.GetText_(1))


    tony soper
    • Marked as answer by tonysoper_MSFT Tuesday, October 07, 2008 10:05 PM
    Tuesday, October 07, 2008 8:22 PM