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
You forgot a back-tick at the end of the ComputerName line
{
$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
You forgot a back-tick at the end of the ComputerName line
{
$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
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
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
LucD,
but this script works when I provide the names as below
-ComputerName app21, app232, app34
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
Thanks for the explanation LucD.
I am able to tweak my script and that worked.