So I would like to see if I can get some advice on what im not doing right.
What I am trying to do is check to see if there is a snapshot for the vm's in $list (this part appears to work. on the IF portion,
but in the ELSE...
my problem is I do not know how to just plug in some html to display something like "NO SNAPSHOT FOUND"
I am using the convertto-html to create a report...
$myreport = @()
$snap = get-vm -name $list | Get-Snapshot
If ($snap)
{
$vmlist = Get-Content C:\test.txt
get-vm -name $vmlist | % {
$Row = "" | Select-Object VM, "VMWare Tools"
$Row.VM = $_.Name
$Row."VMWare Tools" = $_.toolsversionstatus
$myreport += $Row
}
}
Else
{
$myreport = @()
NO SNAPSHOTS FOUND <---- not sure how to add this ($myreport)
$myreport += $Row
}
}
I will check this and let you know.
$myreport = @()
$Vms = Get-VM -Name $list
foreach ($vm in $Vms) {
$snap = ($vm | get-snapshot).count
$snapresult = if ($snap -eq 0) {write-output "No Snapshot found"} else {write-output $snap}
$row = "" | select Name, "VMware Tools", Snapshots
$row.Name = $vm.Name
$row."VMware Tools"= $vm.ExtensionData.Guest.ToolsVersionStatus
$row.Snapshots = $snapresult
$myreport += $row
}
$myreport
a little different than I wanted to do but probably doable...the only thing is this line does not appear to be functioning correctly.
$snapresult = if ($snap -eq 0) {write-output "No Snapshot found"} else {write-output $snap}
I tested the code against a vm with no snapshots and it did not write: No Snapshot found , but instead just left field blank.
I am not sure but it worked at my end, i will check again and let you know.
I just realized that it only places a count in the snapshot field if there is 2 or more snapshots for a vm... a vm with 1 snapshot it does not place it in there as like a vm with no snapshots..
it is strange... thank you for your help...
I will check this and let you know.
Try changing this line
$snap = ($vm | get-snapshot).count
into this
$snap = (@($vm | get-snapshot)).count
That way you force an array, even if there is only 1 snapshot.
Blog: lucd.info Twitter: @LucD22 Co-author PowerCLI Reference
LUCD... that worked...
Thank you both for you help... this was the one piece I could not figure out from the full script I was making...
thanks!!