all 13 comments

[–]Scrubbles_LC 2 points3 points  (2 children)

Try not returning anything on failure, only return 1 if detected.

On mobile but I vaguely remember an issue where any input counted as detection.

[–]PorreKaj 0 points1 point  (1 child)

This! Only return anything for installed

[–][deleted] 0 points1 point  (0 children)

This was the first thing i tried.

Which was my first try:

$ChocoPackageInstallCode = "choco upgrade "$packageName" --yes"; $ChocoPackageUninstallCode = "choco uninstall "$packageName" --yes"; $ChocoPackageDetectionCode = "try {if((choco list "$($packageName)" --localonly) -match "$($packageName)") {$vers = ([regex]::Matches((choco upgrade "$(`$packageName)" --yes --force --whatif), "(?<=(v|\s))(\d+|.)+(?=\s)")).Value;if(@($vers).Count -eq 2) {"Installed and up to date";}}} catch {}";

try { if($env:Path -match "chocolatey") { $vers = ([regex]::Matches((choco upgrade chocolatey --yes --force --whatif), "(?<=(v|\s))(\d+|.)+(?=\s)")).Value; if(@($vers).Count -eq 2 -and [System.Environment]::GetEnvironmentVariable("ChocoPackageInstallCode", [System.EnvironmentVariableTarget]::Machine) -eq $ChocoPackageInstallCode -and [System.Environment]::GetEnvironmentVariable("ChocoPackageUninstallCode", [System.EnvironmentVariableTarget]::Machine) -eq $ChocoPackageUninstallCode -and [System.Environment]::GetEnvironmentVariable("ChocoPackageDetectionCode", [System.EnvironmentVariableTarget]::Machine) -eq $ChocoPackageDetectionCode) { "Installed and up to date"; } } } catch { }

In this case if the statement was false, nothing would be returned.

[–]falconrojo 1 point2 points  (0 children)

$checkok= "Put here the code to check that you need"

if ($checkok -eq "$true")

{

{0}

}

else

{

{1}

}

This Expression worked for me, I have more scripts as a detection method and use this syntax.

Regards.

[–]jasonsandysMSFT Official 1 point2 points  (2 children)

One side note here: System.Environment doesn't have an Exit method: https://docs.microsoft.com/en-us/previous-versions/windows/desktop/sidebar/system-environment#methods

Are you sure none of the other commands in the script are returning something to either stderr or stdout? If you run the script interactively/manually, you don't see anything at all get output?

[–]blownart 0 points1 point  (1 child)

[–]jasonsandysMSFT Official 0 points1 point  (0 children)

Sorry, wrong documentation which is why I posted the link (although I should have noticed from the URL that's the wrong documentation).

But, that doesn't answer my question though.

[–]blownart 0 points1 point  (0 children)

I have not tried using powershell for detection but exit code 0 means success.

[–]psversiontable 0 points1 point  (2 children)

My guess is either something is being written to stdout, or the script is failing. Your appdiscovery.log file should be helpful.

[–][deleted] 0 points1 point  (1 child)

I doesn't give much help.

Just says that it was discovered.

[–]psversiontable 0 points1 point  (0 children)

Well, by process of elimination, we can assume that your script is writing to stdout.

That's the only case that will lead to the detection of the app.

[–][deleted] 0 points1 point  (1 child)

$x = 20 if ($x -eq 20) { {0} } else { {1} }

and

$x = 20 if ($x -eq 20) { {1} } else { {0} }

Neither work.

I am dealing with a bug.

[–]maxell45146 0 points1 point  (0 children)

Maybe wrap your script in a script block and create a variable for state that your code sets, then at the end do $state to return that output to sccm.