10 Replies Latest reply on May 17, 2018 6:52 AM by Satheeshkumar

    Improve the execution Speed while doing invoke-vm

    Vineeth Hot Shot

      I am trying to execute the below script on 180vms but its taking long time to get the output ( 180 vm nearly 14hrs) and some times powershell session is hanging.

      Is it possible to improve the execution time? Can some improvement done for the code in $script variable or anywhere in the script if possible?

      $script = @' 
      if([IntPtr]::Size -eq 8){
          $text = Get-ItemProperty -Path "HKLM:\SOFTWARE\WOW6432Node\Network Associates\ePolicy Orchestrator\Agent\" -ErrorAction SilentlyContinue |
              %{"$($_.AgentGUID)"}
      }
      elseif([IntPtr]::Size -eq 4){
          $text = Get-ItemProperty -Path "HKLM:\SOFTWARE\Network Associates\ePolicy Orchestrator\Agent\" -ErrorAction SilentlyContinue |
              %{"$($_.AgentGUID)"}
      }
      if(-not $text){
          $text = Get-Service -Name "McAfee Framework Service" -ErrorAction SilentlyContinue | %{"$($_.Name) is $($_.Status)"}
          if(-not $text){
              $text = Get-Service -Name wuauserv | Select -First 1 |
                  %{"$($_.Name) is $($_.Status)"}
          }
      }
      $cbout = Get-Service -Name "CarbonBlack" -ErrorAction SilentlyContinue | Select -first 1 | %{"$($_.Name) is $($_.Status)"} 
      if(-not $cbout){ 
          $cbout = Get-Service -Name wuauserv | Select -first 1 | %{"$($_.Name) is $($_.Status)"} 
      }
      $hotfix = "KB4012598", "KB4012598", "KB4012598", "KB4012598", "KB4012212", "KB4012215", "KB4012212", "KB4012215", "KB4012213", "KB4012216", "KB4012214", "KB4012217", "KB4012213", "KB4012216", "KB4012606", "KB4013198", "KB4013429", "KB4013429"
      $hotfixinfo = "No hotfix found!" 
      $fixes = Get-WmiObject -Class "win32_quickfixengineering" |
          where{$hotfix -contains $_.HotFixID} |
          Select -ExpandProperty HotFixID
      if($fixes){
          $hotfixinfo = "$($fixes -join ',') installed"
      } 
      $outputarray=@()
      $outputarray+=$hotfixinfo
      $outputarray+=$cbout 
      $outputarray+=$text
      $outputarray -join ';'
      '@
       $obj = foreach($vm in Get-ResourcePool "RP1" |Get-VM |  Where-Object {$_.PowerState -eq "Poweredon" -and $_.ExtensionData.Config.GuestFullName -like "*Microsoft*"}) {
        $out1 = ""
        $out2 = ""
        $out3 = ""
        $out4 = ""
        $found = $false 
        try{  
          $out1 = Invoke-VMScript -VM $vm.Name -GuestUser "administrator" -GuestPassword "" -ScriptText $script -ScriptType Powershell -ErrorAction Stop | Select -ExpandProperty ScriptOutput 
          $found = $true 
        }  
        catch [VMware.VimAutomation.ViCore.Types.V1.ErrorHandling.InvalidGuestLogin]{  
          $out1 = "Invalid logon"  
        }  
        catch{  
          $out1 = "any other output"  
        }  
        if(!$found) { 
          try{  
            $out2 = Invoke-VMScript -VM $vm.Name -GuestUser "administrator" -GuestPassword "" -ScriptText $script -ScriptType Powershell -ErrorAction Stop | Select -ExpandProperty ScriptOutput 
            $found = $true 
          }  
          catch [VMware.VimAutomation.ViCore.Types.V1.ErrorHandling.InvalidGuestLogin]{  
            $out2 = "Invalid logon"  
          }  
          catch{  
            $out2 = "any other output"  
          }  
        } 
        if(!$found){ 
          try{  
            $out3 = Invoke-VMScript -VM $vm.Name -GuestUser "administrator" -GuestPassword "" -ScriptText $script -ScriptType Powershell -ErrorAction Stop | Select -ExpandProperty ScriptOutput 
            $found = $true 
          }  
          catch [VMware.VimAutomation.ViCore.Types.V1.ErrorHandling.InvalidGuestLogin]{  
            $out3 = "Invalid logon"  
          }  
          catch{  
            $out3 = "any other output"  
          } 
        }
        if(!$found){ 
          try{  
            $out4 = Invoke-VMScript -VM $vm.Name -GuestUser "admin" -GuestPassword "" -ScriptText $script -ScriptType Powershell -ErrorAction Stop | Select -ExpandProperty ScriptOutput 
            $found = $true 
          }  
          catch [VMware.VimAutomation.ViCore.Types.V1.ErrorHandling.InvalidGuestLogin]{  
            $out4 = "Invalid logon"  
          }  
          catch{  
            $out4 = "any other output"  
          } 
        }
       New-Object PSObject -Property (
          [ordered]@{
              Name = $vm.Name
              OS   = $vm.Guest.OSFullName
              IP   = ($vm.Guest.IPAddress | Where {($_.Split(".")).length -eq 4}) -join (",")
              RP   = $vm.ResourcePool.Name
              Out1 = $out1 
              Out2 = $out2 
              Out3 = $out3 
              Out4 = $out4 
        })
      } 
      $obj 
      #|Export-Csv -Path C:\Users\admin\Documents\cbstatusonser29_latest.csv -NoTypeInformation -NoClobber 
      
      Regards
      Vineeth.K