cant get powershell script to run

Started by Egert143, January 11, 2020, 07:55:38 PM

Previous topic - Next topic

Egert143

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}"


Egert143

#1
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 ?


Egert143

Bump, Any suggestions what could be the issue here ? :)


Filipp Sudanov

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.

Egert143

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.

Filipp Sudanov

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.


Egert143

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

Egert143

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 :)

Egert143

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.

Filipp Sudanov

What is your PS and Windows versions?

Egert143

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

Tursiops

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}"

Egert143

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.