Automation

 View Only
  • 1.  Unable to send the output of the script to email body

    Posted Jun 25, 2020 06:56 AM
      |   view attached

    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

    Attachment(s)

    zip
    Date_Compare.ps1.zip   8 KB 1 version


  • 2.  RE: Unable to send the output of the script to email body
    Best Answer

    Posted Jun 25, 2020 09:29 AM

    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 `



  • 3.  RE: Unable to send the output of the script to email body

    Posted Jun 25, 2020 10:30 AM

    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



  • 4.  RE: Unable to send the output of the script to email body

    Posted Jun 25, 2020 10:46 AM

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

    Just do

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



  • 5.  RE: Unable to send the output of the script to email body

    Posted Jun 25, 2020 10:58 AM

    LucD,

    but this script works when I provide the names as below

    -ComputerName app21, app232, app34



  • 6.  RE: Unable to send the output of the script to email body

    Posted Jun 25, 2020 11:04 AM

    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.



  • 7.  RE: Unable to send the output of the script to email body

    Posted Jun 25, 2020 01:27 PM

    Thanks for the explanation LucD.

    I am able to tweak my script and that worked. :smileyhappy: