VMware Cloud Community
ganapa2000
Hot Shot
Hot Shot
Jump to solution

Unable to send the output of the script to email body

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

Reply
0 Kudos
1 Solution

Accepted Solutions
LucD
Leadership
Leadership
Jump to solution

You forgot a back-tick at the end of the ComputerName line

try

        {

            $results = Test-VSystemTimeSynchronization `

                -ComputerName app21, app232, app33 `

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

                -CompareWithNTPServerMaximumTimeDifferenceSeconds 30 `

                -RepetitionCount 2 `

                -RepetitionDelaySeconds 5 `


Blog: lucd.info  Twitter: @LucD22  Co-author PowerCLI Reference

View solution in original post

Reply
0 Kudos
6 Replies
LucD
Leadership
Leadership
Jump to solution

You forgot a back-tick at the end of the ComputerName line

try

        {

            $results = Test-VSystemTimeSynchronization `

                -ComputerName app21, app232, app33 `

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

                -CompareWithNTPServerMaximumTimeDifferenceSeconds 30 `

                -RepetitionCount 2 `

                -RepetitionDelaySeconds 5 `


Blog: lucd.info  Twitter: @LucD22  Co-author PowerCLI Reference

Reply
0 Kudos
ganapa2000
Hot Shot
Hot Shot
Jump to solution

Thanks for the reply LucD. that worked but I have one query.

currently I am specifying the VM Names manually.

-ComputerName app21, app232, app33 `

But when I tried the as below, it is not working

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

$vmList = Get-Content '.\servers.txt' -raw -join ','

-ComputerName $vmList  `

The contents of the .txt file are

app21

app232

app33

Reply
0 Kudos
LucD
Leadership
Leadership
Jump to solution

You don't need the -join, just an array of strings with the names.

Just do

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


Blog: lucd.info  Twitter: @LucD22  Co-author PowerCLI Reference

Reply
0 Kudos
ganapa2000
Hot Shot
Hot Shot
Jump to solution

LucD,

but this script works when I provide the names as below

-ComputerName app21, app232, app34

Reply
0 Kudos
LucD
Leadership
Leadership
Jump to solution

Correct, but when doing the -join you create 1 string from an array of strings.

The parameter wants an array of string, exactly what Get-Content returns.


Blog: lucd.info  Twitter: @LucD22  Co-author PowerCLI Reference

Reply
0 Kudos
ganapa2000
Hot Shot
Hot Shot
Jump to solution

Thanks for the explanation LucD.

I am able to tweak my script and that worked. Smiley Happy

Reply
0 Kudos