Quantcast
Channel: PowerShell General
Viewing all articles
Browse latest Browse all 10624

Custom Objects with Loops...

$
0
0

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
}


Viewing all articles
Browse latest Browse all 10624