VMware Cloud Community
vin01
Expert
Expert
Jump to solution

get vm name while using RunAsync switch

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.

pastedImage_2.png

Regards Vineeth.K
Reply
0 Kudos
1 Solution

Accepted Solutions
LucD
Leadership
Leadership
Jump to solution

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: lucd.info  Twitter: @LucD22  Co-author PowerCLI Reference

View solution in original post

Reply
0 Kudos
2 Replies
LucD
Leadership
Leadership
Jump to solution

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: lucd.info  Twitter: @LucD22  Co-author PowerCLI Reference

Reply
0 Kudos
vin01
Expert
Expert
Jump to solution

Thanks Sir.

Regards Vineeth.K
Reply
0 Kudos