Sep 16

VBScript Code

strComputer = “.”
Set objWMIService = GetObject(“winmgmts:\\” & strComputer & “\root\CIMV2″)
‘ Obtain an instance of the the class
‘ using a key property value.
Set objShare = objWMIService.Get(“SoftwareLicensingService.Version=’6.1.7600.16385′”)

‘ Obtain an InParameters object specific
‘ to the method.
Set objInParam = objShare.Methods_(“InstallProductKey”). _
    inParameters.SpawnInstance_()
‘ Add the input parameters.
objInParam.Properties_.Item(“ProductKey”) =  “ABCDE-FGHIJ-KLMNO-PQRST-UVWXY”

‘ Execute the method and obtain the return status.
‘ The OutParameters object in objOutParams
‘ is created by the provider.
Set objOutParams = objWMIService.ExecMethod(“SoftwareLicensingService.Version=’6.1.7600.16385′”, “InstallProductKey”, objInParam)

‘ List OutParams
Wscript.Echo “Out Parameters: ”
Wscript.echo “The objOutParams.ReturnValue variable contains an object.”

 

VB.Net Code

Imports System
Imports System.Management
Imports System.Windows.Forms

Namespace WMISample

    Public Class CallWMIMethod

        Public Overloads Shared Function Main() As Integer

            Try

                Dim classInstance As New ManagementObject( _
                    “root\CIMV2″, _
                    “SoftwareLicensingService.Version=’6.1.7600.16385′”, _
                    Nothing)

                ‘ Obtain [in] parameters for the method
                Dim inParams As ManagementBaseObject = _
                    classInstance.GetMethodParameters(“InstallProductKey”)

                ‘ Add the input parameters.
                inParams(“ProductKey”) =  “ABCDE-FGHIJ-KLMNO-PQRST-UVWXY”

                ‘ Execute the method and obtain the return values.
                Dim outParams As ManagementBaseObject = _
                    classInstance.InvokeMethod(“InstallProductKey”, inParams, Nothing)

                ‘ List outParams
                Console.WriteLine(“Out parameters:”)
                Console.WriteLine(“The ReturnValue out-parameter contains an object.”)

            Catch err As ManagementException

                MessageBox.Show(“An error occurred while trying to execute the WMI method: ” & err.Message)
            End Try
        End Function
    End Class
End Namespace

 

C# Code

using System;
using System.Management;
using System.Windows.Forms;

namespace WMISample
{
    public class CallWMIMethod
    {
        public static void Main()
        {
            try
            {
                ManagementObject classInstance =
                    new ManagementObject(“root\\CIMV2″,
                    “SoftwareLicensingService.Version=’6.1.7600.16385′”,
                    null);

                // Obtain in-parameters for the method
                ManagementBaseObject inParams =
                    classInstance.GetMethodParameters(“InstallProductKey”);

                // Add the input parameters.
                inParams["ProductKey"] =  “ABCDE-FGHIJ-KLMNO-PQRST-UVWXY”;

                // Execute the method and obtain the return values.
                ManagementBaseObject outParams =
                    classInstance.InvokeMethod(“InstallProductKey”, inParams, null);

                // List outParams
                Console.WriteLine(“Out parameters:”);
                Console.WriteLine(“The ReturnValue out-parameter contains an object.”);
            }
            catch(ManagementException err)
            {
                MessageBox.Show(“An error occurred while trying to execute the WMI method: ” + err.Message);
            }
        }
    }
}

Written by dozty | tags: , , ,