Powershell API - Get List of Computers

jeff.pyne
edited August 2022 in Enterprise Security

I am trying to use the Control Center API to get a list of computers from GravityZone. I've got all the basics working. I generated the API key and I am able to authenticate and query data. Now I am trying to use the code at the bottom of this message to get my computers from Active Directory. No matter what I do, it seems to return results from the "Computers and Groups" group in the hierarchy (indicated by the green arrow in this screensnip). I'm trying to get the computers from one of my Active Directory domains instead (indicated with the red arrow). I used the getCustomGroupsList method to start at the top of the hierarchy and descend down to Active Directory until I see the Name and ID of the AD domain I'm interested in. When I pass that ID into the code at bottom, it's still giving me results from the Computers and Groups group. If I leave the ParentID parameter off, it also returns results from Computers and Groups. (For brevity, I left out the code that uses the API Key to generate the Auth header ($auth_header), but I know that's working since I'm able to get results from my queries.)

------------------------------------ Begin Powershell Code ------------------------------------

$page = 1

$pagesCount = 999999

$Workstations = @()

While ($page -le $pagesCount) {

# Create the query. This will be executed one page a time.

$request_data = '{

"id":"301f7b05-ec02-481b-9ed6-c07b97de2b7b",

"jsonrpc":"2.0",

"method":"getEndpointsList",

"parentID": "ABCDEFGHI123456", # The ID returned from getCustomGroupsList

"filters": {

"type": {

"computers": true,

"vitualMachines": true

},

"depth": {

"allItemsRecursively": false

},

"options": {

"companies": {

"returnAllProducts": true

}

},

"security": {

"management": {

"managedWithBest": true,

"managedRelays": true

}

}

},

"params":

{

"page":' + $page + ',

"perPage":100

}

}'

# Generate the API call parameters

$params = @{

Uri     = $request_uri

Headers   = @{

'Authorization' = "$authheader"

'Content-Type' = "application/json"

}

Method   = 'POST'

Body    = $request_data

ContentType = 'application/json'

}

# Call the API endpoint

$response = Invoke-RestMethod @params

# If we get an error, spit it out and end the loop

If ($response.error) {

$response.error.message

exit 9

# If we get results, add them to the result set.

} Else {

$Workstations += $response.result.items

$pagesCount = $response.result.pagesCount

$page++

}

}

# Dump out the results

$Workstations

------------------------------------ End Powershell Code ------------------------------------

Answers