Sign in to follow this  
Followers 0
JLogan3o13

Adding to a 2d Array

2 posts in this topic

#1 ·  Posted

I am forcing myself to use PowerShell more often, as most of my customers are either utilizing PowerCLI for VMware or want some level of automation for SCCM (my SCCM UDF just isn't cutting it anymore). Currently I am working on a PowerCLI script that will cycle through a bunch of VMs pulling properties for connected CD-ROM, Floppies, etc.; this part is working just fine. 

I am trying to output to a 2d array with headers, which I am creating like this:

$aArray = @()
$sItem = New-Object PSObject
    $sItem | Add-Member -type NoteProperty -Name 'Connected CD-ROM' -Value ''
    $sItem | Add-Member -type NoteProperty -Name 'Start CD Connected' -Value ''
    $sItem | Add-Member -type NoteProperty -Name 'CD Client Device' -Value ''
    $sItem | Add-Member -type NoteProperty -Name 'CD DataStore ISO' -Value ''
    $sItem | Add-Member -type NoteProperty -Name 'Connected Floppy' -Value ''
    $sItem | Add-Member -type NoteProperty -Name 'Start Floppy Connected' -Value ''
    $sItem | Add-Member -type NoteProperty -Name 'Floppy Client Device' -Value ''

$aArray += $sItem

What I am looking for is the best way to add returned info to the proper index and column in the array. For example, if I find a VM that has a connected floppy, add it to the next index in column 4. I have tried something like this...

$aArray += , @("", "", "", "Test", "", "", "")

...but it doesn't work as I would expect. Owing to the fact that I am creating the array through the PSObject, I tried using Add-Member again, but I would think this would re-draw the entire array after every entry. Any suggestions would be appreciated; hope I am missing something simple stupid

 


√-1 2^3 ∑ π, and it was delicious!

Share this post


Link to post
Share on other sites



#2 ·  Posted

As was pointed out to me none too kindly by a colleague of mine, "learn the pipeline instead of trying to make PS do what you're used to" :) For anyone who might look in the future, I ended up with something like this instead:

cls

$sReport = Get-VM | Sort-Object | ForEach-Object {
  $cdDrive = $_ | Get-CDDrive
  $floppyDrive = $_ | Get-FloppyDrive
  [PSCustomObject] @{
    "VMName" = $_.Name
    "HasCD" = [bool]($cdDrive | Where-Object { $_.ConnectionState.Connected })
    "HasCDStartConnected" = [bool]($cdDrive | Where-Object { $_.ConnectionState.StartConnected })
    "HasCDConnectedToRemote" = [bool]($cdDrive | Where-Object { $_.RemoteDevice.Length })
    "HasISO" = [bool]($cdDrive | Where-Object { $_.ISOPath -like "*.iso" })
    "HasFloppy" = [bool]($floppyDrive | Where-Object { $_.ConnectionState.Connected })
    "HasFloppyStartConnected" = [bool]($floppyDrive | Where-Object { $_.ConnectionState.StartConnected })
    "HasFloppyConnectedToRemote" = [bool]($flopyDrive | Where-Object { $_.RemoteDevice.Length })
  }
}

$sReport | Out-GridView

 


√-1 2^3 ∑ π, and it was delicious!

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!


Register a new account

Sign in

Already have an account? Sign in here.


Sign In Now
Sign in to follow this  
Followers 0