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?
LVL 1
Alex YoungAsked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

SubsunCommented:
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..
0
Rainer JeschorCommented:
Hi,
could you try to add a
[string]
in front?

Like
OperationalStatus = [string]$Event.OperationalStatus

Open in new window


HTH
Rainer
0
Alex YoungAuthor Commented:
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.
0
INTRODUCING: WatchGuard's New MFA Solution

WatchGuard is proud to announce the launch of AuthPoint, a powerful, yet simple, Cloud-based MFA service designed to eliminate the vulnerabilities that put your data, systems, and users at risk.

SubsunCommented:
Try changing line 7 to..
OperationalStatus = If($Event.OperationalStatus -match "2"){"OK"}Else{$Event.OperationalStatus}

Open in new window

0
Alex YoungAuthor Commented:
Brilliant, if i want to add other events do i just do an -or and -and?
0
SubsunCommented:
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

0
Alex YoungAuthor Commented:
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.
0
SubsunCommented:
It depends on situation... If you talk about performance then i think switch does a better job than if-else..
0
QlemoBatchelor, Developer and EE Topic AdvisorCommented:
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.
0
Alex YoungAuthor Commented:
Ah okay thanks for that i've yet to test this yet but will let you guys know.
0
Alex YoungAuthor Commented:
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
0
SubsunCommented:
With current code $TemperatureReport += $row will run for each object in pipeline,which might be causing the issue.. try
$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
            }
        $TemperatureReport += $row
        }
        "Temperature Sensor 2" {
        $row = New-Object PSObject -Property @{
                Sensor = "CPU"
                Temperature = $sensor.CurrentReading
            }
        $TemperatureReport += $row
        }
    }
}

$TemperatureReport = $TemperatureReport | ConvertTo-Html -Fragment

Open in new window

0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
Alex YoungAuthor Commented:
Brilliant works perfectly thank you so much.
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Powershell

From novice to tech pro — start learning today.