Hi,
I'm looking to output the following report in an e-mail - can someone please assist.
The script I run to check the number of virtual machines per pool is as follows -
Get-Pool | Select Pool_id, @{Name="NumVM";Expression={($_ | Get-DesktopVM).Count}}
Kind Regards,
Travis
$body = Get-Pool | Select Pool_id, @{Name="NumVM";Expression={($_ | Get-DesktopVM).Count}} | ConvertTo-Html | Out-String
$sMail = @{
From = 'your.email.address'
To = 'destination.email.address'
Subject = 'View Report'
Body = $body
SMTPServer = "your.smtp.gateway"
BodyAsHtml = $true
}
Send-MailMessage @sMail
You can use the Send-MailMessage cmdlet for that.
Something like this for example (update the values for your local settngs).
I used splatting for the cmdlet parameters
$sMail = @{
From = 'your.email.address'
To = 'destination.email.address'
Subject = 'View Report'
Body = "$(Get-Pool | Select Pool_id, @{Name="NumVM";Expression={($_ | Get-DesktopVM).Count}})"
SMTPServer = "your.smtp.gateway"
}
Send-MailMessage @sMail
Blog: lucd.info Twitter: @LucD22 Co-author PowerCLI Reference
Thanks LucD,
Seems to e-mail fine however when received the body contains no data i.e. blank.
Not sure if it's formatting which is an issue?
Thanks,
Travis
Hi LucD,
So I added (| out-string) and this then passed content into e-mail however the columns are not aligned, the data is a bit all over the place.
Anyway to have the data neatly presented in columns or at least aligned?
Can you try that?
It's just Luc's script with a convert to html and body as html.
$sMail = @{
From = 'your.email.address'
To = 'destination.email.address'
Subject = 'View Report'
Body = "$(Get-Pool | Select Pool_id, @{Name="NumVM";Expression={($_ | Get-DesktopVM).Count}})" | ConvertTo-Html | Out-String
SMTPServer = "your.smtp.gateway"
}
Send-MailMessage @sMail -BodyAsHtml
Hi,
It generated next to nothing I'm afraid.
The best result I've had so far is using - Body = "$(Get-Pool | Select Pool_id, @{Name="NumVM";Expression={($_ | Get-DesktopVM).Count}}FT -AutoSize | out-string)"
But still not perfect as in aligned.
$body = Get-Pool | Select Pool_id, @{Name="NumVM";Expression={($_ | Get-DesktopVM).Count}} | ConvertTo-Html | Out-String
$sMail = @{
From = 'your.email.address'
To = 'destination.email.address'
Subject = 'View Report'
Body = $body
SMTPServer = "your.smtp.gateway"
BodyAsHtml = $true
}
Send-MailMessage @sMail
Hi,
I marked both yours and LucD (Answer/Helpful). Hope this helps.
I'm now trying to run this script using a scheduled task and not having much success. I manage to get it to e-mail but it has no content. Here is how I have it setup -
- Scheduled task is running as domain admin direct from the connection server
- Program/Script: C:\Windows\System32\WindowsPowerShell\v1.0\powershell.exe
- Add arguments (optional): -file "C:\temp\View_VD_Numbers.ps1"
Script:
add-pssnapin VMware.View.Broker
$body = Get-Pool | Select Pool_id, @{Name="NumVM";Expression={($_ | Get-DesktopVM).Count}} | ConvertTo-Html | Out-String
$sMail = @{
From = '***'
To = '***'
Subject = 'VD Pool Numbers Monthly Report'
Body = $body
SMTPServer = "***"
BodyAsHtml = $true
}
Send-MailMessage @sMail
Please help.
A scheduled task requires some extra lines in your script.
See Alan's Running a PowerCLI Scheduled task post.
Did you already logon with the account you defined in the scheduled task, and check if you can run the script from the PowerCLI prompt?
Blog: lucd.info Twitter: @LucD22 Co-author PowerCLI Reference
Hi LucD,
I've seen that post before.
I tried adding this to the above of my script and I still get no output in the body of the e-mail (the e-mail however is sent which suggests the task is initiating - it shows no error in the history logs.
if (!(get-pssnapin -name VMware.View.Broker -erroraction silentlycontinue)) {
add-pssnapin VMware.View.Broker
}
I also tried this line in the arguments field: -PSConsoleFile "C:\Program Files\VMware\VMware View\Server\extras\PowerShell\add-snapin.ps1" "& "C:\temp\View_VD_Numbers.ps1""
If I run the script from Powershell or View CLI it seems to works fine.
Thanks.
2 things:
To connect to vCenter just add
$Server = "MyvCenter"
$Session = Connect-VIServer -Server $Server
You might need to add Add-PSSnapin VMware.VimAutomation.Core but I'm not sure about that I've never used view.
I recommend you create specific service accounts to run your VMware tasks. One read only and one with read/write permissions.
Run your scheduled task as vmw-service-ro and that should be you.
For the tasks that require adding, editing and such use the rw user.
More secure and cleaner.
Um I'm confused because I use no authentication in the current script but it works just fine.
And I thought I'd need connecting to the connection server rather than the VC?
That is correct, there is no connection to the VC required if you only use View cmdlets.
When you logon with the account you use in the scheduled task, can you run the script?
There are settings (execution policy, profile...) that might be different or not set for that account.
That account might also be missing permissions in the View environment.
Blog: lucd.info Twitter: @LucD22 Co-author PowerCLI Reference
Thanks LucD,
Got it working now. Tried a VDI account we have and it seemed to do trick.