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. Powershell commands in Agent config

Powershell commands in Agent config

Scheduled Pinned Locked Moved Support
8 Posts 2 Posters 24.2k 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.
  • M
    M
    MarkusW
    wrote on last edited by
    #1

    I need to monitor a java process on some Windows hosts with the NetXMS Agent installed and running. The regular Process.Count and Process.CountEx only work for the process name, which is "java". This is not enough for my needs, since I need to make sure that java is run with certain arguments.

    In linux, I would simply to a ps aux | grep <Java class or jar name> | wc -l which works for all user permissions.

    I'm not very familiar with neither Windows nor Powershell, but I did manage to construct this one-liner for doing (sort of) the same thing:

    (WMIC path win32_process get Commandline | select-string <Java class name>).Matches.count
    

    This command returns the number of matches, or null if none were found.

    This works when I manually start a powershell with elevated permissions. It does not work through an ExternalParameter in the Agent though. When querying in the DCI config it returns "" even when the process is running. Is this because powershell commands are not run with elevated privileges? The agent itself is run elevated as far as I know.

    The full external parameter line in the config looks like this:

    ExternalParameter = ProcMon.SymmetricLauncher:powershell -Command "& {(WMIC path win32_process get Commandline | select-string <Java class name>).Matches.count}"
    

    I also tried ExternalParameterShellExec, but it didn't work either.

    Are there any simple solutions to my problem?

    1 Reply Last reply
    0
    • M
      M
      MarkusW
      wrote on last edited by
      #2

      I see now in the docs that CountEx should be able to match the command line as well. It does not work for me unfortunately.

      Process.CountEx(java, <Java class name>) still returns 0. Is there some subtle feature here that I'm missing?

      NetXMS agent version is 2.0.6. Agent is running on a Windows 7 host.
      NetXMS server version is 2.0.3.

      1 Reply Last reply
      0
      • V
        V
        Victor Kirhenshtein
        wrote on last edited by
        #3

        Hi,

        just tested in on Windows - works as expected. Please provide exact details - process name and command line and how you specify Process.CountEx parameter.

        Best regards,
        Victor

        1 Reply Last reply
        0
        • M
          M
          MarkusW
          wrote on last edited by
          #4

          @victor said in Powershell commands in Agent config:

          Hi,

          just tested in on Windows - works as expected. Please provide exact details - process name and command line and how you specify Process.CountEx parameter.

          Best regards,
          Victor

          Thanks for your reply.

          There are two Java processes running on the agent host.

          The process I need to monitor has a long command line (700+ chars), mostly consisting of class path declarations and jvm arguments. I can't give the exact details, but it looks basically like this:

          "java" -cp "C:\SomePath1\;C:\SomePath2\;C:\SomePath3" -DsomeJvmArg1=someValue1 -DsomeJvmArg2=someValue2 com.domain.package.ClassName --someArg someValue
          

          The above is what comes up when I run the PowerShell command

          WMIC path win32_process get Commandline | select-string ClassName
          

          Process.CountEx(java) returns "2" as expected.

          Process.CountEx(java,ClassName) returns "0". I understand this might be because the command line is longer than the MAX_PATH defined in the source code? Since ClassName appears at the end of the long command line.

          However:

          Process.CountEx(java, SomePath1) also returns 0. As does any attempt to match on anything at the beginning of the command line.

          The Windows host seems to be a 32 bit system.

          1 Reply Last reply
          0
          • V
            V
            Victor Kirhenshtein
            wrote on last edited by
            #5

            Remember that second argument is regular expression, so certain characters have special meaning. For example, \ is an escape character, so to match it you should specify it as \\. As for class name you're probably right - agent extracts only first 255 characters of process command line. I'll fix that in next release.

            Best regards,
            Victor

            1 Reply Last reply
            0
            • M
              M
              MarkusW
              wrote on last edited by
              #6

              @victor said in Powershell commands in Agent config:

              Remember that second argument is regular expression, so certain characters have special meaning. For example, \ is an escape character, so to match it you should specify it as \\. As for class name you're probably right - agent extracts only first 255 characters of process command line. I'll fix that in next release.

              Best regards,
              Victor

              Thank you. I have not used any special characters in the second argument.

              I just did some testing. The class path of one of the java processes contains the word "symmetric".

              Process.CountEx(java) returns 2 as expected.
              Process.CountEx(java,symmetric) returns 0.
              Process.CountEx(java,s) returns 1, even though the character "s" is present in the command line of both java processes.
              Process.CountEx(java,sy) returns 0.
              Process.CountEx(java,.) returns 1.

              Process.CountEx(java,_) returns 1 even though _ is not present in either command line.

              It seems no matter what I put as matching pattern, CountEx returns 0 if the pattern is longer than one character (other than regex special chars), and 1 if the pattern is one character.

              Any ideas?

              1 Reply Last reply
              0
              • M
                M
                MarkusW
                wrote on last edited by
                #7

                @victor said in Powershell commands in Agent config:

                just tested in on Windows - works as expected.

                I did some further testing on different nodes. Process.CountEx works as expected on Windows 8.1 Pro, but not on Windows 7 Home Premium (don't ask, not my system design  😮 ).

                Is the Windows subagent supposed to be generic, or do you have a specific version subset that is supported?

                1 Reply Last reply
                0
                • V
                  V
                  Victor Kirhenshtein
                  wrote on last edited by
                  #8

                  It is supposed to be generic. I'll check it on Windows 7.

                  Best regards,
                  Victor

                  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