WMI and Clean Code

Hi Guys,

I’m looking at the best way to display WMI information which will be in a HTML report but which In I have the minimal code required. See Below what I have. I’m pulling data from 4 WMI objects. Thanks

Write-Output "*Collecting System Information"

try
{

#Get WMI Objects using CIM

$cs = Get-CimInstance -ClassName win32_computersystem -ComputerName $ComputerName -ErrorAction Stop
$os = Get-CimInstance -ClassName Win32_operatingsystem -ComputerName $ComputerName -ErrorAction Stop
$bs = Get-CimInstance -ClassName win32_bios -ComputerName $ComputerName -ErrorAction Stop
$power = Get-CimInstance -Namespace root\cimv2\power -Class win32_PowerPlan | Where-Object {$_.IsActive -eq "True"}|Select ElementName -ErrorAction Stop
$powerplan = if ($OSPower.ElementName -notlike "*Performance") {"$($OSPower.ElementName)(Recommended Plan: High Performance)"}
else {$OSPower.ElementName}

$csinfo = @()
$csinfoObj = New-Object PSObject

$csinfoObj | Add-Member -Name 'Name' -MemberType NoteProperty -Value $cs.Name
$csinfoObj | Add-Member -Name 'System Uptime' -MemberType NoteProperty -Value $cs.AdminPasswordStatus
$csinfoObj | Add-Member -Name 'Serial Number' -MemberType NoteProperty -Value $bs.SerialNumber
$csinfoObj | Add-Member -Name 'Operating System' -MemberType NoteProperty -Value $os.Caption
$csinfoObj | Add-Member -Name 'Last Boot' -MemberType NoteProperty -Value $os.LastBootUpTime
$csinfoObj | Add-Member -Name 'Domain Connected' -MemberType NoteProperty -Value $cs.PartOfDomain
$csinfoObj | Add-Member -Name 'OS Architecture' -MemberType NoteProperty -Value $os.OSArchitecture
$csinfoObj | Add-Member -Name 'Power Plan' -MemberType NoteProperty -Value $powerplan

$csinfo += $csinfoObj

$SystemInfoHTML += $csinfo | ConvertTo-Html -Fragment -As List -PreContent "
<h2>System Information</h2>
" | ForEach {$_ -replace "

",""}<code></code>}

catch
{
Write-Warning $.Exception.Message
$htmlbody += "An error was encountered. $($
.Exception.Message)"
$htmlbody += $spacer
}

I couldn’t extract all of your code because of the bad formatting the forum created but since you’ve asked for minimal code I think there’s some room for improvement on part of your code. :wink:

$CimSession = New-CimSession -ComputerName $ComputerName

$cs = Get-CimInstance -ClassName win32_computersystem -CimSession $CimSession -ErrorAction Stop
$os = Get-CimInstance -ClassName Win32_operatingsystem -CimSession $CimSession -ErrorAction Stop
$bs = Get-CimInstance -ClassName win32_bios -CimSession $CimSession  -ErrorAction Stop
$powerplan = Get-CimInstance -Namespace root\cimv2\power -Class win32_PowerPlan -CimSession $CimSession  | Where-Object { $_.IsActive } | Select-Object -ExpandProperty ElementName -ErrorAction Stop

[PSCustomObject]@{
    Name            = $cs.Name
    System          = $cs.AdminPasswordStatus
    Serial          = $bs.SerialNumber
    OperatingSystem = $os.Caption
    LastBootUpTime  = $os.LastBootUpTime
    DomainConnected = $cs.PartOfDomain
    OSArchitecture  = $os.OSArchitecture
    PowerPlan       = $powerplan
}

Try if you can use some of it.

Also, look into ConvertTo-HTML. Take the resulting hash table from Olaf’s code and run it through ConvertTo-HTML and you will have saved a ton of work.