Author Posts

March 20, 2017 at 3:14 pm

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 ?

March 21, 2017 at 9:42 am

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 ]

March 22, 2017 at 7:16 am

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

March 22, 2017 at 10:25 am

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

March 22, 2017 at 3:03 pm

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.

March 23, 2017 at 6:21 pm

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