none
hyperv wmi How to pass a parameter as a string? RRS feed

  • Question

  • I use the newer Microsoft.Management.Infrastructure and I want to pass a parameter to the ModifySystemSettings method of the class Msvm_VirtualSystemManagementService:

    https://docs.microsoft.com/en-us/windows/win32/hyperv_v2/modifysystemsettings-msvm-virtualsystemmanagementservice

    The parameter type is a string that embedded an instance of the Msvm_VirtualSystemSettingData. 

    In the older version System.Management it is possible to use ManagementBaseObject.GetText(TextFormat.WmiDtd20) to convert it to string:

    https://docs.microsoft.com/en-us/dotnet/api/system.management.managementbaseobject.gettext?view=netframework-4.8

    The following code shows the current implementation of the ModifySystemSettings Method.
    Problem is that snapshotInstance.ToString() is obviously wrong. Does anyone know how to pass the parameter correctly? 


    CimInstance snapshotInstance = session.EnumerateAssociatedInstances(hvNamespace, vm, "Msvm_MostCurrentSnapshotInBranch", "Msvm_VirtualSystemSettingData", null, null).FirstOrDefault();
    
    snapshotInstance.CimInstanceProperties["ElementName"].Value = "testSnapshot";
    CimMethodParametersCollection modifySystemSettingsParameters = new CimMethodParametersCollection
    {
         CimMethodParameter.Create("SystemSettings",snapshotInstance.ToString(),CimType.String, CimFlags.In)
           };
     
    if(virtualSystemManagementService != null)
    {
          CimMethodResult result = session.InvokeMethod(hvNamespace, virtualSystemManagementService, "ModifySystemSettings", modifySystemSettingsParameters);
          Console.WriteLine(result.ReturnValue.Value.ToString());
    }

    My goal is to change the name of a snapshot. 
    • Edited by Rudinator Wednesday, February 26, 2020 12:24 PM
    Wednesday, February 26, 2020 12:23 PM

Answers

  • Hi Rudinator,

    Thank you for posting here.

    Since I don't have the test environment, I will try my best to give my suggestion.

    After searching online for related information, I find a PowerShell snippet:

    #Virtual System Management Service
    $VSMS = Get-CimInstance -Namespace root/virtualization/v2 -Class Msvm_VirtualSystemManagementService
     
    #Virtual Machine
    $VM = Get-CimInstance -Namespace root/virtualization/v2  -Class Msvm_ComputerSystem -Filter "ElementName='Demo-VM'"
     
    #Setting Data
    $SD = $vm | Get-CimAssociatedInstance -ResultClassName Msvm_VirtualSystemSettingData -Association Msvm_SettingsDefineState
    
    #Update bios uuid
    $SD.BIOSGUID = "some guid"
     
    #Create embedded instance
    $cimSerializer = [Microsoft.Management.Infrastructure.Serialization.CimSerializer]::Create()
    $serializedInstance = $cimSerializer.Serialize($SD, [Microsoft.Management.Infrastructure.Serialization.InstanceSerializationOptions]::None)
    $embeddedInstanceString = [System.Text.Encoding]::Unicode.GetString($serializedInstance)
     
    #Modify the system settings
    Invoke-CimMethod -CimInstance $VSMS -MethodName ModifySystemSettings @{SystemSettings = $embeddedInstanceString}

    You can try to convert it to C#.

    Here's another related reference.

    HyperV WMI Apply Snapshot in c#

    Hope them could be helpful.

    Best Regards,

    Xingyu Zhao


    MSDN Community Support
    Please remember to click "Mark as Answer" the responses that resolved your issue, and to click "Unmark as Answer" if not. This can be beneficial to other community members reading this thread. If you have any compliments or complaints to MSDN Support, feel free to contact MSDNFSF@microsoft.com.

    • Marked as answer by Rudinator Tuesday, March 3, 2020 7:00 AM
    Thursday, February 27, 2020 7:26 AM
    Moderator

All replies

  • Hi Rudinator,

    Thank you for posting here.

    Since I don't have the test environment, I will try my best to give my suggestion.

    After searching online for related information, I find a PowerShell snippet:

    #Virtual System Management Service
    $VSMS = Get-CimInstance -Namespace root/virtualization/v2 -Class Msvm_VirtualSystemManagementService
     
    #Virtual Machine
    $VM = Get-CimInstance -Namespace root/virtualization/v2  -Class Msvm_ComputerSystem -Filter "ElementName='Demo-VM'"
     
    #Setting Data
    $SD = $vm | Get-CimAssociatedInstance -ResultClassName Msvm_VirtualSystemSettingData -Association Msvm_SettingsDefineState
    
    #Update bios uuid
    $SD.BIOSGUID = "some guid"
     
    #Create embedded instance
    $cimSerializer = [Microsoft.Management.Infrastructure.Serialization.CimSerializer]::Create()
    $serializedInstance = $cimSerializer.Serialize($SD, [Microsoft.Management.Infrastructure.Serialization.InstanceSerializationOptions]::None)
    $embeddedInstanceString = [System.Text.Encoding]::Unicode.GetString($serializedInstance)
     
    #Modify the system settings
    Invoke-CimMethod -CimInstance $VSMS -MethodName ModifySystemSettings @{SystemSettings = $embeddedInstanceString}

    You can try to convert it to C#.

    Here's another related reference.

    HyperV WMI Apply Snapshot in c#

    Hope them could be helpful.

    Best Regards,

    Xingyu Zhao


    MSDN Community Support
    Please remember to click "Mark as Answer" the responses that resolved your issue, and to click "Unmark as Answer" if not. This can be beneficial to other community members reading this thread. If you have any compliments or complaints to MSDN Support, feel free to contact MSDNFSF@microsoft.com.

    • Marked as answer by Rudinator Tuesday, March 3, 2020 7:00 AM
    Thursday, February 27, 2020 7:26 AM
    Moderator
  • Please refer to some other similar examples. For example, as below:

    https://docs.microsoft.com/en-us/windows/win32/hyperv_v2/requeststatechange-msvm-computersystem#examples

    ManagementBaseObject inParams = vm.GetMethodParameters("RequestStateChange"); const int Enabled = 2; const int Disabled = 3; if (action.ToLower() == "start") { inParams["RequestedState"] = Enabled; } else if (action.ToLower() == "stop") { inParams["RequestedState"] = Disabled; } else { throw new Exception("Wrong action is specified"); } ManagementBaseObject outParams = vm.InvokeMethod( "RequestStateChange", inParams, null); if ((UInt32)outParams["ReturnValue"] == ReturnCode.Started) { if (Utility.JobCompleted(outParams, scope)) { Console.WriteLine( "{0} state was changed successfully.", vmName); } else { Console.WriteLine("Failed to change virtual system state"); } } else if ((UInt32)outParams["ReturnValue"] == ReturnCode.Completed) { Console.WriteLine( "{0} state was changed successfully.", vmName); } else { Console.WriteLine( "Change virtual system state failed with error {0}", outParams["ReturnValue"]); }

    Friday, February 28, 2020 5:28 AM
  • with the CimSerializer it worked out for me. Thank you very much! 
    Tuesday, March 3, 2020 7:00 AM