VMware Cloud Community
Travis_83
Enthusiast
Enthusiast
Jump to solution

E-mail VMView Pool Information

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

1 Solution

Accepted Solutions
vXav
Expert
Expert
Jump to solution

$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

View solution in original post

Reply
0 Kudos
14 Replies
LucD
Leadership
Leadership
Jump to solution

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

Travis_83
Enthusiast
Enthusiast
Jump to solution

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

Reply
0 Kudos
Travis_83
Enthusiast
Enthusiast
Jump to solution

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?

Reply
0 Kudos
vXav
Expert
Expert
Jump to solution

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

Reply
0 Kudos
Travis_83
Enthusiast
Enthusiast
Jump to solution

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.

Reply
0 Kudos
vXav
Expert
Expert
Jump to solution

$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

Reply
0 Kudos
vXav
Expert
Expert
Jump to solution

Thanks for the correct answer but the credit goes to LucD Smiley Happy

Reply
0 Kudos
Travis_83
Enthusiast
Enthusiast
Jump to solution

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.

Reply
0 Kudos
LucD
Leadership
Leadership
Jump to solution

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

Reply
0 Kudos
Travis_83
Enthusiast
Enthusiast
Jump to solution

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.

Reply
0 Kudos
vXav
Expert
Expert
Jump to solution

2 things:

  • You need to connect to your vCenter in the script
  • The user it runs as (domain admin in your case) must have permissions in vCenter.

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.

  1. In your AD create 2 users like: vmw-service-ro and vmw-service-rw
  2. Then in vCenter in add the read only user to the read only role and the rw to the administrator role.

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.

Reply
0 Kudos
Travis_83
Enthusiast
Enthusiast
Jump to solution

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?

Reply
0 Kudos
LucD
Leadership
Leadership
Jump to solution

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

Travis_83
Enthusiast
Enthusiast
Jump to solution

Thanks LucD,

Got it working now. Tried a VDI account we have and it seemed to do trick.

Reply
0 Kudos