Forum Discussion

SteveDahlberg's avatar
9 months ago
Solved

New to LM: How do I refer to the local filesystem in an embedded PowerShell script of a DataSource?

How do I refer to the local filesystem in an embedded PowerShell script of a DataSource? The below works fine from a PowerShell session console on a Collector server, but fails when running embedded in LogicMonitor. The Active Directory LogicMonitor service account is in the local Administrators group on the Resource server (a LM Collector server in this), same as my admin account using the PowerShell session. It also has explicit NTFS "Full Control" rights on the folder in the path variable below. 

Do I have to use the Get-PSDrive cmndlet? Or is there a simple way to do it when running embedded in the LogicMonitor environment?

$textFilePath = 'C:\ps\LM-custom_list.txt' $localList = Get-Content -Path $textFilePath

-Steve

  • Yes, and you see it worked for me in my screenshot with a full path. The only thing I'm aware of that LM will do to a powershell script is do a search-and-replace of ##TOKEN## but I didn't see any of that in your code. Even your error message isn't show the path getting screwed up and it's powershell telling you the file doesn't exist there.

    I would 100% make sure the script is actually running on the server you think it's running on. Does "Write-Output $env:computername" show the name of the server with the file?

65 Replies

  • Yes, and you see it worked for me in my screenshot with a full path. The only thing I'm aware of that LM will do to a powershell script is do a search-and-replace of ##TOKEN## but I didn't see any of that in your code. Even your error message isn't show the path getting screwed up and it's powershell telling you the file doesn't exist there.

    I would 100% make sure the script is actually running on the server you think it's running on. Does "Write-Output $env:computername" show the name of the server with the file?

  • That's funny, I was doing something similar for other environmental variables, just to confirm it was indeed running as our LogicMonitor service account, and what the working directory was. But I will try those things. I will point that your last suggestion "Try just Test-Path "C:\JCC-Local_Text_Files\LM-VM_FQDN_List.txt" and see what that returns" - that's exactly what I did to generate that last screenshot of Test Collection Script. And you can see the PowerShell error that's captured on stdError - it lists the path and says it can't find it. 

    Let me absolutely confirm that using an absolute path like "C:\JCCC-Local_Text_Files\LM-VM_FQDN_list.txt" should be sufficient in an embedded Collection script to reference a local resource like this?  Again, it works perfectly as is from a PS session on that collector server.

  • I suggest simplifying the problem to the smallest you can and also do some sanity checks. For example just have the script only Write-Output $env:computername and make sure the script is running on the computer you think it's running on. Then use gc c:\ to see if it can list the JCCC folder. Then gc C:\JCC-Local_Text_Files\ to see files in that folder. If that is all good, then simplify the script. Try just Test-Path "C:\JCC-Local_Text_Files\LM-VM_FQDN_List.txt" and see what that returns. Basically debug the script a part at a time, within LM using the Test button.

  • Also, I just additionally inserted the below 2 lines of code right at the beginning of the function to get a direct output with the layer of error reporting, and here is a screen shot of the Test Collection Script - do my absolute paths look okay? Anything else provide a clue?

     

  • Also verify that the collector assigned to the device you are testing against is the same one with the file. LM will let you monitor a collector device using a completely different collector, although you should never do that (it will throw the collector DSes off). But something that isn't hard to mistakenly do. I usually set the collector device to use 127.0.0.1 specifically to help catch that.

    Aka if you have servers Collector01 and Collector02, make sure that the collector assigned to Collector01 is Collector01 (itself). LM will let you monitoring Collector01 using Collector02.

    • SteveDahlberg's avatar
      SteveDahlberg
      Icon for Neophyte rankNeophyte

      Mike, Yeah, the textfile is on one of our Collector servers, and that is what we're using to monitor.  Keep in mind, this all works fine in a PS session on the Collector server, it finds the local path, it all works. Just not in LogicMonitor, here are some screenshots below. In this case, the actual test of the textfile path and determination of the result happens in a function that attempts to find and read the contents of the file (a simple list of text, one per line). It's called from the main loop and passed the (previously-assigned) value of:

      $VM_FQDN_Path = 'C:\JCCC-Local_Text_Files\LM_FQDN-VM_list.txt'

      Here a screenshots (in order) of the function that attempts to read the textfile and load its contents, that function being called from the main script and the corresponding output (using Write-Host), and finally, the results in LogicMonitor from using Test Script on the Collection script:

      • SteveDahlberg's avatar
        SteveDahlberg
        Icon for Neophyte rankNeophyte

        Shoot, was hoping it would paste them in, not create download attachment links. Let me see if I can just paste them:

         

  • Shouldn't need to do anything special to reference local system. I'm not able to replicate the issue myself. Can you get a screenshot of a minimum script and Test output like my working test below?

     

  • I'm not clear where the text file you want to pull is. Is the file on the server running the collector software or on a different system?

    Scripts will always run from the collector. So if the file is in another system, you would need to write your script to have it connect and pull the file. That might be using something like \\server\c$ or doing a Invoke-Command to run powershell commands on the remote system.

    If it is local to the collector server, what does the Test Button/Poll Now show? What kinda LM script is this? Part of a ConfigSource? DataSource?

    • SteveDahlberg's avatar
      SteveDahlberg
      Icon for Neophyte rankNeophyte

      I believe most of that is answered in my post. But to be clear, the .txt textfile is (for now anyway) at the absolute path "C:\JCCC-Local_Text_Files\my_testfile.txt" on one of our collector servers where I've been developing the script. It works fine from a PowerShell session on the collector server. But so far, I can't get it to see the file (so I can read from it in the script, like I do when the same script runs in PS session on the collector server). This is in a DataSource.

      I use `Test-Path $textFilePath` in the script to make sure it's there, works fine, but when running embedded in LM, it always reports that it can't find it at that absolute path location. I even tried copying it to "C:\Program Files\LogicMonitor\Agent\bin\JCCC-Local_Text_Files" but that didn't work, then I tried just the text file itself at that location (rather than the containing folder), but no joy. The LogicMonitor service account is in the local admins group on this Windows server, and has "Full Control" explicitly granted on the folder in question, although it shouldn't need it I wouldn't think.

      I'm relatively new to this, and it feels like I'm missing the proper way to refer to the local filesystem (if it's different than what I showed above with Test-Path above. I'm totally stuck.

      -Steve