3 Replies Latest reply on Jan 2, 2019 8:16 AM by Andrew Waters

    Using powershell script to run Discovery API

    Ankush Deshmukh
      Share This:

      Hi Experts,

       

      I have run below powershell script to trigger discovery call and it worked successfully.

      Also got result as shown in below screenshot. But my requirement is to get result with IP address please guide me.

       

       

      $BASE_PATH = "https://appliance/api/v1.1" # UPDATE ME

      $TOKEN = "your token" # UPDATE ME

      $IP_SCAN = @{"scan_kind" = "IP";

                   "ranges"    = @("192.168.0.*"); # UPDATE ME

                   "label"     = "Test network"}

      # Tell powershell not to default to insecure protocols!

      [System.Net.ServicePointManager]::SecurityProtocol = [System.Net.SecurityProtocolType]::Tls12

      [System.Net.ServicePointManager]::ServerCertificateValidationCallback = { $true }

      function restful_get {

          Param([string]$url,

                [string]$token,

                [hashtable]$params)

          $headers = @{"Accept" = "application/json";

                       "Authorization" = "Bearer $token"}

          try {

              return Invoke-RestMethod -Uri $url -Headers $headers -Body $params `

                                       -ErrorVariable rest_error

          } catch {

              $err = $_

              try {

                  $temp = $rest_error.Message | ConvertFrom-Json

                  if ($temp.message) {

                      $err = $temp.message

                  }

              } catch {}

              Write-Error $err

              exit

          }

      }

      function restful_post {

          Param([string]$url,

                [string]$token,

                [hashtable]$params)

          $headers = @{"Accept" = "application/json";

                       "Authorization" = "Bearer $token"}

          $json = $params | ConvertTo-Json

          try {

              return Invoke-RestMethod -Method Post -Uri $url -Headers $headers `

                                       -Body $json -ErrorVariable rest_error

          } catch {

              $err = $_

              try {

                  $temp = $rest_error.Message | ConvertFrom-Json

                  if ($temp.message) {

                      $err = $temp.message

                  }

              } catch {}

              Write-Error $err

              exit

          }

      }

      function scan {

          Param([string]$base_path,

                [string]$token,

                [hashtable]$params)

          # Start the run

          $new_run = restful_post -url "$base_path/discovery/runs" `

                                  -token $token -params $params

          $run_id = $new_run.uuid

          # Poll run status every 3 seconds until the run is finished

          do {

              Start-Sleep -s 3

              Write-Host "Checking discovery run status..."

              $result = restful_get -url "$base_path/discovery/runs/$run_id" `

                                    -token $token

          } while (!$result.finished)

          # See what the scan found

          return restful_get -url "$base_path/discovery/runs/$run_id/inferred" `

                             -token $token

      }

      $result = scan -base_path $BASE_PATH -token $TOKEN -params $IP_SCAN

       

      echo "Inferred devices:"

      foreach($device in $result.PsObject.Properties) {

          echo "    $($device.Name) $($device.Value.count)"

      }