Hi guys,
I have a script that I'm basically using to perform an NTP server, Service and Policy check against our ESX hosts. The script works well and does what I needed to do. Once the script completes, it sends me an e-mail with the information that I am requesting.
The challenge I am having is that if I manually execute it, I am able to receive the content properly formatted as expected. However, if the Windows scheduled task executes the same script, the content of the mail gets truncated and is no longer formatted properly.
How can I ensure that the content will remain the same regardless if Windows task scheduler is executing the script?
See attached examples.
Here's the script I am using. I have modified it with fake information.
##This script will generate an NTP report for all clusters and ESX hosts.
##Load all VMware related snap-ins
#Add-PSSnapin VMware.VimAutomation.Core
Get-Module –ListAvailable VM* | Import-Module
##vCenter connection information
$vcenter = ''vcenter'
$vcenteruser = 'abc123@somecomp.com'
$vcenterpw = '*******'
##Connects to all vCenter instances
Connect-VIServer $vcenter -User $vcenteruser -Password $vcenterpw
##This script will query all ESX servers across all sites for NTP configured values and service status
#$NTPCheck = Get-VMHost *|Sort Name|Select Name, @{N="NTPServer";E={$_ |Get-VMHostNtpServer}},@{N="ServiceRunning";E={(Get-VmHostService -VMHost $_ | Where-Object {$_.key -eq 'ntpd'}).Running}},@{N="Policy";E={(Get-VmHostService -VMHost $_ | Where-Object {$_.key -eq 'ntpd'} | Select -ExpandProperty Policy)}} | ft -AutoSize
$NTPCheck =Get-Cluster * | Get-VMHost *|Sort Name|Select Name, @{N=“NTPServer“;E={$_ |Get-VMHostNtpServer}},@{N=“SvcRunning“;E={(Get-VmHostService -VMHost $_ |Where-Object {$_.key-eq “ntpd“}).Running}},@{N=“Policy”;E={(Get-VmHostService -VMHost $_ | Where-Object {$_.key-eq “ntpd“} | Select -ExpandProperty Policy)}} | ft Name, NTPServer, SvcRunning, Policy -autosize
##Parameters being used for mail SMTP configuration and e-mail addresses
$SendFrom = 'abc123@somecomp.com'
$SendTo = 'abc123@somecomp.com'
$Smtp = 'smtpsomething.something.com'
send-mailmessage -to $sendTo -from $sendFrom -Subject 'NTP Status Report' -smtpserver $smtp -body ($NTPCheck | out-string)
Disconnect-VIServer -server $vcenter -confirm:$false
Did you already try adding the Width parameter on the Out-String cmdlet?
Send-MailMessage -To $sendTo -From $sendFrom -Subject 'NTP Status Report' -SmtpServer $smtp -Body ($NTPCheck | Out-String -Width 132)
Blog: lucd.info Twitter: @LucD22 Co-author PowerCLI Reference
Did you already try adding the Width parameter on the Out-String cmdlet?
Send-MailMessage -To $sendTo -From $sendFrom -Subject 'NTP Status Report' -SmtpServer $smtp -Body ($NTPCheck | Out-String -Width 132)
Blog: lucd.info Twitter: @LucD22 Co-author PowerCLI Reference
Thanks Luc!! That did the trick!