Quantcast
Channel: VMware Communities : All Content - All Communities
Viewing all articles
Browse latest Browse all 182126

Unable to send the output of the script to email body

$
0
0

Hi,

 

I am unable to send the output of the script to email body. I am actually trying to compare the VM time with NTP Server and send a email when there is a difference.

 

Please help.

 

Full script is attached.

 

Error:

WARNING: Exception that is not related to network accessibility.

Some action in case of unknown exception...

 

Script

connect-viserver demovcenter

 

$smtp = '192.168.1.121'

$out_file = '.\output.txt'

if (Test-Path $out_file) {Remove-Item $out_file -force}

get-folder DEV | Get-VM | select -expandproperty Name | out-file '.\servers.txt'

$vmList = Get-Content '.\servers.txt'

try

        {

            $results = Test-VSystemTimeSynchronization `

                -ComputerName app21, app232, app33

                -CompareWithNTPServerName '0.pool.ntp.org' `

                -CompareWithNTPServerMaximumTimeDifferenceSeconds 30 `

                -RepetitionCount 2 `

                -RepetitionDelaySeconds 5 `

 

            $wrongResult += $results | Where-Object -Property Status -NE -Value $true | Select ComputerNameFQDN, ComparisonNTPServerName, ComparisonNTPServerTimeDifferenceSeconds

            $out_file = '.\output.txt'

            $wrongResult | Out-File -FilePath $out_file

            $body = Get-Content $out_file | Out-String

 

            if ($wrongResult)

            {

                Send-MailMessage `

                    -From ntptest@ga.com `

                    -To jsudan@ga.com `

                    -Subject 'Time synchronization error' `

                    #-Body ('Error on following servers: {0}' -f ($wrongResult.ComputerNameBasic -join ', ', (($wrongResult.ComparisonNTPServerTimeDifferenceSeconds) -join ', '))) `

                    -Body $body `

                    -Priority High `

                    -SmtpServer $smtp

            }

        }

        catch

        {

            Write-Warning -Message 'Exception that is not related to network accessibility.'

 

            if ($wrongResult)

            {

                Send-MailMessage `

                    -From ntptest@ga.com `

                    -To jsudan@ga.com `

                    -Subject 'Time synchronization error' `

                    -Body ('Exception: {0}' -f $_.Exception.Message) `

                    -Priority High `

                    -SmtpServer $smtp

            }

 

            'Some action in case of unknwon exception...'

        }

       

disconnect-viserver demovcenter


Viewing all articles
Browse latest Browse all 182126

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>