Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
Skins
  • Light
  • Brite
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (Flatly)
  • No Skin
Collapse
Brand Logo

NetXMS Forum

  1. Home
  2. Support
  3. cant get powershell script to run

cant get powershell script to run

Scheduled Pinned Locked Moved Support
13 Posts 3 Posters 18.3k Views
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • E
    E
    Egert143
    wrote on last edited by
    #2

    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 ?

    1 Reply Last reply
    0
    • E
      E
      Egert143
      wrote on last edited by
      #3

      Bump, Any suggestions what could be the issue here ? 🙂

      1 Reply Last reply
      0
      • F
        F
        Filipp Sudanov
        wrote on last edited by
        #4

        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.

        1 Reply Last reply
        0
        • E
          E
          Egert143
          wrote on last edited by
          #5

          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.

          1 Reply Last reply
          0
          • F
            F
            Filipp Sudanov
            wrote on last edited by
            #6

            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.

            1 Reply Last reply
            0
            • E
              E
              Egert143
              wrote on last edited by
              #7

              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
              
              1 Reply Last reply
              0
              • E
                E
                Egert143
                wrote on last edited by
                #8
                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 🙂

                1 Reply Last reply
                0
                • E
                  E
                  Egert143
                  wrote on last edited by
                  #9

                  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.

                  1 Reply Last reply
                  0
                  • F
                    F
                    Filipp Sudanov
                    wrote on last edited by
                    #10

                    What is your PS and Windows versions?

                    1 Reply Last reply
                    0
                    • E
                      E
                      Egert143
                      wrote on last edited by
                      #11

                      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
                      
                      1 Reply Last reply
                      0
                      • T
                        T
                        Tursiops
                        wrote on last edited by
                        #12

                        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}"
                        
                        1 Reply Last reply
                        0
                        • E
                          E
                          Egert143
                          wrote on last edited by
                          #13

                          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.

                          1 Reply Last reply
                          0

                          Hello! It looks like you're interested in this conversation, but you don't have an account yet.

                          Getting fed up of having to scroll through the same posts each visit? When you register for an account, you'll always come back to exactly where you were before, and choose to be notified of new replies (either via email, or push notification). You'll also be able to save bookmarks and upvote posts to show your appreciation to other community members.

                          With your input, this post could be even better 💗

                          Register Login
                          Reply
                          • Reply as topic
                          Log in to reply
                          • Oldest to Newest
                          • Newest to Oldest
                          • Most Votes


                          • Login

                          • Don't have an account? Register

                          • Login or register to search.
                          Powered by NodeBB Contributors
                          • First post
                            Last post
                          0
                          • Categories
                          • Recent
                          • Tags
                          • Popular
                          • Users
                          • Groups