Search code examples
powershellwmi

PowerShell create WMI Class


I'm trying to create a WMI class and populate it

I can't quite get my head around "Enumeration Available"

I know how to make InvalidExtensions enumerable but I don't understand how to populate this field when creating a WMI instance

I also don't understand how to prefill Possible Enumeration Values

My code so far

$newClass = New-Object System.Management.ManagementClass("root\cimv2", [String]::Empty, $null); 
$newClass["__CLASS"] = "Test"; 
$newClass.Qualifiers.Add("Static", $true)

$newClass.Properties.Add("Username",[System.Management.CimType]::String, $false)
$newClass.Properties["Username"].Qualifiers.Add("Key", $true)

$newClass.Properties.Add("TotalFileCount",[System.Management.CimType]::UInt16, $false)

$newClass.Properties.Add("TotalFolderCount",[System.Management.CimType]::UInt16, $false)

$newClass.Properties.Add("TotalSizeMB",[System.Management.CimType]::UInt16, $false)

$newClass.Properties.Add("InvalidFileNameCount",[System.Management.CimType]::UInt16, $false)

$newClass.Properties.Add("InvalidExtensionCount",[System.Management.CimType]::UInt16, $false)

$newClass.Properties.Add("InvalidExtensions",[System.Management.CimType]::String, $false)
$newClass.Properties["InvalidExtensions"].Qualifiers.Add("Values",$true)

$newClass.Put()

Set-WmiInstance -Class Test -Argument @{Username="testaccount";TotalFileCount="5191";TotalFolderCount="355";TotalSizeMB="3660";InvalidFileNameCount="10";InvalidExtensionCount="2";InvalidExtensions=".appx",".ost"}

Solution

  • I worked out how to add the values

    Changed

    $newClass.Properties.Add("InvalidExtensions",[System.Management.CimType]::String, $false)

    to

    $newClass.Properties.Add("InvalidExtensions",[System.Management.CimType]::String,"StringArray")

    Then a standard array can be used when adding values

    I still don't know how to set possible values in the class properties