Forum Discussion
SCCMnube78
Jun 07, 2024Copper Contributor
Add Entries to WMI
Hello all, i am urgently looking for help from you guys. I have this script with entries that are being written to the registry. I am looking for a way to write the same entries into custom WMI class as well. Here is the script.
$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("OSDStartTime")
$AdvertisementID = $tsenv.Value("_SMSTSAdvertID")
$Organisation = $tsenv.value("_SMSTSOrgName")
$TaskSequenceID = $tsenv.value("_SMSTSPackageID")
$Packagename = $tsenv.value("_SMSTSPackageName")
$MachineName = $env:computername
$Installationmode = $tsenv.value("_SMSTSLaunchMode")
#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 "Installed Date" -Value $InstallTime -Type STRING -Force -ErrorAction SilentlyContinue | Out-Null
new-itemproperty $FullRegKeyName -Name "OSD Start Time" -Value $OSDStartTime -Type STRING -Force -ErrorAction SilentlyContinue | Out-Null
New-ItemProperty $FullRegKeyName -Name "OSD Duration" -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 "OS Version" -value (Get-CimInstance Win32_Operatingsystem).version -PropertyType String -Force | Out-Null
- sdtslmnBrass Contributor
hope the following additions can help you
$RegKeyName = "OSDBuild" $WMIClassName = "OSDBuildInfo" # Choose a suitable name for your WMI class # Set values $tsenv = New-Object -COMObject Microsoft.SMS.TSEnvironment $FullRegKeyName = "HKLM:\SOFTWARE\" + $regkeyname # ... (Your existing code for getting values remains the same) ... # Write values to WMI try { $wmiNamespace = Get-WmiObject -Namespace root\cimv2 -Class __Namespace if (-not ($wmiNamespace | Where-Object {$_.Name -eq $WMIClassName})) { # Create the custom WMI class if it doesn't exist $wmiClass = $wmiNamespace.CreateClass($WMIClassName, "Win32_OperatingSystem", $null) } else { $wmiClass = Get-WmiObject -Namespace root\cimv2 -Class $WMIClassName } $wmiInstance = $wmiClass.CreateInstance() # Set WMI instance properties (same as registry values) $wmiInstance.InstalledDate = $InstallTime $wmiInstance.OSDStartTime = $OSDStartTime $wmiInstance.OSDDuration = $OSDDuration $wmiInstance.OrganisationName = $Organisation $wmiInstance.AdvertisementID = $AdvertisementID $wmiInstance.TaskSequenceID = $TaskSequenceID $wmiInstance.TaskSequenceName = $Packagename $wmiInstance.InstallationType = $Installationmode $wmiInstance.Computername = $MachineName $wmiInstance.OSVersion = (Get-CimInstance Win32_Operatingsystem).version # Commit the changes to the WMI repository $wmiInstance.Put() } catch { Write-Error "Failed to write WMI data: $_" }