2 Replies Latest reply on Jun 15, 2020 5:56 AM by vin01

    get vm name while using RunAsync switch

    vin01 Hot Shot

      I am using Invoke-VMScript with RunAsync switch to execute parallel and after script execution state is success it should write vmname and the result of invoke cmdlet.

      But in Invoke-VMScript with RunAsync if I list the process I don't fine the vmname so is it possible to get the vmname? Here is the sample code and output

      $script=@'
      Get-NetAdapter |select Name
      '@
      $vm=Get-VM 'TestVM01'Test02
      $output=@()
      $vm.Name |
      ForEach-Object -Process {
      $output+=Invoke-VMScript -VM $_ -GuestUser '' -GuestPassword '' -ScriptText $script -ScriptType Powershell -RunAsync -Confirm:$false
      }
      $output |fl
      

      Here if I do format-list how can i get the vmname. I can see the result of the execution but if I need to export to csv I need to know which result belongs to which VM.

       

      Regards
      Vineeth.K
        • 1. Re: get vm name while using RunAsync switch
          LucD Guru
          Community WarriorsvExpertUser Moderators

          You could do something like this

           

          $script = @' 

          Get-NetAdapter | Select -ExpandProperty Name

          '@

           

          $vm = Get-VM -Name 'TestVM01', 'Test02'


          $tasks = @()


          $vm.Name |

            ForEach-Object -Process {

              $tasks += Invoke-VMScript -VM $_ -GuestUser '' -GuestPassword '' -ScriptText $script -ScriptType Powershell -RunAsync -Confirm:$false

            }

          while ($tasks.State -contains 'running') {

            Start-Sleep 1

          }


          $report = foreach ($task in $tasks) {

            $task.Result.ScriptOutput.Split("`n") |

              Where-Object { $_ -ne '' } |

              Select-Object @{N = 'VM'; E = { $task.Result.VM.Name } },

                @{N = 'NIC'; E = { $_.Trim("`r`n") } }

          }

          $report | Export-Csv -Path .\report.csv -NoTypeInformation -UseCulture

          Blog: http://lucd.info | Twitter: @LucD22 | PowerCLI Reference co-author: http://tinyurl.com/hkn4glz
          • 2. Re: get vm name while using RunAsync switch
            vin01 Hot Shot

            Thanks Sir.

            Regards
            Vineeth.K