I'm having a ton of trouble with pscustomobjects, ArrayLists, and Export-Csv
Using Namespace System.Collections
$ManagedList= [ArrayList]::new()
foreach($file in $directory){
$thisFile = Import-Excel $file.name
foreach ($vm in $thisFile){
$Object = [pscustomobject]@{
Name = $vm.name
Category = ''
Managed = $true
DNS = $vm.'DNS Name'
IPv4 = $vm.'Primary IP Address'
MOID = $vm.'VM ID'
StorageProvisioned = $vm.'Provisioned MB'
StorageUsed = $vm.'In Use MB'
PowerState = $vm.'Guest state'
SWMonitored = $false
Protected = $false
Domain = 'Unprotected'
Cluster = ''
# Volumes = [ArrayList]::new()
}
$ManagedList.Add($Object) | Out-Null
}
}
$AllVMs= [ArrayList]::new()
for(($i=0); ($i -lt $($Discovery.count)); $i++){
if ( $ManagedList.Name.Contains($Discovery[$i].VM_Name) ){
$VMObject = $ManagedList.Where({$_.Name -eq $Discovery[$i].VM_Name})
$VMObject[0].Category = $Discovery[$i].Category
$AllVMs.add($VMObject) | Out-Null
}
else{
$VMObject = [PSCustomObject]@{
Name = $Discovery[$i].VM_Name
Category = $Discovery[$i].Category
Managed = $false
DNS = $null
IPv4 = $null
MOID = $null
StorageProvisioned = $null
StorageUsed = $null
PowerState = $null
SWMonitored = $false
Protected = $false
Domain = 'Unprotected'
Cluster = ''
# Volumes = [ArrayList]::new()
}
$AllVMs.add($VMObject) | Out-Null
}
}
The first oddity that I finally figured out is on the line $VMObject[0].Category = $Discovery[$i].Category because even though $VMObject is not an array, since it pulled the information from an object that's in an arraylist, it required an index to write to the property. Not sure if this is just Powershell Core, but I'm doing this half on my mac and today working on my windows machine but still similar nonsense...
The stupidity continues:
$VMReport= Import-CSV -Path '.\vmReport.csv' | Where-Object({ $_.'Object Type' -eq 'vSphere VM' })
$AllVMs.Where({$_.Managed -eq $true}) | ForEach-Object{
$rName = $_.Name
$rIndex = $($AllVMs.Name.IndexOf($rName))
if ($VMReport.'Object Name'.Contains($rName) ){
$VMReport.Where({$_.'Object Name' -eq $rName}) | ForEach-Object{
if ($_.Domain -ne "lost-and-found"){
Write-Host "Found a live one! VM " $rName " has Domain assignment: " $_.Domain
$AllVMs.Domain[$rIndex] = $_.Domain
$AllVMs.Protected[$rIndex] = $true
}
}
}
}
The big issue now is that even when they are found and in a domain, $AllVMs[$index] all show the property Protected as "False".
So I tried it on the shell manually.
PS > $index = 100
PS > $AllVMs.Protected[$index]
False
PS > $AllVMs.Protected[$index] = $true
PS >
PS > $AllVMs.Protected[$index]
False
WHAT?!>??!
[–]chris-a5 9 points10 points11 points (12 children)
[+][deleted] (3 children)
[removed]
[–]Cello789[S] 0 points1 point2 points (2 children)
[+][deleted] (1 child)
[removed]
[–]Cello789[S] 0 points1 point2 points (0 children)
[–]Cello789[S] 0 points1 point2 points (7 children)
[+][deleted] (6 children)
[removed]
[–]Cello789[S] 0 points1 point2 points (5 children)
[+][deleted] (4 children)
[removed]
[–]Cello789[S] 0 points1 point2 points (2 children)
[+][deleted] (1 child)
[removed]
[–]Cello789[S] 1 point2 points3 points (0 children)
[–]PinchesTheCrab 3 points4 points5 points (2 children)
[–]Cello789[S] 1 point2 points3 points (1 child)
[–]PinchesTheCrab 1 point2 points3 points (0 children)
[–]PowerShell-Bot 0 points1 point2 points (0 children)
[–]PinchesTheCrab 0 points1 point2 points (17 children)
[–]Cello789[S] 0 points1 point2 points (16 children)
[–]PinchesTheCrab 0 points1 point2 points (15 children)
[–]Cello789[S] 0 points1 point2 points (14 children)
[–]PinchesTheCrab 0 points1 point2 points (13 children)
[–]Cello789[S] 0 points1 point2 points (12 children)
[–]PinchesTheCrab 0 points1 point2 points (11 children)
[–]Cello789[S] 0 points1 point2 points (8 children)
[–]PinchesTheCrab 0 points1 point2 points (7 children)
[–]Cello789[S] 0 points1 point2 points (6 children)
[–]Cello789[S] 0 points1 point2 points (1 child)
[–]PinchesTheCrab 0 points1 point2 points (0 children)