Looking for a script which provides exports the output in the following format (CSV), I did look for some sample script in the forum but the output is different to what I am currently looking
VM Attribute | VM Name |
VM’s with snapshot | Vm1, vm2 |
VM’s with tools outdate | Vm5,vm10 |
I have got few more properties to add on to the report, if I can get an example to start with I can give a try.
You can use the following example to create the desired report. The code is easily expandable with other properties.
$vm = Get-VM
# VM’s with snapshot
$vms = $vm |
Where-Object {$_ | Get-Snapshot} |
Select-Object -ExpandProperty Name
if ($vms) {$vms = [string]::Join(',', $vms)}
[pscustomobject]@{
"VM Attribute" = "VM’s with snapshot"
"VM Name" = $vms
}
# VM’s with tools outdate
$vms = $vm |
Where-Object {$_.ExtensionData.Summary.Guest.ToolsStatus -ne 'OK'} |
Select-Object -ExpandProperty Name
if ($vms) {$vms = [string]::Join(',', $vms)}
[pscustomobject]@{
"VM Attribute" = "VM’s with tools outdate"
"VM Name" = $vms
}
The given script is producing the output in the below format
name | value |
VM Attribute | VM's with snapshot |
VM Name | vm1, vm5,etc.. |
VM Attribute | VM's with tools outdate |
VM Name | vm2,vm3,etc.. |
But I am looking for output as below in a CSV file
VM Attribute | VM Name |
VM’s with snapshot | Vm1, vm2 |
VM’s with tools outdate | Vm5,vm10 |
This should work for you:
$AllReport = @()
Get-VM | foreach {
$vm = $_
$vmReport = "" | Select Name, @{ N="Tools Status"; E={ $vm.extensiondata.guest.toolsStatus -match "Old" } }, @{ N="Snapshot count"; E={ $vm | where { $vm | Get-Snapshot } } }
$vmReport.Name = $vm.name
$vmReport."Tools Status" = $vm.extensiondata.guest.toolsStatus
$vmReport."Snapshot count" = ($vm | Get-Snapshot).count
$AllReport += $vmReport
Write $vmReport
}
$AllReport | Export-Csv -NoTypeInformation "c:\Foldername\filename.csv" #CHANGE ME
Sorry, the code was cut off.
$AllReport = @()
Get-VM | foreach {
$vm = $_
$vmReport = "" | Select Name, @{ N="Tools Status"; E={ $vm.extensiondata.guest.toolsStatus -match "Old" } },
@{ N="Snapshot count"; E={ $vm | where { $vm | Get-Snapshot } } }
$vmReport.Name = $vm.name
$vmReport."Tools Status" = $vm.extensiondata.guest.toolsStatus
$vmReport."Snapshot count" = ($vm | Get-Snapshot).count
$AllReport += $vmReport
}
$AllReport | Export-Csv -NoTypeInformation "c:\Foldername\filename.csv" #CHANGE ME
I am using a similar script, but the output which I am looking is different to what which is being currently produced.. Check the previous post for the output I am currently looking..
Thanks!!
My previous script is using the [pscustomobject] typecast which is a PowerShell 3.0 feature. You are probably using PowerShell 2.0. I modified the script using the New-Object cmdlet, to make it PowerShell 2.0 compatible.
$vm = Get-VM
# VM’s with snapshot
$vms = $vm |
Where-Object {$_ | Get-Snapshot} |
Select-Object -ExpandProperty Name
if ($vms) {$vms = [string]::Join(',', $vms)}
New-Object -TypeName PSObject -Property @{
"VM Attribute" = "VM’s with snapshot"
"VM Name" = $vms
}
# VM’s with tools outdate
$vms = $vm |
Where-Object {$_.ExtensionData.Summary.Guest.ToolsStatus -ne 'OK'} |
Select-Object -ExpandProperty Name
if ($vms) {$vms = [string]::Join(',', $vms)}
New-Object -TypeName PSObject -Property @{
"VM Attribute" = "VM’s with tools outdate"
"VM Name" = $vms
}