4 Replies Latest reply on Aug 23, 2019 5:21 AM by Vineeth

    modify output of Invoke-VMScript

    Vineeth Hot Shot

      Using this script I can get info of Hotfix installed status inside the GuestOS.

      I am using try/catch method because I have 4 set of passwords in that any one of the password is correct.

      Here is the output csv contain VMName ,OS ,IP ,RP ,Out1 ,Out2 ,Out3 ,Out4.

      The problem here is while sending the csv file to the customers again i need to consolidate the results like this VMName ,OS ,IP ,RP ,Results. So is there any way after getting the output in $obj variable can we consolidate in the same script by removing the 'invalidlogin' or 'any other output'  and get only results( Hotfix installstatus) in one row and generate new csv file which contains results as below.

      VMName ,OS ,IP ,RP ,Results.

       

      If it works with excel also I am ok. I have imported the excel module for powershell.

       

      Script I used

      $script = @'

       

      $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"

      }

      $hotfixinfo

      '@

      $obj = foreach ($vm in Get-ResourcePool 'testpool'|Get-VM) { 

         $out1 = ""

         $out2 = ""

         $out3 = ""

         $out4 = ""

         $found = $false

         try

         $out1 = Invoke-VMScript -VM $vm.Name -GuestUser "administrator" -GuestPassword "" -ScriptText $script -ScriptType Powershell -ErrorAction Stop | Select-Object -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-Object -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-Object -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-Object -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

         Out1 = $out1

         Out2 = $out2

         Out3 = $out3

         Out4 = $out4

        })

      }

      $obj | Export-Csv -Path C:\Users\Administrator\Desktop\WCstatusonser29_latest-off.csv -NoTypeInformation -NoClobber 

       

       

      Sample output:

       

      --------------

       

      Expected after consolidating;

       

      Regards
      Vineeth.K
        • 1. Re: modify output of Invoke-VMScript
          LucD Guru
          vExpertCommunity WarriorsUser Moderators

          Try like this.
          Not needing the results for each user option, also allows simplifying the script.

          All user combinations are now stored in an array, over which the script loops.

           

          $script = @'

          $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"

          }

          $hotfixinfo

          '@

          $accounts = @(

             @{

             User = 'administrator'

             Pswd = 'pswd1'

             },

             @{

             User = 'administrator'

             Pswd = 'pswd2'

             },

             @{

             User = 'administrator'

             Pswd = 'pswd3'

             },

             @{

             User = 'admin'

             Pswd = 'pswd4'

             }

          )


          Get-ResourcePool -Name 'testpool' -PipelineVariable rp |

          ForEach-Object -Process {

             Get-VM -Location $rp -PipelineVariable vm |

             ForEach-Object -Process {

             $out = 'Login failed'

             foreach ($user in $accounts)

             {

             try

             {

             $sInvoke = @{

             VM = $vm

             GuestUser = $user.User

             GuestPassword = $user.Pswd

             ScriptText = $script

             ScriptType = 'Powershell'

             ErrorAction = 'Stop'

             }

             $out = Invoke-VMScript @sInvoke | Select-Object -ExpandProperty ScriptOutput

             break

             }

             catch

             {

             }

             }

             New-Object PSObject -Property (

             [ordered]@{

             Name = $vm.Name

             OS = $vm.Guest.OSFullName

             IP = $vm.Guest.IPAddress -join '|'

             RP = $rp.Name

             Result = $out

             })

             }

          } |

          Export-Csv -Path C:\Users\Administrator\Desktop\WCstatusonser29_latest-off.csv -NoTypeInformation -NoClobber

          • 2. Re: modify output of Invoke-VMScript
            Vineeth Hot Shot

            I tried to execute the script on one resource pool but its giving below error.

             

            Regards
            Vineeth.K
            • 3. Re: modify output of Invoke-VMScript
              LucD Guru
              User ModeratorsvExpertCommunity Warriors

              Must have been a copy/paste issue, the code block for the ForEach-Object didn't start on the same line.

              I corrected above.

              • 4. Re: modify output of Invoke-VMScript
                Vineeth Hot Shot

                Super Sir. Its worked as expected.

                Regards
                Vineeth.K