Alex Young
asked on
Powershell Select Statement Query
Hi,
I have this line of code
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?
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
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?
Hi,
could you try to add a
[string]
in front?
Like
HTH
Rainer
could you try to add a
[string]
in front?
Like
OperationalStatus = [string]$Event.OperationalStatus
HTH
Rainer
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}
ASKER
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"}}
ASKER
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.
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.
ASKER
Ah okay thanks for that i've yet to test this yet but will let you guys know.
ASKER
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
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
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
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
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER
Brilliant works perfectly thank you so much.
Change Select-Object Caption,ElementName,Operat
to
Select-Object Caption,ElementName,@{N="O
And check the result..