dazzpowder
Enthusiast
Enthusiast

Error handling help

Hi All,

I have an esxi postbuild script, all works well except when trying to add the syslog server, that doesn't always work as the DNS entries for some unknown arent always successful, if I run it manually after the script runs it succeeds, so to ensure the DNS entries are there I added some error handling in the add syslog section which catches the error and then runs the set-dns function, this works except the script ends there and doesn't go on to re-attempt adding the syslog server.  How can I achieve this? below is the syslog and set-dns excerpts.

#########################

#Set DNS

#########################

function set-dns{

begin{

write-host -ForegroundColor GREEN "Adding DNS Entries $dns1,$dns2 on vmHost $esxi"

}

Process{

Get-VMHost $esx | Get-VMHostNetwork | Set-VMHostNetwork -DnsAddress $dns1,$dns2 -Confirm:$false

}

}

set-dns

#

######################

#Set syslog

######################

Write-Host -ForegroundColor GREEN "Set Syslog"

Try{

Set-VMHostSyslogserver -SysLogServer $syslog -SysLogServerPort 514 -VMHost $esx -ErrorAction Stop

}

Catch

{

$errormessage = $_.Exception.Message

$FailedItem = $_.Exception.ItemName

Write-Host "DNS  $errormessage $FailedItem"

set-dns

}

0 Kudos
16 Replies
vXav
Expert
Expert

Have you tried with that at the beginning of your function?

$ErrorActionPreference = continue 
0 Kudos
dazzpowder
Enthusiast
Enthusiast

‌not sure what you mean, the error occurs when adding the syslog server so wouldn't I want it to stop there and go to set-dns and correct the error? if I add it to the set-dns there is never an error generated even when Syslog complains that it can't resolve host.

0 Kudos
vXav
Expert
Expert

Sorry it was just me being thick.

Try without -ErrorAction Stop

0 Kudos
dazzpowder
Enthusiast
Enthusiast

Hmm maybe I'm being thick now, if I don't use stop how will I catch the error and send it back to carry out to set the dns entries again?  Once the set-dns is re-run after the catch I want it to continue with the rest of the script but it just catches the error and only runs the set-dns function.

0 Kudos
LucD
Leadership
Leadership

Can't you do it like this?

Check the DNS setting, if not set, set the DNS server addresses.

Then set the syslog server.

Get-VMHost $esx | Get-VMHostNetwork | where{!$_.DnsAddress} |

Set-VMHostNetwork -DnsAddress $dns1,$dns2 -Confirm:$false

Set-VMHostSyslogserver -SysLogServer $syslog -SysLogServerPort 514 -VMHost $esx


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

0 Kudos
dazzpowder
Enthusiast
Enthusiast

Thanks Luc, at first glance I didn't see what this got me but I overlooked the "!"

0 Kudos
LucD
Leadership
Leadership

Yes, sorry about that.

The ! is an alias for -not, which is a bit more visible in code :smileygrin:


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

0 Kudos
dazzpowder
Enthusiast
Enthusiast

Just tried it and it still fails as it cant resolve host yet if I check the dns entry it exists, and if I run the command to set syslog it works.  I just don't get why it doesn't work consistently, which is why I added the error handling to say if syslog fails go back and add the DNS then continue down to add syslog again (that's the part I don't know how to carry out) Its driving me nuts.

Any ideas?

0 Kudos
LucD
Leadership
Leadership

Is the reverse DNS resolution working?
Could it be that your DNS servers need some time to replicate?


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

0 Kudos
dazzpowder
Enthusiast
Enthusiast

‌still fails after the entries have been in place for over an hour, I did check this and the entry was present in all sites. once it fails and I catch the error how do I get it to run the syslog entry after sending it back to enter the set-dns? Could I put the dns entry and says log entry in the same function then catch the error when setting syslog then sending back to itself which would start with the dns entry?

0 Kudos
LucD
Leadership
Leadership

Could you perhaps show a screenshot of the error you were getting originally?


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

0 Kudos
vXav
Expert
Expert

Check out that bit of code and see what comes out of it.

I wrote it "on a napkin" so you might want to tweak it for error handling and stuff.

$esx = get-vmhost "YourNewHost"

$syslog = "udp://Syslog.server"

$syslogPort = 514

While ((Get-VMHostSysLogServer $esx).host -ne $syslog) {

  Set-DNS

  Sleep 4

  Set-VMHostSyslogserver -SysLogServer $syslog -SysLogServerPort $syslogPort -VMHost $esx

}

Get-VMHostSysLogServer $esx | select @{l="esx",e={$esx.name}},Host,Port

0 Kudos
dazzpowder
Enthusiast
Enthusiast

this is the error:-

Set-VMHostSyslogserver : 16/10/2016 16:14:33    Set-VMHostSysLogServer        Unable to configure syslog server information for host 'avmhost.esx'. Additional info: Message:  Failed to resolve remote host:

asyslog01.corp.local;

InnerText:  Failed to resolve remote host: asyslog01.corp.localEsxCLI.CLIFault.summary

Check the inner exception for more details.  

At line:3 char:1

+ Set-VMHostSyslogserver -SysLogServer $syslog -SysLogServerPort 514 -VMHost $esx

+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

    + CategoryInfo          : InvalidArgument: (txmashesxi05.esx:VMHostImpl) [Set-VMHostSysLogServer], VimException

    + FullyQualifiedErrorId : Client20_SystemManagementServiceImpl_SetVmHostSysLogServer_ViError,VMware.VimAutomation.ViCore.Cmdlets.Commands.Host.SetVMHostSysLogServer

even when I can see the DNS is populated and successful, from the same script run:-

DnsAddress              : {10.10.11.21, 10.10.12.21}

Like I say if I run the following manually it works:-

Set-VMHostSyslogserver -SysLogServer $syslog -SysLogServerPort 514 -VMHost $esx

0 Kudos
vXav
Expert
Expert

Looks like your $esx variable isn't right.

Is there a VMHost object type? As in, do you do a $esx = get-VMHost "WhateverESX" ?

0 Kudos
dazzpowder
Enthusiast
Enthusiast

I have the object as a vmhost, and I still get the same error occasionally, think I may just have to love with it.

0 Kudos
LucD
Leadership
Leadership

From what I can deduce, you have two DNS servers.

The error clearly states that there is a resolution issue.

When you check that the DNS entry exists, did you check on both DNS servers?

There might a replication delay between both DNS servers.


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

0 Kudos