Link to home
Start Free TrialLog in
Avatar of Alex Young
Alex YoungFlag for United Kingdom of Great Britain and Northern Ireland

asked on

Powershell Select Statement Query

Hi,

I have this line of code

$RAIDReport = @()
$RAIDEvent = gwmi -Namespace root\hpq -Class HPSA_StorageExtent | Select-Object Caption,ElementName,OperationalStatus
foreach ($event in $RAIDEvent) {
	$row = New-Object -Type PSObject -Property @{
		Caption = $Event.Caption
		ElementName = $Event.ElementName
		OperationalStatus = $Event.OperationalStatus
	}
	$RAIDReport += $row
}

$RAIDReport = $RAIDReport | ConvertTo-html -Fragment

Open in new window


Now when i call the $RaidReport it works but this is the example

OperationalStatus      Caption      ElementName
System.UInt16[]      Port:1I Box:1 Bay:1      Port:1I Box:1 Bay:1
System.UInt16[]      Port:2I Box:1 Bay:2      Port:2I Box:1 Bay:2


How would i combat the Operationstatus reporting correctly it's basically supposed to report back a '2' but i would like to change it so IF it's a '2' then change then put in some text to say OK.

Does that make sense?
Avatar of SubSun
SubSun
Flag of India image

System.UInt16[] seems to be a multivalued attribute..

Change Select-Object Caption,ElementName,OperationalStatus

to

Select-Object Caption,ElementName,@{N="OperationalStatus";E={$_.OperationalStatus}}


And check the result..
Hi,
could you try to add a
[string]
in front?

Like
OperationalStatus = [string]$Event.OperationalStatus

Open in new window


HTH
Rainer
Avatar of Alex Young

ASKER

subsun yours has worked perfectly thank you - how would i base it on the result so it's coming back with a 2 to report the drive is all okay so i would like to report saying OK.
Try changing line 7 to..
OperationalStatus = If($Event.OperationalStatus -match "2"){"OK"}Else{$Event.OperationalStatus}

Open in new window

Brilliant, if i want to add other events do i just do an -or and -and?
You can use if elseif else or Switch..

OperationalStatus = If($Event.OperationalStatus -match "2"){"OK"}Elseif($Event.OperationalStatus -match "3"){"NotOK"}Else{$Event.OperationalStatus}

or

OperationalStatus = Switch ($Event.OperationalStatus){"2"{"OK"};"3"{"NotOK"};"4"{"Warning"}}

Open in new window

okay so switch appears to be a little bit more easier to read. Are there any benefits to using either of them? just out of curiosity.
It depends on situation... If you talk about performance then i think switch does a better job than if-else..
Wrong, unless you use break in the scriptblocks of switch ;-). Without, all conditions are checked each time, no matter whether one was hit already. The if only evaluates until one condition is met.
It doesn't matter here, but with more complex checks it might well make a difference, both for performance and result, e.g. if the conditions aren't mutual exclusive.
Ah okay thanks for that i've yet to test this yet but will let you guys know.
Yep works great thank you - don't suppose you guys have dealt with the HP WPEM queries, just finished the RAID one but getting odd results with the temperature sensors reporting.

the command is similar to the one above

$TemperatureReport = @()
$TemperatureEvent = gwmi -Namespace root\hpq -Class HP_NumericSensor | ForEach {
    $sensor = $_
    Switch ($_.Name) {
        "Temperature Sensor 1" {
       $row = New-Object PSObject -Property @{
                Sensor = "Ambient"
                Temperature = $sensor.CurrentReading
            }
        }
        "Temperature Sensor 2" {
        $row = New-Object PSObject -Property @{
                Sensor = "CPU"
                Temperature = $sensor.CurrentReading
            }        
        }
    }
	$TemperatureReport += $row
	
}

$TemperatureReport = $TemperatureReport | ConvertTo-Html -Fragment

Open in new window


It's coming up with repeating results which might be normal but when i run it manually i only get it 2 results put it in a html and i get a whole load of repeating cpu temps

Sensor      Temperature
Ambient      23
CPU      40
CPU      40
CPU      40
CPU      40
CPU      40
CPU      40
CPU      40
CPU      40
CPU      40
CPU      40
CPU      40
CPU      40
ASKER CERTIFIED SOLUTION
Avatar of SubSun
SubSun
Flag of India image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Brilliant works perfectly thank you so much.