PowerShell and WMI Class reference

  Kiến thức lập trình

I’m trying to create a WMI class property in one class that references a property of an instance in another WMI class, using PowerShell. I’ve been having a hard time trying to figure out the correct properties and qualifiers to get it working.

The ‘Warranty’ property in the $className class references the $classNameElement class.

if (!(Get-CimInstance -Namespace $Namespace -ClassName $classNameElement)){
    
        
    # Create a ManagementClass object bound to the WMI repository
    $managementClass = New-Object System.Management.ManagementClass($namespace, [String]::Empty, $null)

    # Specify the new class name
    $managementClass["__CLASS"] = $classNameElement

    # Add 'ID' property as a string and mark it as a key
    $managementClass.Properties.Add("ID", [System.Management.CimType]::String, $false)
    $managementClass.Properties["ID"].Qualifiers.Add("Key", $true)

    # Add 'Name' property as a string
    $managementClass.Properties.Add("Name", [System.Management.CimType]::String, $false)

    # Add 'Description' property as a string
    $managementClass.Properties.Add("Description", [System.Management.CimType]::String, $false)

    # Add 'Type' property as a string
    $managementClass.Properties.Add("Type", [System.Management.CimType]::String, $false)

    # Add 'Start' property as a string
    $managementClass.Properties.Add("Start", [System.Management.CimType]::String, $false)

    # Add 'End' property as a string
    $managementClass.Properties.Add("End", [System.Management.CimType]::String, $false)

    # Create the class in the WMI repository
    $managementClass.Put()
}

$managementClass = $null

if (!(Get-CimInstance -Namespace $Namespace -ClassName $className)){
    
    # Create a ManagementClass object bound to the WMI repository
    $managementClass = New-Object System.Management.ManagementClass($namespace, [String]::Empty, $null)

    # Specify the new class name
    $managementClass["__CLASS"] = $className

    # Add Association qualifier
    #$managementClass.Qualifiers.Add("Association",$true)

    # Add 'Serial' property as a string and mark it as a key
    $managementClass.Properties.Add("Serial", [System.Management.CimType]::String, $false)
    $managementClass.Properties["Serial"].Qualifiers.Add("Key", $true)

    # Add 'Country' property as a string
    $managementClass.Properties.Add("Country", [System.Management.CimType]::String, $false)

    # Add 'Warranty' property as a reference to the Device_WarrantyElement class
    $managementClass.Properties.Add("Warranty", [System.Management.CimType]::Reference, $true)

    $managementClass.Properties["Warranty"].Qualifiers.Add("CIMTYPE", "ref:Device_WarrantyElement")

    # Create the class in the WMI repository
    $managementClass.Put()


}

LEAVE A COMMENT