VMware Cloud Community
jamie20
Enthusiast
Enthusiast
Jump to solution

export-csv in invoke-vmscript

Hi folks,

Am trying a script which gets information about vmware applications installed from several  windows vms.

The script is working fine, But I dont know how to make the output in a csv.

$VM = (Get-Content d:\Reports\input.txt)
foreach($vm in Get-VM -Name $VM)
{
$ScriptText1 = @"
get-wmiobject -class win32_product -filter "vendor like 'VMware%'" | select __SERVER, Name, Version, InstallDate
"@
Invoke-VMScript -VM $VM -ScriptText $ScriptText1 -GuestUser 'administrator' -GuestPassword 'welcome'
}

 

Any help?

0 Kudos
1 Solution

Accepted Solutions
LucD
Leadership
Leadership
Jump to solution

I prefer using the transfer of the data in CSV format.

Something like this

$ScriptText1 = @"
Get-WmiObject -Class win32_product -Filter "vendor like 'VMware%'" | 
select __SERVER, Name, Version, InstallDate | ConvertTo-Csv
"@


Get-Content d:\Reports\input.txt -PipelineVariable vm |
ForEach-Object -Process {
    Invoke-VMScript -VM $vm -ScriptText $ScriptText1 -GuestUser 'administrator' -GuestPassword 'welcome' |
    Select -ExpandProperty ScriptOutput | 
    ConvertFrom-Csv
} | Export-Csv -Path .\report.csv -NoTypeInformation -UseCulture


Blog: lucd.info  Twitter: @LucD22  Co-author PowerCLI Reference

View solution in original post

8 Replies
LucD
Leadership
Leadership
Jump to solution

Try something like this.
Foreach does not place anything in the pipeline, while Foreach-Object does.

 

 

$ScriptText1 = @"
get-wmiobject -class win32_product -filter "vendor like 'VMware%'" | select __SERVER, Name, Version, InstallDate
"@

Get-Content d:\Reports\input.txt -PipelineVariable vm |
ForEach-Object -Process {
    Invoke-VMScript -VM $VM -ScriptText $ScriptText1 -GuestUser 'administrator' -GuestPassword 'welcome' |
    Select @{N='ScriptOutput';E={$_.ScriptOutput}}
} | Export-Csv -Path .\report.csv -NoTypeInformation -UseCulture

 

 

 


Blog: lucd.info  Twitter: @LucD22  Co-author PowerCLI Reference

0 Kudos
jamie20
Enthusiast
Enthusiast
Jump to solution

Hi LucD,

Got the output like below

Length
168
177
177
177
177
177
177
174
0 Kudos
LucD
Leadership
Leadership
Jump to solution

My bad, I corrected the code above.


Blog: lucd.info  Twitter: @LucD22  Co-author PowerCLI Reference

0 Kudos
jamie20
Enthusiast
Enthusiast
Jump to solution

Hi LucD,

It showing error, So I removed pipeline from this line

Select @{N='ScriptOutput';E={$_.ScriptOutput}} |

Now am getting the output, but I need to get each value in a separate cell

Output:

ScriptOutput


__SERVER    Name                               Version         InstallDate

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

VM1 VMware Tools                       11.2.5.17337674 20210418  

VM1 VMware Dynamic Environment Manager 9.11.0.932      20210418  

VM1 VMware Horizon Agent               7.13.0          20210418  







__SERVER       Name                               Version         InstallDate

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

VM2 VMware Tools                       11.2.5.17337674 20210418  

VM2 VMware Dynamic Environment Manager 9.11.0.932      20210418  

VM2 VMware Horizon Agent               7.13.0          20210418  





0 Kudos
LucD
Leadership
Leadership
Jump to solution

There was a pipeline symbol at the end of the Select line, that shouldn't have been there.
I removed it


Blog: lucd.info  Twitter: @LucD22  Co-author PowerCLI Reference

jamie20
Enthusiast
Enthusiast
Jump to solution

Yes Lucd I corrected it.

But I need output like below format, Is it possible?

_ServerNameVersionInstall Date
vm1VMware Tools                      11.2.5.17337674 20210418
vm1VMware Horizon Agent7.13.020210418
vm2VMware Tools                      11.2.5.17337674 20210418

 

The above script is printing as it is in the powershell console.

0 Kudos
LucD
Leadership
Leadership
Jump to solution

I prefer using the transfer of the data in CSV format.

Something like this

$ScriptText1 = @"
Get-WmiObject -Class win32_product -Filter "vendor like 'VMware%'" | 
select __SERVER, Name, Version, InstallDate | ConvertTo-Csv
"@


Get-Content d:\Reports\input.txt -PipelineVariable vm |
ForEach-Object -Process {
    Invoke-VMScript -VM $vm -ScriptText $ScriptText1 -GuestUser 'administrator' -GuestPassword 'welcome' |
    Select -ExpandProperty ScriptOutput | 
    ConvertFrom-Csv
} | Export-Csv -Path .\report.csv -NoTypeInformation -UseCulture


Blog: lucd.info  Twitter: @LucD22  Co-author PowerCLI Reference

jamie20
Enthusiast
Enthusiast
Jump to solution

Thanks LucD...Thats it

0 Kudos