Windows Vista Forums

Creating a WMI class

  1. #1


    urkec Guest

    Creating a WMI class

    I am trying to create a new join view class in WMI. I need to create an empty
    class, set it's properties and qualifiers and save it to the repository. In
    VBScript it is done by binding to the SWBemServices object and using it's Get
    method to create an empty class, adding properties and using the Put method
    to save the new class. With PowerShell I tried to use

    $ViewClass = New-Object System.Management.ManagementClass
    $ViewClass.Path.ClassName = "Test"

    but I get an error that the ClassName property doesn't exists, even if it
    does (System.Management.ManagementPath class). I tried to use

    $ViewClass = New-Object System.Management.ManagementClass
    $ViewClass.Put()

    but the PowerGui script editor throws an exception. I also tried

    $ViewClass = New-Object System.Management.ManagementObject
    $ViewClass.Path.ClassName = "Test"

    but I get the same error. This is probably a simple question, but I couldn't
    find anything with Google or searching this group.

    Thank you.

    --
    urkec

      My System SpecsSystem Spec

  2. #2


    Darren Mar-Elia Guest

    Re: Creating a WMI class

    How about this:

    $ViewClass = New-Object System.Management.ManagementObject
    $ViewClass.PSBase.ClassPath.ClassName = "Test"

    Darren
    --
    Darren Mar-Elia
    MS-MVP-Windows Server--Group Policy

    Manage Group Policy Backup and Recovery with the GPExpert Backup Manager for
    GP!
    Find out more at http://www.sdmsoftware.com/products.php

    Visit the GPOGUY: http://www.gpoguy.com -- The Windows Group Policy
    Information Hub:
    FAQs, Training Videos, Whitepapers and Utilities for all things Group
    Policy-related



    "urkec" <urkec@xxxxxx> wrote in message
    news:9991B00E-54E1-46CE-AAE6-EBF40B490D8F@xxxxxx

    >I am trying to create a new join view class in WMI. I need to create an
    >empty
    > class, set it's properties and qualifiers and save it to the repository.
    > In
    > VBScript it is done by binding to the SWBemServices object and using it's
    > Get
    > method to create an empty class, adding properties and using the Put
    > method
    > to save the new class. With PowerShell I tried to use
    >
    > $ViewClass = New-Object System.Management.ManagementClass
    > $ViewClass.Path.ClassName = "Test"
    >
    > but I get an error that the ClassName property doesn't exists, even if it
    > does (System.Management.ManagementPath class). I tried to use
    >
    > $ViewClass = New-Object System.Management.ManagementClass
    > $ViewClass.Put()
    >
    > but the PowerGui script editor throws an exception. I also tried
    >
    > $ViewClass = New-Object System.Management.ManagementObject
    > $ViewClass.Path.ClassName = "Test"
    >
    > but I get the same error. This is probably a simple question, but I
    > couldn't
    > find anything with Google or searching this group.
    >
    > Thank you.
    >
    > --
    > urkec

      My System SpecsSystem Spec

  3. #3


    RichS Guest

    RE: Creating a WMI class

    I must admit that I'm not 100% sure what you are trying to achieve from your
    question but if you want to create a new instance of a class in WMI in
    PowerShell V1 the normal way to do it is

    $w = [WMIClass]"\\.\root\cimv2:Win32_Process"
    $w.Create("Notepad.exe")

    Can you post an example of what you are trying to do and which class you are
    using. It can be in VBScript if necessary.
    --
    Richard Siddaway
    Please note that all scripts are supplied "as is" and with no warranty
    Blog: http://richardsiddaway.spaces.live.com/
    PowerShell User Group: http://www.get-psuguk.org.uk


    "urkec" wrote:

    > I am trying to create a new join view class in WMI. I need to create an empty
    > class, set it's properties and qualifiers and save it to the repository. In
    > VBScript it is done by binding to the SWBemServices object and using it's Get
    > method to create an empty class, adding properties and using the Put method
    > to save the new class. With PowerShell I tried to use
    >
    > $ViewClass = New-Object System.Management.ManagementClass
    > $ViewClass.Path.ClassName = "Test"
    >
    > but I get an error that the ClassName property doesn't exists, even if it
    > does (System.Management.ManagementPath class). I tried to use
    >
    > $ViewClass = New-Object System.Management.ManagementClass
    > $ViewClass.Put()
    >
    > but the PowerGui script editor throws an exception. I also tried
    >
    > $ViewClass = New-Object System.Management.ManagementObject
    > $ViewClass.Path.ClassName = "Test"
    >
    > but I get the same error. This is probably a simple question, but I couldn't
    > find anything with Google or searching this group.
    >
    > Thank you.
    >
    > --
    > urkec

      My System SpecsSystem Spec

  4. #4


    urkec Guest

    RE: Creating a WMI class

    "RichS" wrote:

    > I must admit that I'm not 100% sure what you are trying to achieve from your
    > question but if you want to create a new instance of a class in WMI in
    > PowerShell V1 the normal way to do it is
    >
    > $w = [WMIClass]"\\.\root\cimv2:Win32_Process"
    > $w.Create("Notepad.exe")
    >
    > Can you post an example of what you are trying to do and which class you are
    > using. It can be in VBScript if necessary.

    > > I am trying to create a new join view class in WMI. I need to create an empty
    > > class, set it's properties and qualifiers and save it to the repository.
    I am trying to create WMI join view classes. I managed to register the view
    provider, but I can't figure out how to create a new class (not an instance
    of an existing class). Basically I want to replicate a MOF like this one with
    PowerShell:

    [JoinOn("Win32_DiskPartition.DiskIndex = Win32_DiskDrive.Index"),
    ViewSources{"SELECT DiskIndex, Index, DeviceID FROM Win32_DiskPartition" ,
    "SELECT Index, Caption, Model FROM Win32_DiskDrive"},
    ViewSpaces{"\\\\.\\root\\cimv2", "\\\\.\\root\\cimv2"},
    dynamic: ToInstance, provider("MS_VIEW_INSTANCE_PROVIDER")]

    class PrinterPropsd
    {
    [PropertySources{"DiskIndex", "Index"}] UInt32 ID;
    [PropertySources{"", "Caption"}] String Caption;
    [PropertySources{"Index", ""}, key] Uint32 Index;
    [PropertySources{"DeviceID", ""}] String DeviceId;
    [PropertySources{"", "Model"}] String Model;
    };

    In VBScript I would get the SWbemServices object and use it's Get method to
    get an empty SWBemObject, make it a class with properties and qualifiers like
    above and save it to the repository as a new class. This is the part of WMI
    SDK where I found this:

    http://msdn2.microsoft.com/en-us/lib...65(VS.85).aspx

    This is the sample join view class:

    http://msdn2.microsoft.com/en-us/lib...54(VS.85).aspx

    This is how a new class is created with VBScript:

    set NewObject = GetObject("winmgmts:root\default").Get
    NewObject.Path_.Class = "Test"
    NewObject.Properties_.Add "A", wbemCimtypeString

    I tried Darren's suggestion and it didn't error out on the second line

    $ViewClass = New-Object System.Management.ManagementClass
    $ViewClass.PSBase.ClassPath.ClassName = "Test"
    $ViewClass.PSBase.GetType()

    $ViewClass.PSBase.Properties.Add("A", "A")
    $ViewClass.PSBase.Put()

    but when I try to use Put method I get "Invalid operation" error.

    Thanks.

    --
    urkec

      My System SpecsSystem Spec

  5. #5


    Darren Mar-Elia Guest

    Re: Creating a WMI class

    In looking at the docs on the Put method, it looks like it expects a param
    of type PutOptions, so that could be why the Put is erroring out.

    Darren


    "urkec" <urkec@xxxxxx> wrote in message
    news:FF95B377-2446-400E-AF9D-A2EF9CA133FB@xxxxxx

    > "RichS" wrote:
    >

    >> I must admit that I'm not 100% sure what you are trying to achieve from
    >> your
    >> question but if you want to create a new instance of a class in WMI in
    >> PowerShell V1 the normal way to do it is
    >>
    >> $w = [WMIClass]"\\.\root\cimv2:Win32_Process"
    >> $w.Create("Notepad.exe")
    >>
    >> Can you post an example of what you are trying to do and which class you
    >> are
    >> using. It can be in VBScript if necessary.
    >

    >> > I am trying to create a new join view class in WMI. I need to create an
    >> > empty
    >> > class, set it's properties and qualifiers and save it to the
    >> > repository.
    >
    > I am trying to create WMI join view classes. I managed to register the
    > view
    > provider, but I can't figure out how to create a new class (not an
    > instance
    > of an existing class). Basically I want to replicate a MOF like this one
    > with
    > PowerShell:
    >
    > [JoinOn("Win32_DiskPartition.DiskIndex = Win32_DiskDrive.Index"),
    > ViewSources{"SELECT DiskIndex, Index, DeviceID FROM Win32_DiskPartition"
    > ,
    > "SELECT Index, Caption, Model FROM Win32_DiskDrive"},
    > ViewSpaces{"\\\\.\\root\\cimv2", "\\\\.\\root\\cimv2"},
    > dynamic: ToInstance, provider("MS_VIEW_INSTANCE_PROVIDER")]
    >
    > class PrinterPropsd
    > {
    > [PropertySources{"DiskIndex", "Index"}] UInt32 ID;
    > [PropertySources{"", "Caption"}] String Caption;
    > [PropertySources{"Index", ""}, key] Uint32 Index;
    > [PropertySources{"DeviceID", ""}] String DeviceId;
    > [PropertySources{"", "Model"}] String Model;
    > };
    >
    > In VBScript I would get the SWbemServices object and use it's Get method
    > to
    > get an empty SWBemObject, make it a class with properties and qualifiers
    > like
    > above and save it to the repository as a new class. This is the part of
    > WMI
    > SDK where I found this:
    >
    > http://msdn2.microsoft.com/en-us/lib...65(VS.85).aspx
    >
    > This is the sample join view class:
    >
    > http://msdn2.microsoft.com/en-us/lib...54(VS.85).aspx
    >
    > This is how a new class is created with VBScript:
    >
    > set NewObject = GetObject("winmgmts:root\default").Get
    > NewObject.Path_.Class = "Test"
    > NewObject.Properties_.Add "A", wbemCimtypeString
    >
    > I tried Darren's suggestion and it didn't error out on the second line
    >
    > $ViewClass = New-Object System.Management.ManagementClass
    > $ViewClass.PSBase.ClassPath.ClassName = "Test"
    > $ViewClass.PSBase.GetType()
    >
    > $ViewClass.PSBase.Properties.Add("A", "A")
    > $ViewClass.PSBase.Put()
    >
    > but when I try to use Put method I get "Invalid operation" error.
    >
    > Thanks.
    >
    > --
    > urkec

      My System SpecsSystem Spec

  6. #6


    urkec Guest

    Re: Creating a WMI class

    There is also Put overload without parameters. I haven't had time to continue
    testing, but thank you for your help. I am new to PowerShell and .Net, but
    creating a new class in WMI is a common task using VBScript or MOF, so I
    suppose there must be a way to get this to work. Thanks.

    --
    urkec


    "Darren Mar-Elia" wrote:

    > In looking at the docs on the Put method, it looks like it expects a param
    > of type PutOptions, so that could be why the Put is erroring out.
    >
    > Darren
    >
    >
    > "urkec" <urkec@xxxxxx> wrote in message
    > news:FF95B377-2446-400E-AF9D-A2EF9CA133FB@xxxxxx

    > > "RichS" wrote:
    > >

    > >> I must admit that I'm not 100% sure what you are trying to achieve from
    > >> your
    > >> question but if you want to create a new instance of a class in WMI in
    > >> PowerShell V1 the normal way to do it is
    > >>
    > >> $w = [WMIClass]"\\.\root\cimv2:Win32_Process"
    > >> $w.Create("Notepad.exe")
    > >>
    > >> Can you post an example of what you are trying to do and which class you
    > >> are
    > >> using. It can be in VBScript if necessary.
    > >

    > >> > I am trying to create a new join view class in WMI. I need to create an
    > >> > empty
    > >> > class, set it's properties and qualifiers and save it to the
    > >> > repository.
    > >
    > > I am trying to create WMI join view classes. I managed to register the
    > > view
    > > provider, but I can't figure out how to create a new class (not an
    > > instance
    > > of an existing class). Basically I want to replicate a MOF like this one
    > > with
    > > PowerShell:
    > >
    > > [JoinOn("Win32_DiskPartition.DiskIndex = Win32_DiskDrive.Index"),
    > > ViewSources{"SELECT DiskIndex, Index, DeviceID FROM Win32_DiskPartition"
    > > ,
    > > "SELECT Index, Caption, Model FROM Win32_DiskDrive"},
    > > ViewSpaces{"\\\\.\\root\\cimv2", "\\\\.\\root\\cimv2"},
    > > dynamic: ToInstance, provider("MS_VIEW_INSTANCE_PROVIDER")]
    > >
    > > class PrinterPropsd
    > > {
    > > [PropertySources{"DiskIndex", "Index"}] UInt32 ID;
    > > [PropertySources{"", "Caption"}] String Caption;
    > > [PropertySources{"Index", ""}, key] Uint32 Index;
    > > [PropertySources{"DeviceID", ""}] String DeviceId;
    > > [PropertySources{"", "Model"}] String Model;
    > > };
    > >
    > > In VBScript I would get the SWbemServices object and use it's Get method
    > > to
    > > get an empty SWBemObject, make it a class with properties and qualifiers
    > > like
    > > above and save it to the repository as a new class. This is the part of
    > > WMI
    > > SDK where I found this:
    > >
    > > http://msdn2.microsoft.com/en-us/lib...65(VS.85).aspx
    > >
    > > This is the sample join view class:
    > >
    > > http://msdn2.microsoft.com/en-us/lib...54(VS.85).aspx
    > >
    > > This is how a new class is created with VBScript:
    > >
    > > set NewObject = GetObject("winmgmts:root\default").Get
    > > NewObject.Path_.Class = "Test"
    > > NewObject.Properties_.Add "A", wbemCimtypeString
    > >
    > > I tried Darren's suggestion and it didn't error out on the second line
    > >
    > > $ViewClass = New-Object System.Management.ManagementClass
    > > $ViewClass.PSBase.ClassPath.ClassName = "Test"
    > > $ViewClass.PSBase.GetType()
    > >
    > > $ViewClass.PSBase.Properties.Add("A", "A")
    > > $ViewClass.PSBase.Put()
    > >
    > > but when I try to use Put method I get "Invalid operation" error.
    > >
    > > Thanks.
    > >
    > > --
    > > urkec
    >

      My System SpecsSystem Spec


Creating a WMI class
Similar Threads
Thread Forum
Moving from class C to class B ip address scheme, any tips(DC/DNS) Server General
When a class is both an inherited class of another, and alsoimplements an interface method .NET General
Creating a class in a HTA VB Script
win32_pingstatus class / dns class PowerShell
Creating a Custom Enumerator in a Base Class .NET General