-2147217407 error or 80041001 how to avoid programmatically
If you have done any scripting or programming for WMI you have inevitably run into error 2147217407 or -2147217407 or 80041001
What is this error?
Well it is ExecASynchQuery Failure.
Umm okay what does that mean?
It means the query took long enough that it timed out.
What does that mean?
That either you are referencing an object that was just created (and isn't done being created) or the server was to busy (might want to consider opening more WMI connections) or that your search isn't specific enough
Okay how do I fix... or avoid the issue. Well this is going to infuriate you, but seriously, take a deep breath and wait. 10 seconds seems to be a good time.
Here is how you do it in VBA:
'Turn off the pause if error thingy (don't worry we check)
On Error Resume Next
'Create the advertisement.
'This is your put statement or could be any command line
instAdvert.Put_
'Count it so you don't infinite loop
ErrCounter = 0
'Try it once. You'll like it
Do While (Err.Number <> 0) And (ErrCounter < 3)
'Tell the plebian something didn't work right.
'Plus you give them error code... so when they report something broke you say what :)
Result = MsgBox("Advertisement connect failed Trying again in 10 seconds" & vbCrLf & _
" Error Loop #: " & ErrCounter & " of 3" & vbCrLf & _
" Error Number: " & Err.Number, vbExclamation, "Trying again in 10 seconds")
'Sleep for 10 seconds because advertisements are being communicated to fast (WMI issues)
newHour = Hour(Now())
newMinute = Minute(Now())
' + 10 = Delay 10 seconds from now
newSecond = Second(Now()) + 10
waitTime = TimeSerial(newHour, newMinute, newSecond)
' Hang time!
Application.Wait waitTime
'Reset the Err counter
' !!!!! VERY IMPORTANT !!!!!
Err.Number = 0
'Try again
instAdvert.Put_
'count up so you don't infinite loop I miss C++ (inside joke)
ErrCounter = ErrCounter + 1
Loop
'Turn error stopping back on!
'Don't forget this one; otherwise you'll have no idea if something breaks later.
On Error GoTo 0