3 Replies Latest reply on Sep 16, 2019 5:27 PM by BrianDGeorge

    PowerCLI Report Filtering by Annotation

    BrianDGeorge Enthusiast

      I have an inventory report of VMs that I was requested to create a weekly report by the backup team to ensure that all VMs are configured for backups since sometimes the systems team doesn't create tasks for the backup team in the tickets for servers.  The script works great except that I am sending them all the VMs and want to filter out the servers that are already configured.  Network does a good job of creating an annotation like the following: "Backup Server=zzz-nwkr1, Policy=PROD VM, Workflow=PROD01, Action=Backup, JobId=19102, StartTime=2019-09-15T20:30:23Z, EndTime=2019-09-15T20:32:24Z. The filter object is "Last EMC vProxy Backup".  Below is my script.  If you have any suggestions I would greatly appreciate it.

      function  Get-VMinventory {

       

      #Write-Host $vm.Name

       

          $vms = get-vm  | Where-Object {$_.powerstate -eq 'PoweredOn'}

          $vmevts = @()

          $vmevt = new-object PSObject

         

          foreach ($vm in $vms) {

            #Progress bar:

            $foundString = "       Found: "+$vmevt.name

            $searchString = "Searching: "+$vm.name

            $percentComplete = $vmevts.count / $vms.count * 100

            write-progress -activity $foundString -status $searchString -percentcomplete $percentComplete

           

      #All global info here     

      $Annotation = Get-Annotation -Entity $vm     

      $Networker = $Annotation | Where-Object {$_.Name -eq "Last EMC vProxy Backup"} | Select-Object -ExpandProperty value

      $cluster = $vm | Get-Cluster | Select-Object -ExpandProperty name

      $datacenter = $vm | Get-Datacenter | Select-Object -ExpandProperty name

      $vCenter = $vm.ExtensionData.Client.ServiceUrl.Split('/')[2].trimend(":443")

       

       

      #foreach($vm in Get-View -ViewType Virtualmachine -Filter @{'Runtime.PowerState'='poweredOn' -and {$_.Key -eq 'Last EMC vProxy Backup'}}) {

          $Vmresult = New-Object PSObject  

          $Vmresult | add-member -MemberType NoteProperty -Name "VMName" -Value $vm.Name

          $Vmresult | add-member -MemberType NoteProperty -Name "vCenter Server" -Value $vCenter

          $Vmresult | add-member -MemberType NoteProperty -Name "datacenter" -Value $datacenter 

          $Vmresult | add-member -MemberType NoteProperty -Name "Cluster" -Value $cluster

          $Vmresult | add-member -MemberType NoteProperty -Name "Backup Policy" -Value $Networker

          $Vmresult

      }

      }

      Get-VMinventory | Sort VMname | Export-CSV -Path "$FileCSV" -NoTypeInformation

        • 1. Re: PowerCLI Report Filtering by Annotation
          Zsoldier Expert
          vExpert

          Add Parameters and Filters.

           

          function  Get-VMinventory {

          param (

          [Parameter(Mandatory=$false)]

          [switch]

          $notbackedup=$false

          )

          #Write-Host $vm.Name

           

              $vms = get-vm  | Where-Object {$_.powerstate -eq 'PoweredOn'}

              $vmevts = @()

              $vmevt = new-object PSObject

           

              foreach ($vm in $vms) {

                #Progress bar:

                $foundString = "       Found: "+$vmevt.name

                $searchString = "Searching: "+$vm.name

                $percentComplete = $vmevts.count / $vms.count * 100

                write-progress -activity $foundString -status $searchString -percentcomplete $percentComplete

            

          #All global info here  

          $Annotation = Get-Annotation -Entity $vm  

          $Networker = $Annotation | Where-Object {$_.Name -eq "Last EMC vProxy Backup"} | Select-Object -ExpandProperty value

          If (!$Networker){$Networker = $null}

          $cluster = $vm | Get-Cluster | Select-Object -ExpandProperty name

          $datacenter = $vm | Get-Datacenter | Select-Object -ExpandProperty name

          $vCenter = $vm.ExtensionData.Client.ServiceUrl.Split('/')[2].trimend(":443")

           

           

          #foreach($vm in Get-View -ViewType Virtualmachine -Filter @{'Runtime.PowerState'='poweredOn' -and {$_.Key -eq 'Last EMC vProxy Backup'}}) {

              $Vmresult = New-Object PSObject

              $Vmresult | add-member -MemberType NoteProperty -Name "VMName" -Value $vm.Name

              $Vmresult | add-member -MemberType NoteProperty -Name "vCenter Server" -Value $vCenter

              $Vmresult | add-member -MemberType NoteProperty -Name "datacenter" -Value $datacenter

              $Vmresult | add-member -MemberType NoteProperty -Name "Cluster" -Value $cluster

              $Vmresult | add-member -MemberType NoteProperty -Name "Backup Policy" -Value $Networker

          Switch ($notbackedup)

          {

          $false { 

          $Vmresult

          }

          $true {

          $VMResult | Where-Object {$_."Backup Policy" -match $null}

          }

          }

          }

          }

          Get-VMinventory | Sort VMname | Export-CSV -Path "$FileCSV" -NoTypeInformation

          Get-VMInventory -notbackedup | Sort VMName | Export-CSV -Path "$FileCSV" -NoTypeInformation

          • 2. Re: PowerCLI Report Filtering by Annotation
            Zsoldier Expert
            vExpert

            Edited.  I made some syntax errors in my last reply and corrected.

            • 3. Re: PowerCLI Report Filtering by Annotation
              BrianDGeorge Enthusiast

              Still returning all results like Backup Server=zzz-nwkr1, Policy=PROD VM, Workflow=PROD01, Action=Backup, JobId=20983, StartTime=2019-09-16T20:30:22Z, EndTime=2019-09-16T20:32:22Z and not just the blanks.