executeAgentCommand fails successfully

Started by noel, January 23, 2024, 10:55:29 PM

Previous topic - Next topic

noel

I'd like to automate the installation of applications on Windows 10 machines with winget and two scripts.
I have the following script on the host in c:\wg.ps1
$p=Resolve-Path "C:\Program Files\WindowsApps\Microsoft.DesktopAppInstaller_*_x64__8wekyb3d8bbwe"
echo "Winget path is: '$p'"
& "$p\winget" $args
(This script fixes MS logic to not make winget available from the system context.) It works fine when invoked from an elevated cmd like this:
powershell -Command c:\wg install -e -h --accept-package-agreements --accept-source-agreements --log c:\install-log.txt --scope machine --id Notepad++.Notepad++
I also have the following nx script to install a few programs:
pkgIds = %(
"VeyonSolutions.Veyon --custom /NoMaster",
"Git.Git",
'Microsoft.VisualStudioCode --custom "--add \"Microsoft.VisualStudio.Workload.ManagedDesktop;includeRecommended\" --add \"Microsoft.VisualStudio.Workload.NetWeb;includeRecommended\""',
"GIMP.GIMP",
"Inkscape.Inkscape",
"OpenJS.NodeJS",
"ApacheFriends.Xampp.8.2",
"Oracle.VirtualBox",
"RARLab.WinRAR",
"mcmilk.7zip-zstd",
"Notepad++.Notepad++",
"Python.Python.3.11",
"AivarAnnamaa.Thonny",
"Ghisler.TotalCommander",
"Adobe.Acrobat.Reader.64-bit",
"Postman.Postman"
);

println("Installing winget packages");
for (pkg : pkgIds) {
    print("Installing " . pkg . ": ");
    cmd = "-Command c:\wg install --accept-package-agreements --accept-source-agreements --log c:\install-log.txt -h -e --scope machine --id " . pkg;
    res = $node->executeAgentCommand("ps", cmd);
    println(res ? "OK" : "FAILED");
}
println("End of winget installation");
and the ps action is defined like this in the agent config:
Action = ps:powershell $1When I run the script it scrolls through all the packages in about 2 sec and prints OK for all of them when they clearly didn't succeed.
By now I spent 3-4 days with this issue and I just can't figure out where is the problem and I'm out of ideas what to try.
I verified that the wg.ps1 works by executing it from cmd, I verified that the nx script works by replacing the wg script with one that prints the args to a txt file.

Filipp Sudanov

There might be some changes recently on how agent handles actions - it might terminate them after some quite short time unless "action produces output" is enabled.
Can you please enable
DebugLevel=6
in agent config and share the log file for the moment when action is being executed?

P.S.
There's actually another approach that might help - In Configuration->Packages you can have executable or archive. When you select "Deploy package..." (in new nxmc this is done from context menu of nodes or containers), file will be uploaded and in case of executable it's possible to specify a command that will be executed. See docs for more info: https://www.netxms.org/documentation/adminguide/package-manager.html#

noel

TLDR: Use $ErrorActionPreference = "Stop" so the agent knows one of the script's command failed.

Found the root cause, but I forget to post it here. Despite several issues and discussions on winget-cli's github suggesting it can be run in system context it turns out that it is in fact impossible. So when i tried to execute it with the agent it returned instantly with a non zero exit code, but the ps script returned normally due to the lack of $ErrorActionPreference = "Stop" which would have caused the agent to recognise the script had failed.

For now I'll use chocolatey with a custom caching proxy that rewrites packages - so even external resources can be cached locally by nginx - until MS figures out how to create a usable package manager.

P.S.
Thank you for the tip about auto termination, I'll keep that in mind. Also sorry for the late reply.