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.
You could do something like this
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
You could do something like this
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
Thanks Sir.