Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #ninjaone App Keys
- $clientId = "xxx"
- $clientSecret = "xxx"
- $location = "oc"
- function Get-S1APIData {
- [CmdletBinding()]
- param (
- [Parameter(Mandatory=$true)]
- [string]
- $APIRequest
- )
- $S1TenantList = @()
- $S1TenantList += [pscustomobject]@{Name="XologyNFR"; S1Token = "xx" ; URL="https://apne1-1101-nfr.sentinelone.net/"}
- $S1TenantList += [pscustomobject]@{Name="XologyMSP"; S1Token = "xx" ; URL="https://apne1-pax8.sentinelone.net/"}
- $AllS1TenantResults = @()
- $APIRequest = "$($APIRequest)?limit=100"
- foreach ($S1Tenant in $S1TenantList) {
- $HTTPHeaders = @{}
- $HTTPHeaders.Add('Authorization', ("ApiToken {0}" -f ($S1Tenant.S1Token)))
- $HTTPHeaders.Add('Content-Type', 'application/json')
- [Net.ServicePointManager]::SecurityProtocol = [Net.SecurityProtocolType]::Tls12
- $S1URL = "$($S1Tenant.URL)$APIRequest"
- do {
- write-host "Executing: $S1URL"
- $request = (Invoke-RestMethod -Headers $HTTPHeaders -Uri $S1URL -Method Get -ResponseHeadersVariable ResponseHeaders -StatusCodeVariable scv -SkipHttpErrorCheck)
- if ($scv -eq 401) {
- throw "Bad API Key"
- } elseif ($scv -ne 200) {
- throw "Bad Request"
- }
- $AllS1TenantResults += $request.data
- $S1URL = "$($S1Tenant.URL)$APIRequest&cursor=$($request.pagination.nextCursor)"
- if ($request.pagination.nextCursor -eq $null) {
- $S1URL = $null
- }
- } while ($S1URL)
- }
- return $AllS1TenantResults
- }
- function get-NinjaDeviceIDs {
- # Construct Body
- $body = @{
- client_id = $clientId
- client_secret = $clientSecret
- grant_type = "client_credentials"
- scope = "monitoring management"
- }
- # Get OAuth 2.0 Token
- write-host "Getting Security Token"
- $tokenRequest = Invoke-WebRequest -Method Post -Uri "https://$location.ninjarmm.com/ws/oauth/token" -ContentType "application/x-www-form-urlencoded" -Body $body -UseBasicParsing
- # Access Token
- $token = ($tokenRequest.Content | ConvertFrom-Json).access_token
- $query = "/api/v2/devices?pageSize=100"
- $results = @()
- do {
- $request = (Invoke-RestMethod -Headers @{Authorization = "Bearer $($Token)" } -Uri "https://$location.ninjarmm.com/$query" -Method Get -ResponseHeadersVariable ResponseHeaders )
- $results += $request
- $query = "/api/v2/devices?pageSize=100&after=$($request[-1].id)"
- if ($request.count -eq 0) {
- $query = $null
- }
- } while ($query)
- return $results
- }
- class MappedDevice {
- [string]$ComputerName
- [string]$DisplayName
- [int]$NinjaID
- [string]$nodeClass
- [string]$S1PassPhrase
- }
- $S1PassPhrasesList = Get-S1APIData -APIRequest "/web/api/v2.1/agents/passphrases"
- $S1PassPhrasesListLookup = @{}
- foreach ($item in $S1PassPhrasesList | Where-Object {$_.computerName -ne $null} ) {
- $S1PassPhrasesListLookup[$item.computerName] = $item
- }
- $NinjaDeviceList = get-NinjaDeviceIDs | Where-Object {$_.nodeClass -like "windows_*"} | Sort-Object systemName
- $NinjaDeviceToUpdateList = [System.Collections.Generic.List[object]]::new()
- foreach ($Device in $NinjaDeviceList) {
- $MappedDevice = [MappedDevice]::New()
- $MappedDevice.ComputerName = $device.systemName
- $MappedDevice.DisplayName = $device.DisplayName
- $MappedDevice.NinjaID = $Device.id
- $MappedDevice.nodeClass = $Device.nodeClass
- $MappedDevice.s1passphrase = $S1PassPhrasesListLookup[$($device.systemName)].passphrase
- [void]$NinjaDeviceToUpdateList.Add([PSCustomObject]$MappedDevice)
- }
- $NinjaDeviceToUpdateList.Count
- ######
- $body = @{
- client_id = $clientId
- client_secret = $clientSecret
- grant_type = "client_credentials"
- scope = "monitoring management"
- }
- # Get OAuth 2.0 Token
- $tokenRequest = Invoke-WebRequest -Method Post -Uri "https://$location.ninjarmm.com/ws/oauth/token" -ContentType "application/x-www-form-urlencoded" -Body $body -UseBasicParsing
- # Access Token
- $token = ($tokenRequest.Content | ConvertFrom-Json).access_token
- foreach ($UpdateData in $NinjaDeviceToUpdateList | Where-Object {[string]::IsNullOrEmpty($_.S1PassPhrase) -ne $true}) {
- $body = @{
- sentinelonepassphrase = $UpdateData.S1PassPhrase
- } | ConvertTo-Json
- write-host "Updating: $($UpdateData.ComputerName)"
- $response = Invoke-RestMethod -Uri "https://$location.ninjarmm.com/api/v2/device/$($UpdateData.NinjaID)/custom-fields" -Method PATCH -Headers @{Authorization = "Bearer $($Token)" } -ContentType 'application/json' -Body $body -StatusCodeVariable scv -SkipHttpErrorCheck
- if ($scv -eq 401) {
- throw "Bad API Key"
- } elseif ($scv -ne 204) { # 200 is ok = 204 is ok (no content)
- throw "Bad Request: Error Code: $scv - $response"
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement