Export to csv a Parallel invoke-AZVMRUNcommand

Viewed 55

I'm having trouble finding out how to export data from a invoke-AZVMRUNcommand in parallel to a csv file. Usually this is simple to do but this time it got me the csv is not showing any data.. Can someone share a little knowledge of what to do.

$myAzureVMs | ForEach-Object -Parallel {
$out = Invoke-AzVMRunCommand `
    -ResourceGroupName *********** `
    -Name $_.Name `
    -CommandId 'RunPowerShellScript' `
    -ScriptPath C:\Users\user******\Downloads\ListCertificates\ListCertificates.ps1
    #Formating the Output with the VM name
$output = $_.Name + " " + $out.Value[0].Message
$output} | Export-Csv -Path C:\CertList\CertEXPlist.csv
1 Answers

You can use the below PowerShell script to export into CSV file along with Invoke-AzVMRunCommand without using -Parallel as this works only with latest version of Powershell.

We've run a sample Powershell script by using the above cmdlet which in turn returns the result from the VM and saves in the csv file locally.

$out = Invoke-AzVMRunCommand `
    -ResourceGroupName RGName `
    -Name myVmrajkumarvm `
    -CommandId 'RunPowerShellScript' `
    -ScriptPath 'C:\Users\*****\xxxxxxxxxxxx\Documents\test.ps1'
    #Formating the Output with the VM name
$out | Export-Csv -Path C:\Users\*******\CertEXPlist.csv

After running the above ps script, the output will be stored in the directory which is given above:

Sample Output screen:

enter image description here

enter image description here

Related