Get-PC/Private/Spark.ps1
Zachary Gorman f3e8186b1c Spark, yay!
2024-08-02 12:03:04 -07:00

58 lines
2.2 KiB
PowerShell

$SparkHeaders = @{
"Content-Type" = "application/json"
"Authorization" = ""
"Accept" = "*/*"
"Accept-Encoding" = "gzip, deflate, br"
}
$SparkTenantPrefix = "samaritanhealth-amc"
$SparkURL = "https://$SparkTenantPrefix.ivanticloud.com"
Function Connect-ISM {
try {
#Try a fast route to check if authorization headers are set properly
Invoke-RestMethod -Method Get -URI "$SparkURL/api/odata" -Headers $SparkHeaders
} catch {
$errobject = ConvertFrom-Json $_
#A 404 means we were authorized and didn't find anything, as intended!
if ($errobject.code -eq "ISM_4004") { return }
#Anything other than a 401 Unauthorized is unexpected, attempt to handle gracefully
if ($errobject.code -ne "ISM_4001") {
Write-Host -ForegroundColor Red "Unexpected error connecting to Spark!"
Write-Host -ForegroundColor Red "$errobject"
$SparkHeaders["Authorization"] = ""
return
}
#Unuathorized response, so let's update our authorization!
if ( $SparkHeaders["Authorization"] ) {
Write-Host "Spark Authorization key expired, please update key"
}
$authKey = Read-Host "Login to Spark, open browser dev tools, and paste SID cookie here, or an API key if you have one"
if ($authKey -match "[0-9A-F](32)") {
$SparkHeaders["Authorization"] = "rest_api_key=$authKey"
} elseif ($authKey -match "$($SparkURL.split('/')[-1])#.*#") {
$SparkHeaders["Authorization"] = $authKey
} else {
Write-Host -ForegroundColor Yellow "Authorization key not a recognized key format"
$SparkHeaders["Authorization"] = ""
}
}
}
if ($(Read-Host "Enable Spark features? (y/N)") -match "^y") { Connect-ISM }
Function Get-SparkEnabled {
return -not -not $SparkHeaders["Authorization"]
}
Function Get-SparkCI($CIName) {
Connect-ISM
$uri = "$SparkURL/api/odata/businessobject/CIs`?`$filter=Name+eq+%27$CIName%27"
try{
$Query = Invoke-RestMethod -Method GET -uri $uri -headers $SparkHeaders
} catch {
$ExceptionErrors += $_.Exception.Message
}
return $Query.Value
}