I am trying to query properties of numerous vm's and have them placed into a csv. Thanks to RvdNieuwendijk I have a simple way to retrieve some of the properties as seen below.
$VMs = "vm1", "vm2", "vm3"
foreach ($i in $vms) {
Get-VM $i | `
Select-Object -Property Name,
@{N="DNS Name";E={$_.ExtensionData.Guest.HostName}},
PowerState,numCPU,MemoryMB,Version,
Id,ProvisionedSpaceGB,UsedSpaceGB,VMHost,
@{N="ToolsVersion";E={$_.ExtensionData.Config.Tools.ToolsVersion}},
@{N="ToolsStatus";E={$_.ExtensionData.Guest.ToolsStatus}} | `
Export-Csv -NoTypeInformation -UseCulture -Path E:\scripts\$i.csv
}
However when I use the export-csv cmdlet the file only contains an entry for the last vm. So my question is how can I export this to csv so that each vm is on its own line in the same file with no headers?
Hello, sas23-
You were quite close. The main thing was to move the Export-CSV call outside of the foreach loop like:
$VMs = "vm1", "vm2", "vm3"
&{foreach ($i in $VMs) {
Get-VM $i | Select-Object -Property Name,
@{N="DNS Name";E={$_.ExtensionData.Guest.HostName}},
PowerState,numCPU,MemoryMB,Version,
Id,ProvisionedSpaceGB,UsedSpaceGB,VMHost,
@{N="ToolsVersion";E={$_.ExtensionData.Config.Tools.ToolsVersion}},
@{N="ToolsStatus";E={$_.ExtensionData.Guest.ToolsStatus}}
}} | Export-Csv -NoTypeInformation -UseCulture -Path E:\scripts\VMsInfo.csv
or, to cut a couple of lines, you could pass the VM names directly to Get-VM, since the names are not used elsewhere in the script. That would look like:
(Get-VM "vm1", "vm2", "vm3" | Select-Object -Property Name,
@{N="DNS Name";E={$_.ExtensionData.Guest.HostName}},
PowerState,numCPU,MemoryMB,Version,
Id,ProvisionedSpaceGB,UsedSpaceGB,VMHost,
@{N="ToolsVersion";E={$_.ExtensionData.Config.Tools.ToolsVersion}},
@{N="ToolsStatus";E={$_.ExtensionData.Guest.ToolsStatus}}
) | Export-Csv -NoTypeInformation -UseCulture -Path E:\scripts\VMsInfo.csv
Both ways should result in a single CSV file with one row of header info and three (3) rows of VM info. You said, "no headers", but I took that to mean that you did not want a header row for each VM's info, not that you wanted no headers whatsoever.
Enjoy
I have not used the power shell that much. However if you do not pipe to export-cvs do you see all three servers data?
After running your script in on machine I do get the disred result that you are looking for.
$VMs = "v1", "v2", "v3"
foreach ($i in $vms) {
Get-VM $i | `
Select-Object -Property Name,
@{N="DNS Name";E={$_.ExtensionData.Guest.HostName}},
PowerState,numCPU,MemoryMB,Version,
Id,ProvisionedSpaceGB,UsedSpaceGB,VMHost,
@{N="ToolsVersion";E={$_.ExtensionData.Config.Tools.ToolsVersion}},
@{N="ToolsStatus";E={$_.ExtensionData.Guest.ToolsStatus}} | `
Export-Csv -NoTypeInformation -UseCulture -Path c:\$i.csv
}
This is what you ar looking for,
$myRow= @()
$VMs = "v1", "v2", "v3"foreach ($i in $vms) {
$vm=Get-VM $i
$info ="" | Select-Object VMName, PowerState,numCPU,MemoryMB,Version,ID,
ProvisionedSpaceGB,UsedSpaceGB
$info.VMName=$vm.Name
$info.PowerState=$vm.PowerState
$info.numCPU=$vm.numCPU
$info.MemoryMB=$vm.MemoryMB
$info.Version=$vm.Version
$info.ID=$vm.ID
$info.ProvisionedSpaceGB=$vm.ProvisionedSpaceGB
$info.UsedSpaceGB=$vm.UsedSpaceGB
$myRow +=$info
}$myRow | Export-Csv -NoTypeInformation -UseCulture -Path c:\vm.csv
Hello, sas23-
You were quite close. The main thing was to move the Export-CSV call outside of the foreach loop like:
$VMs = "vm1", "vm2", "vm3"
&{foreach ($i in $VMs) {
Get-VM $i | Select-Object -Property Name,
@{N="DNS Name";E={$_.ExtensionData.Guest.HostName}},
PowerState,numCPU,MemoryMB,Version,
Id,ProvisionedSpaceGB,UsedSpaceGB,VMHost,
@{N="ToolsVersion";E={$_.ExtensionData.Config.Tools.ToolsVersion}},
@{N="ToolsStatus";E={$_.ExtensionData.Guest.ToolsStatus}}
}} | Export-Csv -NoTypeInformation -UseCulture -Path E:\scripts\VMsInfo.csv
or, to cut a couple of lines, you could pass the VM names directly to Get-VM, since the names are not used elsewhere in the script. That would look like:
(Get-VM "vm1", "vm2", "vm3" | Select-Object -Property Name,
@{N="DNS Name";E={$_.ExtensionData.Guest.HostName}},
PowerState,numCPU,MemoryMB,Version,
Id,ProvisionedSpaceGB,UsedSpaceGB,VMHost,
@{N="ToolsVersion";E={$_.ExtensionData.Config.Tools.ToolsVersion}},
@{N="ToolsStatus";E={$_.ExtensionData.Guest.ToolsStatus}}
) | Export-Csv -NoTypeInformation -UseCulture -Path E:\scripts\VMsInfo.csv
Both ways should result in a single CSV file with one row of header info and three (3) rows of VM info. You said, "no headers", but I took that to mean that you did not want a header row for each VM's info, not that you wanted no headers whatsoever.
Enjoy