piercj2
Enthusiast
Enthusiast

My first attempt was failing because i am using VM Attributes that don't exist for every VM inside of a TRY/CATCH Statement.

My issue, i need to check for these Attributes on all VM's.

QUESTION 1: How can i do it inside a TRY/CATCH without breaking the script ? (below is my attempt at a solution but still breaks the script)

QUESTION 2: why is $script:vm_Found_YES += New-Object -TypeName psobject -Property $Script:vmProperty not adding to the $script:vm_Found_YES

function Get-vraDetails {

    if ($script:target.customFields.Item("vrmManagedMachine")) {

        $script:vmProperty.Add("Attribute: vRA Managed","True")

        $script:vmProperty.Add("Attribute: vRA Owner", $script:target.customFields.Item("VRM Owner"))

    }

    else {

        $script:vmProperty.Add("Attribute: vRA Managed","False")

        $script:vmProperty.Add("Attribute: vRA Owner", "")

    }

    $script:vm_Found_YES += New-Object -TypeName psobject -Property $Script:vmProperty

}

function Main {

    $script:vm_Found_YES =@()

    $vm_Found_NO = @()

    #$isVRAmanaged = @()

    $targetList = get-content $Script:file

    $count = $targetList | Measure-Object -Line

    $i = 1

    $dir = $Script:directory

    $vm_Found_YES_ReportPath = $Script:directory + "\" + $script:appName + "-FoundReport-" + (Get-Date -Format yyyy-MMM-dd-HHmm) + ".xlsx"

    $vm_Found_NO_ReportPath = $Script:directory + "\" + $script:appName + "-NotFoundReport-" + (Get-Date -Format yyyy-MMM-dd-HHmm) + ".xlsx"

    #$isVRAmanaged_ReportPath = $Script:directory + "\" + $script:appName + "-isVRAManaged-" + (Get-Date -Format yyyy-MMM-dd-HHmm) + ".xlsx"

   

    foreach ($line in $targetList){

        Write-Progress -Activity "Collecting details on provided VMs" -Status "Working on $line" -PercentComplete (($i*100)/$count.Lines)

        try {

            write-host "inside TRY, working on $line"

            $script:target = Get-VM $line -ErrorAction SilentlyContinue

            $powerState = $script:target.PowerState

            $ip = $script:target.guest.IPAddress[0]

            $memory = $script:target.memoryGB

            $hddSize = [math]::Round(((Get-HardDisk -VM $script:target).CapacityGB | Measure-Object -Sum).Sum)

            #$vraManaged = $script:target.customFields.Item("vrmManagedMachine")

            #$vraOwner = $script:target.customFields.Item("VRM Owner")

            $script:vmProperty = [ordered] @{

                'vCenter' = $script:target.Uid.Split('@')[1].Split(':')[0]

                'Cluster' = $script:target.VMHost.Parent.Name

                'VM Name' = $script:target.Name

                'IP Address' = $ip

                'PowerState' = $powerState

                'Memory (GB)' = $memory

                'Disk Capacity (GB)' = $hddSize

                #'Attribute: vRA Managed' = if ($vraManaged) {"True"} else {"False"}

                #'Attribute: vRA Owner' = $vraOwner

            }

            Get-vraDetails

            $script:vm_Found_YES += New-Object -TypeName psobject -Property $Script:vmProperty

        }

        catch {

            Write-Host "inside CATCH working on $line"

            $script:notFound = [ordered] @{

                'VM Name' = $line

               'VM Exists' = "NO"

            }

            Write-Host -ForegroundColor Red "$line does not exist on the vCenter being searched"

            $vm_Found_NO += New-Object -TypeName psobject -Property $script:notFound

        }

        $i++

    }

   

    $script:vm_Found_YES | Sort-Object -Property 'VM Name' | Export-Excel -Path $vm_Found_YES_ReportPath -AutoFilter -AutoSize -TableStyle Light2 -Show

   

    if ($vm_Found_NO){

        $vm_Found_NO | Export-Excel -Path $vm_Found_NO_ReportPath -AutoFilter -AutoSize -TableStyle Light3 -Show

    }

}   

Reply
0 Kudos