WMI Output

Tagged: , ,

This topic contains 5 replies, has 2 voices, and was last updated by Profile photo of Adarsh Adarsh 2 days, 20 hours ago.

  • Author
    Posts
  • #66783
    Profile photo of Adarsh
    Adarsh
    Participant

    Hi Guys,

    I'm using below code to validate xml configuration with WMI output.

    https://gist.github.com/anonymous/7f371b0e171ce4452aa55249bd0b5d9c#file-config-xml

    try {$hbas = gwmi -Namespace 'root\wmi' -Class MSFC_FibrePortHBAAttributes -ErrorAction Stop }
    catch {'Error'; exit}
    
    foreach (Configuration in OS.Configuration) { 
    foreach ($hba in $hbas) { 
    $myHBA = $configuration.hba | where {hba.manufacturer -match $_.Manufacturer} |where {hba.model -match $_.model} | where {hba.Driverversion -match $_.Driverversion} | | where {hba.Firmwareversion -match $_.Firmwareversion}
    if ($myHBA) { break }
    }
    if ($myHBA) { break }
    }
    
    # To display error message
    if (!$myHBA) {
    'Critical error : combination does not match any configuration'
    }
    
    # To display details to users
    $hbaAttributes = gwmi -Namespace 'root\wmi' -Class MSFC_FibrePortHBAAttributes
    foreach ($hba in $hbas) {
    $pw = ($hbaAttributes | where {$_.InstanceName -eq $hba.InstanceName }).Attributes.PortWWN | hex: | 
    $hba | Add-Member -MemberType NoteProperty -Name PW -Value $pw | out-null
    }
    
    $hbas | select Manufacturer, Model, Driverversion, Firmwareversion, PW | ft -Autosize 
    
    

    I want to show details of every components from Manufacturer, Model, Driverversion and Firmwareversion that fails to match with configuration such as driver version "10.40.201.19" does not match or Firmware "11.40.201.15" not matches for every PW entry, instead of generic message "Critical error : combination does not match any configuration"

    Any suggestion ?

  • #66822
    Profile photo of Graham Beer
    Graham Beer
    Participant

    Hi,

    Probably the best way is using a if statement in a foreach loop.

    I've created a simple example in checking PC model, just for ideas. You could create custom objects like this :

    $computers = 'localhost', 'pc1', 'pc2'
    
    foreach ($computer in $Computers) {
    
        if (($model = (gwmi -Namespace 'root\CIMV2' -Class win32_ComputerSystem -ComputerName $Computer).Model) -eq 'HP Z400 Workstation' ) {
        
            New-Object -TypeName PSObject -Property @{
                Computer = $computer
                Model = $model
                State = "Model Matches"
            }
        }
        else { 
    
            New-Object -TypeName PSObject -Property @{
                Computer = $computer
                Model = $model
                State = "Incorrect Model!"
            }
        }
    }
    
    # output
    Model                   Computer  State
    -----                   --------  -----
    HP Z400 Workstation     localhost Model Matches
    HP Z400 Workstation     pc1       Model Matches
    HP EliteDesk 800 G1 SFF pc2       Incorrect Model!
    

    Or with a statement :

    $computers = 'localhost', 'pc1', 'pc2'
    
    foreach ($computer in $Computers) {
    
        if (($model = (gwmi -Namespace 'root\CIMV2' -Class win32_ComputerSystem -ComputerName $Computer).Model) -eq 'HP Z400 Workstation' ) {  
            "$computer is the correct model of : [ $model ]"    
        }
        else { 
            "$computer is the wrong model of : [ $model ]"
        }
    }
    
    # output
    localhost is the correct model of : [ HP Z400 Workstation ]
    pc1 is the correct model of : [ HP Z400 Workstation ]
    pc2 is the wrong model of : [ HP EliteDesk 800 G1 SFF ]
    
  • #66915
    Profile photo of Adarsh
    Adarsh
    Participant

    Thanks Graham.

    Using multiple if blocks is causing issue with Output formatting. Could you please help.

    foreach ($hba in $hbas) {
    
        if (($manufacturer_wmi = (gwmi -Namespace 'root\wmi' -Class MSFC_FibrePortHBAAttributes).Manufacturer) -in "$conf_Manufacturer" ) {
        
            New-Object -TypeName PSObject -Property @{
                Manufacturer = $manufacturer_wmi
                State = "Passed"
            }
        } 
        else { 
    
            New-Object -TypeName PSObject -Property @{
                Manufacturer = $manufacturer_wmi
                State = "Failed"
            }
        }
    	
    	if (($Driver_wmi = (gwmi -Namespace 'root\wmi' -Class MSFC_FibrePortHBAAttributes).Model) -in "$conf_Model" ) {
        
            New-Object -TypeName PSObject -Property @{
                Driver = $Driver_wmi
                DriverState = "Passed"
            }
        }
        else { 
    
            New-Object -TypeName PSObject -Property @{
                Driver = $Driver_wmi
                DriverState = "Failed"
            }
        }
    
    }
    
    Output
    
    Driverstate Manufacturer Driver          State
    ---------------------------------------------
    Passed      {Emulex}     {11.0.247.8000} Passed
    Failed                   {11.0.247.8000} 
    Passed      {Emulex}     {11.0.247.8000} Passed
    Failed                   {11.0.247.8000} 
    Passed      {Emulex}     {11.0.247.8000} Passed
    Failed                   {11.0.247.8000} 
    Passed      {Emulex}     {11.0.247.8000} Passed
    Failed                   {11.0.247.8000}
    
    

    I'm trying to fetch output in below format

    Manufacturer State
    ———————
    Emulex Passed
    Emulex Passed
    Emulex Passed
    Emulex Passed

    DriverVersion State
    ———————
    {11.0.247.8000} Failed
    {11.0.247.8000} Failed
    {11.0.247.8000} Failed
    {11.0.247.8000} Failed

  • #66928
    Profile photo of Adarsh
    Adarsh
    Participant

    Thanks Graham for your help. Found the way what I was looking for.

  • #66958
    Profile photo of Graham Beer
    Graham Beer
    Participant

    Hi Adarsh,

    Glad to hear you got it sorted. What was your code in the end ? Was going to say that instead of multiple if/else statement look at the switch statement.
    Here is a simple example:

    switch (3)
    {
        1 {"It is one."}
        2 {"It is two."}
        3 {"It is three."}
        4 {"It is four."}
    }
    It is three.
    
  • #67098
    Profile photo of Adarsh
    Adarsh
    Participant

    Sure Graham, will give code a try using Switch statement. Thanks for suggestion.

You must be logged in to reply to this topic.