Created
June 20, 2024 21:32
-
-
Save qcomer/fa0dcb609bed9956758d871625c0bfa8 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| function Invoke-SentinelWebRequest { | |
| [CmdletBinding()] | |
| param ( | |
| [Parameter()] | |
| [String] | |
| $Server, | |
| [Parameter()] | |
| [String] | |
| $APIToken, | |
| [Parameter()] | |
| [String] | |
| $Target | |
| ) | |
| $global:Headers = @{ | |
| 'Authorization' = "ApiToken $APIToken" | |
| 'Content-Type' = 'application/json' | |
| } | |
| $BaseURL = "$Server/web/api/v2.1/" | |
| if (-not($BaseURL.endsWith('/'))) { | |
| $BaseURL += '/' | |
| } | |
| if (-not([string]::IsNullOrEmpty($target))) { | |
| $URL = "$BaseURL$Target" | |
| } else { | |
| $URL = $BaseURL | |
| } | |
| $Response = Invoke-WebRequest -Uri $URL -Method 'GET' -Headers $global:Headers | |
| if ($response.headers['statuscode'] -ne 200) { | |
| Write-Output "ERROR: Could not get proper response. $($Response.Headers['StatusCode'])" | |
| } | |
| $Response = $Response.Content | ConvertFrom-Json | |
| return $response | |
| } | |
| function Get-InstallerFiles { | |
| [CmdletBinding()] | |
| param ( | |
| [Parameter()] | |
| [string] | |
| $FolderPath = 'C:\Windows\Temp', | |
| [Parameter()] | |
| [string] | |
| $SiteToken, | |
| [Parameter()] | |
| [string] | |
| $APIToken, | |
| [Parameter()] | |
| [String] | |
| $Server | |
| ) | |
| $Target = 'update/agent/packages?platformTypes=windows&status=ga&fileExtension=.msi&sortOrder=desc&limit=2' | |
| $Response = Invoke-SentinelWebRequest -APIToken "$APIToken" -Target "$Target" -Server "$Server" | |
| $Response = $Response.data | Where-Object { $_.osarch -eq '64 bit' } | |
| $Information = @{ | |
| FileName = $Response.FileName | |
| DownloadSize = $Response.FileSize | |
| Version = "$($Response.MajorVersion) $($Response.MinorVersion)" | |
| Sha1 = $REsponse.Sha1 | |
| DownloadURL = $Response.Link | |
| } | |
| if ([string]::IsNullOrEmpty($Information)) { | |
| Write-Output 'ERROR: Failed to retrieve a response for the Agent' | |
| exit 1 | |
| } | |
| $Filename = $Information.FileName | |
| $FilePath = Join-Path -Path "$FolderPath" "$fileName" | |
| if (Test-Path -Path "$FilePath") { | |
| $FileHash = (Get-FileHash -Path "$filePath" -Algorithm SHA1).hash | |
| if ($FileHash -eq $Information.Sha1) { | |
| return $filepath | |
| } | |
| } else { | |
| Invoke-WebRequest -Uri "$($Information.DownloadURL)" -OutFile "$FilePath" -UseBasicParsing -Headers $global:headers | |
| if (Test-Path -Path "$FilePath") { | |
| $FileHash = (Get-FileHash -Path "$filePath" -Algorithm SHA1).hash | |
| if ($FileHash -eq $Information.Sha1) { | |
| return $filepath | |
| } | |
| } | |
| } | |
| return $False | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment