Hello, not sure if I am in the right spot to post my question about PowerShell/PowerCLI
I have a script in powershell that will send an email to an outlook client.
The msg body will have values regarding to two things: Active Snapshots in the virtual environment and CD ROMs attached to the VMs.
Let's talk when the email reports more that one CDRoms attached, how do I increase or decrease the msg body depending of the results gathered from the virtual environment ?
So, far I have this code:
if (($Snapshots -eq "yes") -or ($cdroms -eq "yes")) {
$array0 = @()
$array1 = @()
$array2 = @()
$array3 = @()
start-sleep 1 connect-VIServer $vcserver
$array0 += Get-VM | get-snapshot | %{$_.VM.Name} $array1 += Get-VM | get-snapshot | select-object -expandproperty Name
$array2 += Get-VM | get-snapshot | select-object -expandproperty Created
$array3 =@(Get-VM | where { $_ | Get-CDdrive | where { $_.ConnectionState.Connected -eq "true" } } | Select-object -ExpandProperty Name)
$i = 0
$j = 0
Do {
$msg.Body += "<FONT COLOR=black>VMware HealthCheck vCenter2</FONT><BR><BR>" + `
"<B><FONT COLOR=black>Snapshots Active</FONT></B><BR>" + `
"<B><FONT COLOR=black>VM Name</FONT></B>"+`
"<B><FONT COLOR=black> ` Name</FONT></B>"+` "<B><FONT COLOR=black> ` Created</FONT></B><BR>"+` "<FONT COLOR=Red>"+$array0+"</FONT>"+` "<FONT COLOR=Red> "+$array1+"</FONT>"+` "<FONT COLOR=Red> "+$array2+"</FONT><BR><BR>"+` "<B><FONT COLOR=black>CDROMS Connected (Bad Habit)</FONT></B><BR>"+"<B><FONT COLOR=black>VM Name</FONT></B><BR>"+"<FONT COLOR=Red>"+$array3+"</FONT>"
$i++
$j++
} Until (($array0[$i] -eq $null ) -or ($array3[$j] -eq $null))
The output i get is:
From: VMware Healtcheck
Sent: Thursday, June 20, 2013 1:26 PM
To: Me
Subject: VMware Health UPIvCenter2
VMware HealthCheck vCenter2
Snapshots Active
VM Name Name Created
VEEAM1 VEEAM1 Test1 Test2 06/20/2013 09:06:09 06/20/2013 09:31:15
CDROMS Connected (Bad Habit)
VM Name
MAILGATE
And would like to have is:
From: VMware Healtcheck
Sent: Thursday, June 20, 2013 1:26 PM
To: Me
Subject: VMware Health UPIvCenter2
VMware HealthCheck vCenter2
Snapshots Active
VM Name Name Created
VEEAM1 Test1 06/20/2013 09:06:09
VEEAM1 Test2 06/20/2013 09:31:15
CDROMS Connected (Bad Habit)
VM Name
MAILGATE
NOTE: I can have multiple snapshots or multiple VMs with CD Roms connected. Thanks for your help.
The reason you are not seeing all the snapshots is because your loop stops when either the snapshots or the cdroms are done.
Suppose you have 3 entries for the snapshots and 2 for the cdroms, you will only get 2 lines of each.
I would do it something like this
if (($Snapshots -eq "yes") -or ($cdroms -eq "yes")) {
$array0 = @()
$array1 = @()
$array2 = @()
$array3 = @()
start-sleep 1
connect-VIServer $vcserver
$vms = Get-VM
$snaps = $vms | Get-Snapshot
$array0 = $snaps | %{$_.VM.Name}
$array1 = $snaps| select-object -expandproperty Name
$array2 = $snaps | select-object -expandproperty Created
$array3 =@($vms | where { $_ | Get-CDdrive | where { $_.ConnectionState.Connected -eq "true" } } | Select-object -ExpandProperty Name)
# Header
$msg.Body += "<FONT COLOR=black>VMware HealthCheck vCenter2</FONT><BR><BR>"
# Snapshot header
$msg.Body += "<B><FONT COLOR=black>Snapshots Active</FONT></B><BR>"
$msg.Body += "<B><FONT COLOR=black>VM Name</FONT></B><B><FONT COLOR=black>Name</FONT></B><B><FONT COLOR=black>Created</FONT></B>"
# Snapshots
0..($array0.Count-1) | %{
$msg.Body += "<BR><FONT COLOR=Red>" + $array0[$_]
$msg.Body += "</FONT><FONT COLOR=Red>" + $array1[$_]
$msg.Body += "</FONT><FONT COLOR=Red>" + $array2[$_] + "</FONT><BR><BR>"
}
# CDRom header
msg.Body += "<B><FONT COLOR=black>CDROMS Connected (Bad Habit)</FONT></B><BR>"
# CDRoms
$array3 | %{
$msg.Body += "<B><FONT COLOR=black>VM Name</FONT></B><BR>"+"<FONT COLOR=Red>" + $_ + "</FONT>"
}
}
Note that I didn't check if the HTML spacing and layout is still like you wanted it. You might need to add some spacing.
Blog: lucd.info Twitter: @LucD22 Co-author PowerCLI Reference
The reason you are not seeing all the snapshots is because your loop stops when either the snapshots or the cdroms are done.
Suppose you have 3 entries for the snapshots and 2 for the cdroms, you will only get 2 lines of each.
I would do it something like this
if (($Snapshots -eq "yes") -or ($cdroms -eq "yes")) {
$array0 = @()
$array1 = @()
$array2 = @()
$array3 = @()
start-sleep 1
connect-VIServer $vcserver
$vms = Get-VM
$snaps = $vms | Get-Snapshot
$array0 = $snaps | %{$_.VM.Name}
$array1 = $snaps| select-object -expandproperty Name
$array2 = $snaps | select-object -expandproperty Created
$array3 =@($vms | where { $_ | Get-CDdrive | where { $_.ConnectionState.Connected -eq "true" } } | Select-object -ExpandProperty Name)
# Header
$msg.Body += "<FONT COLOR=black>VMware HealthCheck vCenter2</FONT><BR><BR>"
# Snapshot header
$msg.Body += "<B><FONT COLOR=black>Snapshots Active</FONT></B><BR>"
$msg.Body += "<B><FONT COLOR=black>VM Name</FONT></B><B><FONT COLOR=black>Name</FONT></B><B><FONT COLOR=black>Created</FONT></B>"
# Snapshots
0..($array0.Count-1) | %{
$msg.Body += "<BR><FONT COLOR=Red>" + $array0[$_]
$msg.Body += "</FONT><FONT COLOR=Red>" + $array1[$_]
$msg.Body += "</FONT><FONT COLOR=Red>" + $array2[$_] + "</FONT><BR><BR>"
}
# CDRom header
msg.Body += "<B><FONT COLOR=black>CDROMS Connected (Bad Habit)</FONT></B><BR>"
# CDRoms
$array3 | %{
$msg.Body += "<B><FONT COLOR=black>VM Name</FONT></B><BR>"+"<FONT COLOR=Red>" + $_ + "</FONT>"
}
}
Note that I didn't check if the HTML spacing and layout is still like you wanted it. You might need to add some spacing.
Blog: lucd.info Twitter: @LucD22 Co-author PowerCLI Reference