Subscribe
Accepted Solution

question about filtering results

I am very new to Powershell, and coding as a whole, and have really only begun to realize what it can do for me. I am new to the company I'm working for as well, and one thing they are lacking is Operations Manager, or any real reporting tool. I've begun to try to use PS for this. I've been successful at some scripts but one thing I wanted to do was to create a list of disks, which aggr they are assigned to, as well as the raid state. I've entered this command get-nadisk | ft disktype,aggregate,raidstate -auto to get this output (just a sample):

DiskType Aggregate   RaidState
--------       ---------          ---------
FCAL                      spare
ATA                        partner
ATA                        partner
ATA                        partner
ATA        sata0        present

What I want to be able to do is write a script that will omit the "partner" disks and only show the present and spare disks. I was thinking that a "where" statement might work, but then I'm thinking that will only pull one of the 3 raid states. Then I was thinking I might want to further sort FC and SATA disks as well as present or partner and my head started to spin. How would I go about filtering down my results? If I do use a "where" statement, what property would I use to define the RaidState column?

Thanks,

Aaron

Re: question about filtering results

Get-NaDisk | ? {$_.RaidState -NE "partner"} | sort DiskType | ft DiskType,Aggregate,RaidState -AutoSize

Re: question about filtering results

Awesome, thanks! I think I'm going to read up on Powershell this weekend...

Re: question about filtering results

If you're looking to store the command in a script, I would recommend using a more formal command structure.

Get-NaDisk | ? {$_.RaidState -NE "partner"} | sort DiskType | ft DiskType,Aggregate,RaidState -AutoSize

Becomes...

Get-NaDisk | Where-Object {$_.RaidState -ne "partner"} | Sort-Object -Property DiskType | Format-Table -Property DiskType,Aggregate,RaidState -AutoSize

If you want to re-use any of the intermediate states, you can also use variables.  Storing intermediate values in variables can also be helpful in troubleshooting errors in your scripts.  Also, if you want to include comments, they are preceded with the # character.

#Get a list of all disks

$allDisks = Get-NaDisk

#Filter out partner disks

$nonpartnerDisks = $allDisks | Where-Object {$_.RaidState -ne "partner"}

#Sort the list of non-partner disks

$sortedNonpartnerDisks = $nonpartnerDisks = $allDisks | Sort-Object -Property DiskType

#Output the data is a formatted table showing the disk type, aggregate, and raidstate

$sortedNonpartnerDisks | Format-Table -Property DiskType,Aggregate,RaidState -AutoSize

Happy scripting!