Ad Widget

Collapse

Discovery with Powershell 2.0 and JSON

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • WebGreg
    Member
    • Feb 2021
    • 49

    #1

    Discovery with Powershell 2.0 and JSON

    Hello.

    Can anyone help me how to convert the script to work in Powershell 2.0?

    Ex.
    UserParameter=custom.vfs.dev.discovery,powershell -NoProfile -ExecutionPolicy Bypass -Command "& {[Console]::OutputEncoding = [System.Text.Encoding]::UTF8; $result_json = @{'data' = @(Get-WmiObject win32_PerfFormattedData_PerfDisk_PhysicalDisk | Where-Object {$_.name -ne '_Total'} | ForEach-Object {@{ '{#DEVICENAME}' = $_.Name }})} | ConvertTo-Json; [Console]::WriteLine($result_json);}"

    Problem is with ConvertTo-Json:

    The term 'ConvertTo-Json' is not recognized as the name of a cmdlet

    In this Host I can't update powershell to higher version. But I need to get some information from it.
  • WebGreg
    Member
    • Feb 2021
    • 49

    #2
    Has this forum ever helped anyone?
    I browse threads and essentially here are unanswered questions, or the author answers himself.

    Comment

    • dimir
      Zabbix developer
      • Apr 2011
      • 1080

      #3
      For sure there are lots of users that got help from others. Your case is very specific it requires knowledge of Powershell 2 this is why perhaps there is no answer yet. I'm not a Powershell user but it looks like it'd be easier to create a ps2 file with the powershell command
      Code:
      file: C:\powershell\custom-vfs-dev-discovery.ps2:
      ---
      [Console]::OutputEncoding = [System.Text.Encoding]::UTF8
      $result_json = @{'data' = @(Get-WmiObject win32_PerfFormattedData_PerfDisk_PhysicalDisk | Where-Object {$_.name -ne '_Total'} | ForEach-Object {@{ '{#DEVICENAME}' = $_.Name }})} | ConvertTo-Json
      [Console]::WriteLine($result_json)
      ---
      and just call it in UserParameter:
      Code:
      UserParameter=custom.vfs.dev.discovery,powershell -NoProfile -ExecutionPolicy Bypass -File C:\powershell\custom-vfs-dev-discovery.ps2
      Untested code but I hope you got the idea and this helps. :-)

      Comment

      • Hamardaban
        Senior Member
        Zabbix Certified SpecialistZabbix Certified Professional
        • May 2019
        • 2713

        #4
        PowerShell 2.0 does not contain implementation of the ConvertFrom-Json cmdlet and ConvertTo-Json cmdlet that are in PowerShell 3.0

        Comment

        • WebGreg
          Member
          • Feb 2021
          • 49

          #5
          Originally posted by Hamardaban
          PowerShell 2.0 does not contain implementation of the ConvertFrom-Json cmdlet and ConvertTo-Json cmdlet that are in PowerShell 3.0
          Exactly But somehow the data was collected early - when the powershell 2.0 was the highest version. Maybe it is still possible without a JSON?

          Originally posted by dimir
          For sure there are lots of users that got help from others. Your case is very specific it requires knowledge of Powershell 2 this is why perhaps there is no answer yet
          Yes but I'm new here - I asked 7 questions and this is the second one when someone replied :-D I was curious if the community is alive

          I found this: https://stackoverflow.com/questions/...implementation

          I didn't know how to implement it, but with your hint I have to try splitting it into two files - maybe it will work.

          Comment

          • WebGreg
            Member
            • Feb 2021
            • 49

            #6
            Originally posted by splitek
            In my opinion simplest way is to output everything in CSV format and use Zabbix preprocessing: "csv to json".


            Looks promising. Thanks, I'll try this too.

            Comment

            Working...