i am hoping some one can help me with an issue that i am having. I am need to create a Custom WMI class in root/cmiv2 and add entries from the registry into to that custom class. Ideally i would like to have the code just to look at the regkey go though everything thats in it and add those entries to custom wmi class.
My code is:
$RegKeyName = "OSDBuild"
# Set values
$tsenv = New-Object -COMObject Microsoft.SMS.TSEnvironment
$FullRegKeyName = "HKLM:SOFTWARE" + $regkeyname
# Create Registry key
New-Item -Path $FullRegKeyName -type Directory -Force -ErrorAction SilentlyContinue
# Get values
$InstallTime = Get-Date -Format G
$OSDStartTime = $tsenv.value("OSDStartTSTime")
$AdvertisementID = $tsenv.value("_SMSTSAdvertID")
$Organisation = $tsenv.value("_SMSTSOrgName")
$TaskSequenceID = $tsenv.value("_SMSTSPackageID")
$Packagename = $tsenv.value("_SMSTSPackageName")
$MachineName = $env:computername
$Installationmode = $tsenv.value("_SMSTSLaunchMode")
$BuildRevision = $tsenv.value("BuildRevision")
$OSName = $tsenv.vlaue("OSName")
$OSDDateUTC = (Get-Date).ToUniversalTime()
#Calculate time elapsed
$OSDTImeSpan = New-TimeSpan -start $OSDStartTime -end $InstallTime
$OSDDuration = "{0:hh}:{0:mm}:{0:ss}" -f $OSDTimeSpan
# Write values
new-itemProperty $FullRegKeyName -Name "InstalledDate" -value $InstallTime -Type STRING -Force -ErrorAction SilentlyContinue | Out-Null
new-itemProperty $FullRegKeyName -Name "OSDStartTime" -value $OSDStartTime -Type STRING -Force -ErrorAction SilentlyContinue | Out-Null
New-ItemProperty $FullRegKeyName -Name "OSDDuration" -value $OSDDuration -Type STRING -Force -ErrorAction SilentlyContinue | Out-Null
new-itemProperty $FullRegKeyName -Name "OrganisationName" -value $Organisation -Type STRING -Force -ErrorAction SilentlyContinue | Out-Null
new-itemProperty $FullRegKeyName -Name "AdvertisementID" -value $AdvertisementID -Type STRING -Force -ErrorAction SilentlyContinue | Out-Null
new-itemProperty $FullRegKeyName -Name "TaskSequenceID" -value $TaskSequenceID -Type STRING -Force -ErrorAction SilentlyContinue | Out-Null
new-itemProperty $FullRegKeyName -Name "Task Sequence Name" -value $Packagename -Type STRING -Force -ErrorAction SilentlyContinue | Out-Null
new-itemProperty $FullRegKeyName -Name "Installation Type" -value $Installationmode -Type STRING -Force -ErrorAction SilentlyContinue | Out-Null
new-itemProperty $FullRegKeyName -Name "Computername" -value $MachineName -Type STRING -Force -ErrorAction SilentlyContinue | Out-Null
new-itemProperty $FullRegKeyName -Name "OSVersion" -value (Get-CimInstance Win32_Operatingsystem).version -PropertyType String -Force | Out-Null
new-itemProperty $FullRegKeyName -Name "BuildRevision" -value $BuildRevision -Type STRING -Force -ErroAction SilentlyContinue | Out-Null
new-itemProperty $FullRegKeyName -Name "OSVersionName" -value $OSversionName -Type STRING -Force -ErrorAction SilentlyContinue | Out-Null
new-itemProperty $FullRegKeyName -Name "OSDDateUTC" -value $OSDDateUTC -Type STRING -Force -ErrorAction SilentlyContinue | Out-Null
I tried the following code, but it did not work.
# Define the namespace and class name
$namespace = "rootcimv2"
$className = "OSDBuild"
# Define the class properties
$classProperties = @(
New-CimProperty -Name "RegistryKeyName" -CimType String -Flags "Key, ReadOnly"
New-CimProperty -Name "RegistryValueName" -CimType String -Flags "ReadOnly"
New-CimProperty -Name "RegistryValueData" -CimType String -Flags "ReadOnly"
)
# Create the class
$class = New-CimClass -Namespace $namespace -ClassName $className -PropertyDefinition $classProperties
# Define the registry path and values
$registryPath = "HKLM:SOFTWAREOSDBuild"
$registryKey = "AdvertisementID"
$registryKey = "BuildRevision"
$registryKey = "Computername"
$registryKey = "Installation Type"
$registryKey = "InstallationDate"
$registryKey = "OrganisationName"
$registryKey = "OSDDuration"
$registryKey = "OSDStartTime"
$registryKey = "OSVersion"
$registryKey = "OSVersionName"
$registryKey = "Task Sequence Name"
$registryKey = "TaskSequenceID"
$registryValues = Get-ItemProperty -Path $registryPath
# Iterate through registry values and create instances of the custom class
foreach ($property in $registryValues.PSObject.Properties) {
# Skip non-value properties
if ($property.Name -eq "PSPath" -or $property.Name -eq "PSParentPath" -or $property.Name -eq "PSChildName" -or $property.Name -eq "PSDrive" -or $property.Name -eq "PSProvider") {
continue
}
# Create an instance of the custom WMI class
$instance = [PSCustomObject]@{
RegistryKeyName = $registryKey
RegistryValueName = $property.Name
RegistryValueData = $property.Value
}
# Convert to CimInstance
$cimInstance = New-CimInstance -Namespace $namespace -ClassName $className -Property $instance
# Save the instance to the WMI repository
$cimInstance | New-CimInstance -Namespace $namespace
New contributor
user25512276 is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.