Inconsistent Behavior in Script Token Replacements for "Missing" Properties
Hi Folks -
I'm on UIv4 and newish to LM: I chased this for way too many hours and realized that my optional/missing properties (and thus the tokens substituted in the scripts) seem to function differently in !posh (UIv3?) than in the DataSource modules "Test Active Discovery" section... AND in "Test Collection Script".
If the property didn't exist:
- In !posh and the module's Collection, it seemed to basically substitute the token for an empty string.
- In the module's ActiveDiscovery, it seemed to leave them in place, untouched.
This was a little difficult to figure out because the module's "Test Active Discovery" isn't helpful. It only displays successful key/value tables or a gentle ambiguous error "no instances found" popup. Also unfortunate, because the module's collection script "Test Script" seems to substitute them with an empty string - unlike what it does right above for Active Discovery.
Perhaps I'm doing this wrong? Anyone else see this? Seems I need to check for empty/null strings AND also the actual token strings before proceeding? This was unnecessarily difficult to figure out on my own. I'm still hopeful there is a "better way" (TM) for debugging and building modules that I'll miraculously stumble upon. (I'd love any tips)
I'll post a follow up to this with my simple test script and the Outputs I saw.
daviddhall
OP1 year agoThanks Mike, very helpful tips! I'll definitely add these tips to my debugging skills kit. :-)
Once I learned about !posh and !groovy, I became heavily reliant on them.
Regardless of !posh, you can still see the different module behavior in AD vs Collection. (AD = no replace, Collection = replace with empty string)
Mike Moniz
·1 year agoInteresting, your right and I can replicate that. Didn't notice that before. I try to use groovy most of the time and it works differently. So I myself is not sure if it has always been that way or not.
Mike Moniz
·1 year agoInteresting, I haven't really tried to directly debug scripts using !posh myself. I'm kinda surprised that it even attempts to replace tokens actually.
If it helps, when I'm writing up DataSources I do write and debug in the module section of LM and use the Test Script in most cases. Some tricks I use during debugging is to return an exit code that is not 0. That will cause the Test button to return your output as part of the error message. You can also test out your ActiveDiscovery (AD) code within the Collection section. Once the code outputs the correct aaa##bbb text then move it to AD. You can also write debug info to a log file, and if you put that in the log folder for the collector you can download the file via Send Log Files to LM (but this is really slow).
If I was writing a more complex powershell script. That I would likely write directly in ISE/VSC on the collector or other windows system first, but I know that isn't always an option. And I've never bothered to setup a groovy debug environment, so those I've always written directly in LM.
Also, and I think you will see this in a few existing DataSources, you should have a check in the script that the token substitution worked. Something like "if ($myprop -matches '##' -or $myprop -eq '')"
daviddhall
OP1 year agoOutputs
In !posh:
output:
test-server##
In "Test Active Discovery Script":
test-server##nothingness (actually looks like a table, so I'm just guessing)
In "Test Collection Script":
stdOutput: test-server##
daviddhall
OP1 year ago$exists = '##SYSTEM.HOSTNAME##' #replaced with my actual server name, but using "test-server" here
$noexists = '##nothingness##'
#need to strip out #'s so Write-Output succeeds in the module test areas
if ($exists.Contains('#')) {
$exists = $exists -replace '#', '' #remove any #'s
}
if ($noexists.Contains('#')) {
$noexists = $noexists -replace '#', '' #remove any #'s
}
Write-Output("$exists##$noexists")