Last active
March 13, 2020 05:26
-
-
Save HawaiiRyan/9a740aded3015302382089d7b122e672 to your computer and use it in GitHub Desktop.
This file contains 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
#Set Verbosity Preference | |
$VerbosePreference = "Continue" | |
#Require TLS 1.2 for communication | |
[Net.ServicePointManager]::SecurityProtocol = | |
[Net.SecurityProtocolType]::Tls12 | |
#Workspace ONE Environment Address, Authentication, and Headers | |
$addy = "https://ws1.ryanpringnitz.com" | |
$b64 = Get-content -Path "c:\intAppDelivery\b64.txt" -ErrorAction Continue ####Update the -path with the location where the base64 encoded credentials can be obtained | |
$encodedString = "Basic " + $b64 | |
$apiKey = Get-content -Path "c:\intAppDelivery\api.txt"-ErrorAction Continue ####Update the -path with the location where the API key can be obtained | |
$useJSON = "application/json" | |
$headers = @{"Authorization" = $encodedString; "aw-tenant-code" = $apiKey; "Accept" = $useJSON; "Content-Type" = $useJSON} | |
$statuscode = "202" | |
try { | |
Write-Verbose -Message "Setting up HTTP session" | |
#Get public Chrome app from Workspace ONE API | |
$ServicePoint = [System.Net.ServicePointManager]::FindServicePoint($addy) | |
Write-Verbose -Message "Successfully opened service point connection" | |
$devid = Invoke-RestMethod -Uri "$addy/API/mam/apps/public/223/devices" -Headers $headers | |
$ServicePoint.CloseConnectionGroup("") | |
} catch { | |
$statuscode = $_.Exception.Response.StatusCode.value_ | |
$devid = $_.Exception.Response.GetResponseStream() | |
$reader = New-Object System.IO.StreamReader($devid) | |
$reader.BaseStream.Position = 0 | |
$reader.DiscardBufferedData() | |
$responseBody = $reader.ReadToEnd(); | |
} | |
if ($statuscode -ne "202") { | |
Write-Verbose -Message "Problem in API call, $statuscode received" | |
Write-Verbose -Message "$responseBody" | |
Break | |
} | |
$fixdevice = $devid.DeviceId | |
ForEach($id in $fixdevice){ | |
$devicebody=@" | |
{"DeviceId": $id} | |
"@ | |
$statuscode = "202" | |
try { | |
$ServicePoint = [System.Net.ServicePointManager]::FindServicePoint($addy) | |
Invoke-RestMethod -Method Post -Uri "$addy/API/mam/apps/public/223/install" -Headers $headers -Body $devicebody | |
$ServicePoint.CloseConnectionGroup("") | |
} | |
catch | |
{ | |
$statuscode = $_.Exception.Response.StatusCode.value_ | |
$devid = $_.Exception.Response.GetResponseStream() | |
$reader = New-Object System.IO.StreamReader($devid) | |
$reader.BaseStream.Position = 0 | |
$reader.DiscardBufferedData() | |
$responseBody = $reader.ReadToEnd(); | |
if ($statuscode -ne "202") { | |
Write-Verbose -Message "Problem in API call, $statuscode received" | |
Write-Verbose -Message "$responseBody" | |
#Break | |
#Un-comment the line above to stop script if HTTP response of 202 not received it didn't work | |
} | |
} | |
} | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment