Custom Objects with Loops...

rated by 0 users
Not Answered This post has 0 verified answers | 32 Replies | 4 Followers

Top 50 Contributor
113 Posts
Goldy posted on 01-24-2013 10:09 AM

Hey All - I'm not 100% sure i'm doing custom objects properly b/c im not getting consistent results

So i'm delving into the realm of custom objects and Im enjoying it, but i'm not 100% sure of the proper way to handle custom objects inside of multiple loops...

Basic idea is that I'm importing a list of filers and I want to report on shelf information, but im not getting consistent results...

And at the current time, i'm only interested in the last object in the array that's why you will see [-1]

Here's a snippit of the main_block of code.

I'm just not sure the proper way to build objects in the manner i am trying.. I commented out a few of the lines, but it wasn't working properly

Any insight would be greatly appreciated - thx

Function main_block {
## Clear screen
cls

## Starting Main
# Importing input file

$hosts = import-csv -path $inputfile



$array = @()

$hosts | % {
$filer = $_.filer

#$customobject = New-Object psobject
#Add-Member -inputobject $customobject -membertype Noteproperty -name Filer -value $filer

Write-Host "`nConnecting to filer:" $Filer
Try {
$c = connect-controller $filer -erroraction "silentlycontinue"
} catch {
[system.exception]

}
# Check if connecting to filer


if ($c -ne $null)
{
###
write-host "`n`t`t`tChecking Shelf Firmware on Filer:" $filer
## Gather Shelf information
$Shelf = Get-Shelf | ? {$_.type -like "*IO*"}
## Check for $Null Object
If ($Shelf -ne $Null) {
# Create custom object
## Loop through shelves
# foreach ($s in $Shelf) {
$Shelf[-1] | % {
$customobject = New-Object psobject
Add-Member -inputobject $customobject -membertype Noteproperty -name Filer -value $filer
Add-Member -inputobject $customobject -membertype Noteproperty -name "Connected to filer" -value "Yes"
Add-Member -inputobject $customobject -membertype Noteproperty -name "IM Shelves Present" -value "Yes"
Add-Member -inputobject $customobject -membertype Noteproperty -name Name -value $_.name
Add-Member -inputobject $customobject -membertype Noteproperty -name Status -value $_.status
Add-Member -inputobject $customobject -membertype Noteproperty -name "type" -value $_.type
Add-Member -inputobject $customobject -membertype Noteproperty -name Firmwarex -value $_.FirmwareRevx
Add-Member -inputobject $customobject -membertype Noteproperty -name Firmwarey -value $_.FirmwareRevy
$array += $customobject
}
} Else {
Write-Host "`n`t`t`tFiler:" $filer "Does not have any Shelves"
$customobject = New-Object psobject
Add-Member -inputobject $customobject -membertype Noteproperty -name Filer -value $filer
Add-Member -inputobject $customobject -membertype Noteproperty -name "Connected to filer" -value "Yes"
Add-Member -inputobject $customobject -membertype Noteproperty -name "IM Shelves Present" -value "NO"
$array += $customobject
}

} else {

Write-Host "`n`t`t`tCould not connect to filer:" $filer
$customobject = New-Object psobject
Add-Member -inputobject $customobject -membertype Noteproperty -name Filer -value $filer
Add-Member -inputobject $customobject -membertype Noteproperty -name "Connected to filer" -value "No"
$array += $customobject
}
#$array += $customobject

Clear-Variable c
clear-variable filer

}
cls

$array | Export-Csv c:\temp\shelfinformation.csv -NoTypeInformation
$array | ft -autosize

### Close Main Function
}

All Replies

Top 50 Contributor
113 Posts

Damn, I need a brain dump from you to me :-)

How on earth did you get so good at this..

Top 10 Contributor
2,170 Posts
Microsoft MVP
Top Contributor

Thanks, but if you should want someones braindump then go for Bruce Payette not me. I highly doubt he will agree,though since the procedure is probably lethal :D fortunately there is Powershell in Action book which is kinda the same thing :) 

but seriously: powershell in action is great, takes about a month to read. Powershell cookbook may follow as great reference book. Then some general programming book. Code complete. During all of this try to script at least one thing a day. This forum is great source of new topics to learn about. Give ir dome time, work and will. And I am sure I will be asking you the same question in no time :)

Top 50 Contributor
113 Posts

Yeah, i've been dabbling in it for about a year now.. But i'm solely focused on Netapp powershell.  That's where it's at!

 

 

Page 3 of 3 (33 items) < Previous 1 2 3 | RSS
Copyright 2012 PowerShell.com. All rights reserved.