Reputation: 11
I am still learning powershell and powercli, but wondering if anyone could help me with this.
I am trying to display (eventually export to csv) an inventory of ALL VMs with a column for Snapshot with value of "yes" if snapshot is present (not null).
I have the extra column created and output to csv:
Get-VM | select Name, PowerState, Snapshot | Export-Csv -Path "c:\VMs\vminventory.csv" -NoTypeInformation
And how to get a "yes" if Snapshot is not null:
$VMs = Get-VM
ForEach ($VM in $VMs) {
$VMSnapshot = Get-VM -name $VM | get-Snapshot
If ($VMSnapshot) {Write-Host "yes" }
}
Anyone have any ideas of how to combine these concepts to output something like this?
Upvotes: 1
Views: 234
Reputation: 11
Thank you!!!!!!!! Final code here:
$VMs = Get-VM
ForEach ($VM in $VMs) {
$VMSnapshot = $vm | Get-Snapshot
$row = $VM | select Name,Powerstate
If ($VMSnapshot) {
$row | Add-Member -MemberType NoteProperty -Name "Snapshot" -Value "Yes"
}
$row | Export-CSV -Path "c:\VMs\vminventory.csv" -Append -NoTypeInformation -Force
}
Upvotes: -1
Reputation: 72171
Probably something like this will work (I don't have PowerCli to test). What happens here is you add new property to the object and export the object to csv. you could probably construct the property on the fly.
I've also removed the extra call to get-vm, should save a lot of time.
$VMs = Get-VM
ForEach ($VM in $VMs) {
$VMSnapshot = $vm | Get-Snapshot
If ($VMSnapshot) {
$row = $VM | select Name,Powerstate
$row | Add-Member-MemberType NoteProperty -Name "Snapshot" -Value "Yes"
$row | Export-CSV -Path "c:\VMs\vminventory.csv" -Append -NoTypeInformation
}
}
if you need to export all vm's (not only those with snapshot), do this:
$VMs = Get-VM
ForEach ($VM in $VMs) {
$VMSnapshot = $vm | Get-Snapshot
$row = $VM | select Name,Powerstate
If ($VMSnapshot) {
$row | Add-Member-MemberType NoteProperty -Name "Snapshot" -Value "Yes"
}
else {
$row | Add-Member-MemberType NoteProperty -Name "Snapshot" -Value "No"
}
$row | Export-CSV -Path "c:\VMs\vminventory.csv" -Append -NoTypeInformation
}
Upvotes: 2