Jump to content

Adding to a 2d Array


JLogan3o13
 Share

Recommended Posts

  • Moderators

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

 

"Profanity is the last vestige of the feeble mind. For the man who cannot express himself forcibly through intellect must do so through shock and awe" - Spencer W. Kimball

How to get your question answered on this forum!

Link to comment
Share on other sites

  • Moderators

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

 

"Profanity is the last vestige of the feeble mind. For the man who cannot express himself forcibly through intellect must do so through shock and awe" - Spencer W. Kimball

How to get your question answered on this forum!

Link to comment
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
 Share

  • Recently Browsing   0 members

    • No registered users viewing this page.
×
×
  • Create New...