Share via


ModifyVirtualSystem method of the Msvm_VirtualSystemManagementService class

Modifies the settings for an existing virtual computer system.

Syntax

uint32 ModifyVirtualSystem(
  [in]  CIM_ComputerSystem           REF ComputerSystem,
  [in]  string                           SystemSettingData,
  [out] CIM_VirtualSystemSettingData REF ModifiedSettingData,
  [out] CIM_ConcreteJob              REF Job
);

Parameters

ComputerSystem [in]

Type: CIM_ComputerSystem

A reference to the virtual computer system to be modified.

SystemSettingData [in]

Type: string

An embedded instance of the CIM_VirtualSystemSettingData class that describes the modified setting values for the virtual computer system.

ModifiedSettingData [out]

Type: CIM_VirtualSystemSettingData

A reference to the CIM_VirtualSystemSettingData instance that represents the object that was modified.

Job [out]

Type: CIM_ConcreteJob

An optional reference that is returned if the operation is executed asynchronously. If present, the returned reference to an instance of CIM_ConcreteJob can be used to monitor progress and to obtain the result of the method.

Return value

Type: uint32

If this method is executed synchronously, it returns 0 if it succeeds. If this method is executed asynchronously, it returns 4096 and the Job output parameter can be used to track the progress of the asynchronous operation. Any other return value indicates an error.

Completed with No Error (0)

Method Parameters Checked - Job Started (4096)

Failed (32768)

Access Denied (32769)

Not Supported (32770)

Status is unknown (32771)

Timeout (32772)

Invalid parameter (32773)

System is in used (32774)

Invalid state for this operation (32775)

Incorrect data type (32776)

System is not available (32777)

Out of memory (32778)

Remarks

Access to the Msvm_VirtualSystemManagementService class might be restricted by UAC Filtering. For more information, see User Account Control and WMI.

Examples

The following C# sample modifies the settings for a virtual system. The referenced utilities can be found in Common Utilities for the Virtualization Samples.

[!Important]
To function correctly, the following code must be run on the VM host server, and must be run with Administrator privileges.

using System;
using System.Management;

namespace HyperVSamples
{
    class ModifyVirtualSystemClass
    {
        static void ModifyVirtualSystem(string vmName, string vmNewName)
        {
            ManagementScope scope = new ManagementScope(@"root\virtualization", null);
            ManagementObject virtualSystemService = Utility.GetServiceObject(scope, "Msvm_VirtualSystemManagementService");

            ManagementBaseObject inParams = virtualSystemService.GetMethodParameters("ModifyVirtualSystem");

            ManagementObject vm = Utility.GetTargetComputer(vmName, scope);
            inParams["ComputerSystem"] = vm.Path.Path;

            ManagementObject           settingData  = null;
            ManagementObjectCollection settingsData = vm.GetRelated( "Msvm_VirtualSystemSettingData",
                                                                     "Msvm_SettingsDefineState",
                                                                     null,
                                                                     null,
                                                                     "SettingData",
                                                                     "ManagedElement",
                                                                     false,
                                                                     null);

            foreach (ManagementObject data in settingsData)
            {
                settingData = data;
            }

            settingData["ElementName"] = vmNewName;

            inParams["SystemsettingData"] = settingData.GetText(TextFormat.CimDtd20);

            ManagementBaseObject outParams = virtualSystemService.InvokeMethod("ModifyVirtualSystem", inParams, null);

            if ((UInt32)outParams["ReturnValue"] == ReturnCode.Started)
            {
                if (Utility.JobCompleted(outParams, scope))
                {
                    Console.WriteLine("VM '{0}' was renamed successfully.", vm["ElementName"]);

                }
                else
                {
                    Console.WriteLine("Failed to Modify VM");
                }
            }
            else if ((UInt32)outParams["ReturnValue"] == ReturnCode.Completed)
            {
                Console.WriteLine("VM '{0}' was renamed successfully.", vm["ElementName"]);
            }
            else
            {
                Console.WriteLine("Failed to modify VM. Error {0}", outParams["ReturnValue"]);
            }

            inParams.Dispose();
            outParams.Dispose();
            vm.Dispose();
            virtualSystemService.Dispose();
        }

        static void Main(string[] args)
        {
            if (args != null && args.Length != 2)
            {
                Console.WriteLine("Usage: ModifyVirtualSystem vmName vmNewName");
                return;
            }
            ModifyVirtualSystem(args[0], args[1]);
        }
    }
}

The following VBScript sample modifies the settings for a virtual system.

[!Important]
To function correctly, the following code must be run on the VM host server, and must be run with Administrator privileges.

dim objWMIService
dim managementService
dim fileSystem


const JobStarting = 3
const JobRunning = 4
const JobCompleted = 7
const wmiStarted = 4096
const wmiSuccessful = 0
const Saved = 32769

'VM Operational Status
const VMRunning = 2
const VMStopped = 10
const VMSaved = 15

Main()


'-----------------------------------------------------------------
' Main
'-----------------------------------------------------------------
Sub Main()
    set fileSystem = Wscript.CreateObject("Scripting.FileSystemObject")
    strComputer = "."
    set objWMIService = GetObject("winmgmts:\\" & strComputer & "\root\virtualization")
    GetManagmentServiceInstance
    set MyFirstVM = GetComputerSystem("My first VM")

    if ModifyVirtualSystem(MyFirstVM) then
        WriteLog "Done"
        WScript.Quit(0)
    else
        WriteLog "Failed"
        WScript.Quit(1)
    end if
End Sub


'-----------------------------------------------------------------
' Retrieve Msvm_VirtualSystemManagementService from WMI
'-----------------------------------------------------------------
Sub GetManagmentServiceInstance()
    query = "select * from Msvm_VirtualSystemManagementService"
    set managementService = objWMIService.ExecQuery(query).ItemIndex(0)
End Sub

'-----------------------------------------------------------------
' Retrieve Msvm_VirtualComputerSystem from base on its ElementName
' 
'-----------------------------------------------------------------
Function GetComputerSystem(vmElementName)
    On Error Resume Next
    query = Format1("select * from Msvm_ComputerSystem where ElementName = '{0}'", vmElementName)
    set GetComputerSystem = objWMIService.ExecQuery(query).ItemIndex(0)
    if (Err.Number <> 0) then
        WriteLog Format1("Err.Number: {0}", Err.Number)
        WriteLog Format1("Err.Description:{0}",Err.Description)
        WScript.Quit(1)
    end if
End Function

'-----------------------------------------------------------------
' Modify a virtual system
'-----------------------------------------------------------------
Function ModifyVirtualSystem(computerSystem)

    ModifyVirtualSystem = false
    set objInParam = managementService.Methods_("ModifyVirtualSystem").InParameters.SpawnInstance_()
    
    query = Format1("ASSOCIATORS OF {{0}} WHERE AssocClass = Msvm_SettingsDefineState resultClass = Msvm_VirtualSystemsettingData", computerSystem.Path_.Path)
    set virtualSystemsetting = objWMIService.ExecQuery(query).ItemIndex(0)
    virtualSystemsetting.ElementName = "Renamed VM"
    
    objInParam.ComputerSystem = computerSystem.Path_.Path
    objInParam.SystemsettingData = virtualSystemsetting.GetText_(1)
    
    set objOutParams = managementService.ExecMethod_("ModifyVirtualSystem", objInParam)

    if (WMIMethodStarted(objOutParams)) then
        if (WMIJobCompleted(objOutParams)) then
            ModifyVirtualSystem = true
        end if
    else
        if (wmiStatus = wmiSuccessful) then
            ModifyVirtualSystem = true
        else
            WriteLog Format1("ModifyVirtualSystem failed with ReturnValue {0}", wmiStatus)
        end if
    end if

End Function

'-----------------------------------------------------------------
' Handle wmi return values
'-----------------------------------------------------------------
Function WMIMethodStarted(outParam)

    WMIMethodStarted = false

    if Not IsNull(outParam) then
        wmiStatus = outParam.ReturnValue
        WriteLog Format1("WMI API return code: {0}", wmiStatus)

        if  wmiStatus = wmiStarted then
            WMIMethodStarted = true
        end if

    end if

End Function


'-----------------------------------------------------------------
' Handle wmi Job object
'-----------------------------------------------------------------
Function WMIJobCompleted(outParam)
    dim WMIJob
    WMIJobCompleted = true

    set WMIJob = objWMIService.Get(outParam.Job)

    while jobState = JobRunning or jobState = JobStarting

        WScript.Sleep(1000)
        set WMIJob = objWMIService.Get(outParam.Job)

    wend

    if (WMIJob.JobState <> JobCompleted) then
        WriteLog Format1("ErrorDescription:{0}", WMIJob.ErrorDescription)
        WMIJobCompleted = false
    end if

End Function

'-----------------------------------------------------------------
' Create the console log files.
'-----------------------------------------------------------------
Sub WriteLog(line)
    dim fileStream
    set fileStream = fileSystem.OpenTextFile(".\ADDSCSIContoller.log", 8, true)
    WScript.Echo line
    fileStream.WriteLine line
    fileStream.Close

End Sub


'------------------------------------------------------------------------------
' The string formatting functions to avoid string concatenation.
'------------------------------------------------------------------------------
Function Format2(myString, arg0, arg1)
    Format2 = Format1(myString, arg0)
    Format2 = Replace(Format2, "{1}", arg1)
End Function

'------------------------------------------------------------------------------
' The string formatting functions to avoid string concatenation.
'------------------------------------------------------------------------------
Function Format1(myString, arg0)
    Format1 = Replace(myString, "{0}", arg0)
End Function

Requirements

Minimum supported client
None supported
Minimum supported server
Windows Server 2008
End of client support
None supported
End of server support
Windows Server 2012
Namespace
Root\Virtualization
MOF
WindowsVirtualization.mof

See also

ModifySystemSettings (V2)

Msvm_VirtualSystemManagementService