NetXMS Support Forum

English Support => General Support => Topic started by: Egert143 on January 11, 2020, 07:55:38 PM

Title: cant get powershell script to run
Post by: Egert143 on January 11, 2020, 07:55:38 PM
Hello

i made a small powershell query to return oldest folder age in days. But i cant get it to run with netxms agent, i get error:
Process "powershell -Command "& SCRIPT"" killed because of execution timeout


Agent conf file:
MasterServers = ip
ConfigIncludeDir = C:\NetXMS\etc\nxagentd.conf.d
LogFile = C:\NetXMS\log\netxms-agent.log
FileStore = C:\NetXMS\var
SubAgent = winperf.nsm

ExternalParameter = Custom.Script:powershell -Command "& {Get-ChildItem 'path' | Where { $_.PSIsContainer } | Sort CreationTime | Select -First 1 | Foreach-Object {$dtdiff = (New-TimeSpan ($_.LastWriteTime) $(Get-Date))}; write-host $dtdiff.days}"

Title: Re: cant get powershell script to run
Post by: Egert143 on January 12, 2020, 07:24:57 PM
Attempt #2

This time i tryed with ExternalParametersProvider:

Agent Conf

MasterServers = ip
ExecTimeout = 10000
ConfigIncludeDir = C:\NetXMS\etc\nxagentd.conf.d
LogFile = C:\NetXMS\log\netxms-agent.log
FileStore = C:\NetXMS\var
SubAgent = winperf.nsm
SubAgent = filemgr.nsm
DebugLevel = 5

ExternalParametersProvider=powershell.exe C:\NetXMS\script\test2.ps1:60


Script: Get-ChildItem 'D:\test\609920fa-6243-4587-b33c-b73961130e04' | Where { $_.PSIsContainer } | Sort LastWriteTime | Select -First 1 | Foreach-Object {$age = (New-TimeSpan ($_.LastWriteTime) $(Get-Date)); $out = 'Parameter1=' + $age.days; echo $out}

Agent Debug log:2020.01.12 19:17:10.703 *D* [                   ] ProcessExecutor::execute(): process "CMD.EXE /C powershell.exe C:\NetXMS\script\test2.ps1" started
2020.01.12 19:17:10.703 *D* [                   ] ParamProvider::poll(): started command "powershell.exe C:\NetXMS\script\test2.ps1"
2020.01.12 19:17:22.825 *D* [                   ] ParamProvider::poll(): command "powershell.exe C:\NetXMS\script\test2.ps1" execution completed, 0 values read


When i run command manualy i get response Parameter1=114
CMD.EXE /C powershell.exe C:\NetXMS\script\test2.ps1

Why doesent netxms get that value ?

EDIT: when testing with smaller ammount of files it does return a value. When testing folder with ~82000 sub folders where script runs about ~6 seconds then agent gets result 0, manualy running script works ? Any fix for that ?

Title: Re: cant get powershell script to run
Post by: Egert143 on January 13, 2020, 02:38:28 PM
Bump, Any suggestions what could be the issue here ? :)

Title: Re: cant get powershell script to run
Post by: Filipp Sudanov on January 13, 2020, 07:23:43 PM
There is agent configuration parameter ExternalParameterProviderTimeout that sets timeout for external parameters provider execution. By default it's equial to 30 seconds. The behavior you describing might be happening if your script runs longer then 30s.
Title: Re: cant get powershell script to run
Post by: Egert143 on January 14, 2020, 09:45:19 AM
Thanks for suggestion, i tested that config parameter but no joy. I think it must be somthing else, because according to logs, script dont even have time to run 30s before '0 value' is returned according to netxms agent.

From agent log:
2020.01.14 09:33:34.573 *I* [                   ] NetXMS Agent started
2020.01.14 09:33:34.575 *D* [                   ] ProcessExecutor::execute(): process "CMD.EXE /C powershell.exe C:\NetXMS\script\test.ps1" started
2020.01.14 09:33:34.575 *D* [                   ] ParamProvider::poll(): started command "powershell.exe C:\NetXMS\script\test.ps1"
2020.01.14 09:33:39.852 *D* [                   ] ParamProvider::poll(): command "powershell.exe C:\NetXMS\script\test.ps1" execution completed, 0 values read


Agent conf file:
#
# NetXMS agent configuration file
# Created by agent installer at Wed Sep  4 14:38:01 2019
#

MasterServers = ip
ConfigIncludeDir = C:\NetXMS\etc\nxagentd.conf.d
LogFile = C:\NetXMS\log\netxms-agent.log
FileStore = C:\NetXMS\var
SubAgent = winperf.nsm
SubAgent = filemgr.nsm
DebugLevel = 5
ExternalParameterProviderTimeout = 60

ExternalParametersProvider=powershell.exe C:\NetXMS\script\test.ps1:300


Script after updating PowerShell to ver 4:
$folder = Get-ChildItem 'D:\a8becad8-6be2-4164-a405-dea4052b3720' -Directory |
    Sort LastWriteTime |
    Select -First 1

$age = ([datetime]::Now - $folder.LastWriteTime).TotalHours
#$age = ([datetime]::Now - $folder.LastWriteTime).Days


if($age -ige 1)
{

    $Output = 'Parameter1=' + $age

}else{

    $Output = 'Parameter1=1'

}

echo $Output


When i change path variable in script to count smaller folder where result will be instant, instead of few seconds then agent gets the value.

Even updated to latest version today.
Title: Re: cant get powershell script to run
Post by: Filipp Sudanov on January 14, 2020, 05:30:22 PM
Yes, right, it's not the timeout, otherwise there would be a message on 4-th debug level:
command "powershell.exe C:\test3.ps1" execution timeout (70 seconds)

Can you increase debug level to 7 and attach the log.

Title: Re: cant get powershell script to run
Post by: Egert143 on January 14, 2020, 07:52:15 PM
Debug 7 gave one extra line:
2020.01.14 19:49:10.356 *D* [                   ] ProcessExecutor::execute(): process "CMD.EXE /C powershell.exe C:\NetXMS\script\test.ps1" started
2020.01.14 19:49:10.356 *D* [                   ] ParamProvider::poll(): started command "powershell.exe C:\NetXMS\script\test.ps1"
2020.01.14 19:49:10.357 *I* [                   ] NetXMS Agent started
2020.01.14 19:49:15.569 *D* [                   ] ProcessExecutor::readOutput(): stopped on GetOverlappedResult (The pipe has been ended.)
2020.01.14 19:49:15.569 *D* [                   ] ParamProvider::poll(): command "powershell.exe C:\NetXMS\script\test.ps1" execution completed, 0 values read
Title: Re: cant get powershell script to run
Post by: Egert143 on January 16, 2020, 08:03:20 AM
2020.01.14 19:49:15.569 *D* [                   ] ProcessExecutor::readOutput(): stopped on GetOverlappedResult (The pipe has been ended.)

Could that mean script or agent error ? It seems script runs about 5-6 seconds as usual. Also if someone is decent at powershell maybe there is somthing wrong with script itself? I am very beginner in PS :)
Title: Re: cant get powershell script to run
Post by: Egert143 on January 18, 2020, 01:01:15 PM
Seems like it is Bug / error on netxms, did quick and simple test:

Script:
$age = 1500;
Start-Sleep -Milliseconds 4840
$Output = 'Parameter.Name=' + $age
echo $Output


if script ran 4835 ms or more then no result to agent. Anything less and agent received value.

Also "The pipe has been ended." doesent mean error, it comes with 0 or 1 value.
Title: Re: cant get powershell script to run
Post by: Filipp Sudanov on January 23, 2020, 03:18:03 PM
What is your PS and Windows versions?
Title: Re: cant get powershell script to run
Post by: Egert143 on January 23, 2020, 04:07:29 PM
Win 7 Pro

$PSVersionTable:
Name                           Value                                                                                                                                                                                                                             
----                           -----                                                                                                                                                                                                                             
PSVersion                      4.0                                                                                                                                                                                                                               
WSManStackVersion              3.0                                                                                                                                                                                                                               
SerializationVersion           1.1.0.1                                                                                                                                                                                                                           
CLRVersion                     4.0.30319.42000                                                                                                                                                                                                                   
BuildVersion                   6.3.9600.16406                                                                                                                                                                                                                   
PSCompatibleVersions           {1.0, 2.0, 3.0, 4.0}                                                                                                                                                                                                             
PSRemotingProtocolVersion      2.2   



Currently i have setup where i run PS script with task scheduler and save result into txt file. Then with netxms agent i run a script that reades value out of txt file. That way it stays well under 1 sec runtime and works perfectly. Currently testing in 8 different machines.

ExternalParametersProvider=powershell.exe C:\NetXMS\script\ReturnInfo.ps1:1800
Title: Re: cant get powershell script to run
Post by: Tursiops on February 17, 2020, 08:56:17 AM
Could the issue be the missing quotes?
Try this (not sure about your execution policy, so I added that):
ExternalParametersProvider=powershell.exe -executionpolicy bypass -c "C:\NetXMS\script\ReturnInfo.ps1":1800

Without the quotes, I'm not sure what NetXMS will make out of your C:, considering the colon is a separator.

Your original ExternalParameter did not escape the $ character, which needs to be double up, e.g. like this:
ExternalParameter=Custom.Script:powershell -Command "& {Get-ChildItem 'path' | Where { $$_.PSIsContainer } | Sort CreationTime | Select -First 1 | Foreach-Object {$$dtdiff = (New-TimeSpan ($$_.LastWriteTime) $$(Get-Date))}; write-host $$dtdiff.days}"
Title: Re: cant get powershell script to run
Post by: Egert143 on February 17, 2020, 11:44:11 AM
Thanks for the reply. In later posts i changed the polling method to script file instead of direct command in agent conf. When i changed script content to simple output where i could change execution time, then depending on run time > 4835ms:error | < 4835ms:good. So i think path variable is working.

$age = 1500;
Start-Sleep -Milliseconds 4840
$Output = 'Parameter.Name=' + $age
echo $Output



Currently working method:

Agent Conf:
ExternalParametersProvider=powershell.exe C:\NetXMS\script\ReturnInfo.ps1:1800

ReturnInfo.ps:
$DataFile = "C:\NetXMS\script\output.txt"

$Array = "";

if (Test-Path -Path $DataFile) {

    $Content = Get-Content $DataFile
    $Array = $Content.split('|');

}


return $Array


Output.txt is generated from different script that is running with task scheduler.