Advertisement
Xology

S1PassPhraseToNinjaOneSync

Sep 11th, 2024
211
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1.  
  2. #ninjaone App Keys
  3. $clientId = "xxx"
  4. $clientSecret = "xxx"
  5. $location = "oc"
  6.  
  7. function Get-S1APIData {
  8.  
  9.     [CmdletBinding()]
  10.     param (
  11.         [Parameter(Mandatory=$true)]
  12.         [string]
  13.         $APIRequest
  14.     )
  15.     $S1TenantList = @()
  16.     $S1TenantList += [pscustomobject]@{Name="XologyNFR"; S1Token = "xx" ; URL="https://apne1-1101-nfr.sentinelone.net/"}
  17.     $S1TenantList += [pscustomobject]@{Name="XologyMSP"; S1Token = "xx" ; URL="https://apne1-pax8.sentinelone.net/"}
  18.  
  19.     $AllS1TenantResults = @()
  20.  
  21.     $APIRequest = "$($APIRequest)?limit=100"
  22.  
  23.     foreach ($S1Tenant in $S1TenantList) {
  24.  
  25.         $HTTPHeaders = @{}
  26.         $HTTPHeaders.Add('Authorization', ("ApiToken {0}" -f ($S1Tenant.S1Token)))
  27.         $HTTPHeaders.Add('Content-Type', 'application/json')
  28.        
  29.         [Net.ServicePointManager]::SecurityProtocol = [Net.SecurityProtocolType]::Tls12
  30.  
  31.         $S1URL = "$($S1Tenant.URL)$APIRequest"
  32.    
  33.         do {
  34.             write-host "Executing: $S1URL"
  35.             $request = (Invoke-RestMethod -Headers $HTTPHeaders  -Uri $S1URL -Method Get  -ResponseHeadersVariable ResponseHeaders -StatusCodeVariable scv -SkipHttpErrorCheck)
  36.             if ($scv -eq 401) {
  37.                 throw "Bad API Key"
  38.             } elseif ($scv -ne 200) {
  39.                 throw "Bad Request"
  40.             }
  41.             $AllS1TenantResults += $request.data
  42.             $S1URL = "$($S1Tenant.URL)$APIRequest&cursor=$($request.pagination.nextCursor)"
  43.             if ($request.pagination.nextCursor -eq $null) {
  44.                 $S1URL = $null
  45.             }
  46.         } while ($S1URL)
  47.    
  48.     }
  49.  
  50.     return $AllS1TenantResults
  51. }
  52. function get-NinjaDeviceIDs {
  53.  
  54.            
  55.     # Construct Body
  56.     $body = @{
  57.         client_id     = $clientId
  58.         client_secret = $clientSecret
  59.         grant_type    = "client_credentials"
  60.         scope           = "monitoring management"
  61.     }
  62.        
  63.     # Get OAuth 2.0 Token
  64.     write-host "Getting Security Token"
  65.     $tokenRequest = Invoke-WebRequest -Method Post -Uri "https://$location.ninjarmm.com/ws/oauth/token" -ContentType "application/x-www-form-urlencoded" -Body $body -UseBasicParsing
  66.  
  67.     # Access Token
  68.     $token = ($tokenRequest.Content | ConvertFrom-Json).access_token
  69.  
  70.     $query = "/api/v2/devices?pageSize=100"
  71.  
  72.     $results = @()
  73.     do {
  74.         $request = (Invoke-RestMethod -Headers @{Authorization = "Bearer $($Token)" } -Uri "https://$location.ninjarmm.com/$query" -Method Get  -ResponseHeadersVariable ResponseHeaders )
  75.         $results += $request
  76.         $query = "/api/v2/devices?pageSize=100&after=$($request[-1].id)"
  77.         if ($request.count -eq 0) {
  78.             $query = $null
  79.         }
  80.     } while ($query)
  81.     return $results
  82. }
  83. class MappedDevice {
  84.     [string]$ComputerName
  85.     [string]$DisplayName
  86.     [int]$NinjaID
  87.     [string]$nodeClass
  88.     [string]$S1PassPhrase
  89. }
  90.  
  91. $S1PassPhrasesList = Get-S1APIData -APIRequest "/web/api/v2.1/agents/passphrases"
  92. $S1PassPhrasesListLookup = @{}
  93. foreach ($item in $S1PassPhrasesList | Where-Object {$_.computerName -ne $null} ) {
  94.     $S1PassPhrasesListLookup[$item.computerName] = $item
  95. }
  96.  
  97. $NinjaDeviceList = get-NinjaDeviceIDs | Where-Object {$_.nodeClass -like "windows_*"} | Sort-Object systemName
  98.  
  99. $NinjaDeviceToUpdateList = [System.Collections.Generic.List[object]]::new()
  100. foreach ($Device in $NinjaDeviceList) {
  101.     $MappedDevice = [MappedDevice]::New()
  102.     $MappedDevice.ComputerName  = $device.systemName
  103.     $MappedDevice.DisplayName   = $device.DisplayName
  104.     $MappedDevice.NinjaID       = $Device.id
  105.     $MappedDevice.nodeClass     = $Device.nodeClass
  106.     $MappedDevice.s1passphrase   = $S1PassPhrasesListLookup[$($device.systemName)].passphrase
  107.     [void]$NinjaDeviceToUpdateList.Add([PSCustomObject]$MappedDevice)
  108. }
  109. $NinjaDeviceToUpdateList.Count
  110.  
  111. ######
  112. $body = @{
  113.     client_id     = $clientId
  114.     client_secret = $clientSecret
  115.     grant_type    = "client_credentials"
  116.     scope           = "monitoring management"
  117. }
  118.    
  119. # Get OAuth 2.0 Token
  120. $tokenRequest = Invoke-WebRequest -Method Post -Uri "https://$location.ninjarmm.com/ws/oauth/token" -ContentType "application/x-www-form-urlencoded" -Body $body -UseBasicParsing
  121.  
  122. # Access Token
  123. $token = ($tokenRequest.Content | ConvertFrom-Json).access_token
  124.  
  125. foreach ($UpdateData in $NinjaDeviceToUpdateList | Where-Object {[string]::IsNullOrEmpty($_.S1PassPhrase) -ne $true}) {
  126.  
  127.     $body = @{
  128.         sentinelonepassphrase     = $UpdateData.S1PassPhrase
  129.     } | ConvertTo-Json
  130.  
  131.     write-host "Updating: $($UpdateData.ComputerName)"
  132.     $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
  133.     if ($scv -eq 401) {
  134.         throw "Bad API Key"
  135.     } elseif ($scv -ne 204) {   # 200 is ok = 204 is ok (no content)
  136.         throw "Bad Request: Error Code: $scv - $response"
  137.     }
  138. }
  139.  
  140.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement