Author Topic: cant get powershell script to run  (Read 138 times)

Egert143

  • Jr. Member
  • **
  • Posts: 56
    • View Profile
cant get powershell script to run
« 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:
Code: [Select]
Process "powershell -Command "& SCRIPT"" killed because of execution timeout

Agent conf file:
Code: [Select]
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

  • Jr. Member
  • **
  • Posts: 56
    • View Profile
Re: cant get powershell script to run
« Reply #1 on: January 12, 2020, 07:24:57 pm »
Attempt #2

This time i tryed with ExternalParametersProvider:

Agent Conf
Code: [Select]
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:
Code: [Select]
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:
Code: [Select]
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
Code: [Select]
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 ?

« Last Edit: January 12, 2020, 07:40:43 pm by Egert143 »

Egert143

  • Jr. Member
  • **
  • Posts: 56
    • View Profile
Re: cant get powershell script to run
« Reply #2 on: January 13, 2020, 02:38:28 pm »
Bump, Any suggestions what could be the issue here ? :)


Filipp Sudanov

  • Global Moderator
  • Jr. Member
  • *****
  • Posts: 74
    • View Profile
Re: cant get powershell script to run
« Reply #3 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.

Egert143

  • Jr. Member
  • **
  • Posts: 56
    • View Profile
Re: cant get powershell script to run
« Reply #4 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:
Code: [Select]
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:
Code: [Select]
#
# 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:
Code: [Select]
$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

  • Global Moderator
  • Jr. Member
  • *****
  • Posts: 74
    • View Profile
Re: cant get powershell script to run
« Reply #5 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:
Code: [Select]
command "powershell.exe C:\test3.ps1" execution timeout (70 seconds)
Can you increase debug level to 7 and attach the log.


Egert143

  • Jr. Member
  • **
  • Posts: 56
    • View Profile
Re: cant get powershell script to run
« Reply #6 on: January 14, 2020, 07:52:15 pm »
Debug 7 gave one extra line:
Code: [Select]
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

  • Jr. Member
  • **
  • Posts: 56
    • View Profile
Re: cant get powershell script to run
« Reply #7 on: January 16, 2020, 08:03:20 am »
Code: [Select]
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

  • Jr. Member
  • **
  • Posts: 56
    • View Profile
Re: cant get powershell script to run
« Reply #8 on: January 18, 2020, 01:01:15 pm »
Seems like it is Bug / error on netxms, did quick and simple test:

Script:
Code: [Select]
$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.