Using PowerShell on macOS

PowerShell. That thing that replaced the Command terminal (or if you're old enough to remember, DOS) on Windows, is actually cross platform and can be run on a number of other O/S's including macOS.

If you're a macOS user and you work with customers who primarily use Windows, PowerShell is a great way to build out script that they can use and is very useful when it comes to using API's.

I've done a lot of work with the Workspace ONE API's in the past and thought it it would be worth making a post on how to use PowerShell on your mac as well as some samples that you can use to call the Workspace ONE API's

X Code Install (Required for Homebrew)

Terminal command:

Xcode-select –install

(If prompted, install the Command Line Developer tools)


Homebrew Install (Required to Install PowerShell)

Terminal Command (To download Homebrew): 

curl -fsSL -o install.sh https://raw.githubusercontent.com/Homebrew/install/master/install.sh


Terminal Command (To installs Homebrew):

/bin/bash install.sh


Terminal Command (Confirm Homebrew correctly installed)

Brew doctor


PowerShell Install

Terminal Command (To install PowerShell)

brew install --cask powershell


Terminal Command (To confirm PowerShell is installed)

PWSH

Graphical user interface, text, application, email

Description automatically generated

With PowerShell installed, the next step would be using an IDE to actually use it. You could just run everything from the pwsh window, but that is no fun. Download Microsoft Visual Studio Code for macOS from here: https://code.visualstudio.com/docs/setup/mac

Open a new text file in VS Code and select PowerShell as the language and you're ready to go!

Some sample PS1 scripts for using with accessing the WS1 UEM API's:

Basic auth (not recommended, but sometimes you have to use it)

$UserName = ""
$Password = ""
$Text = $UserName + ":" + $Password
$Bytes = [System.Text.Encoding]::UTF8.GetBytes($Text)
$EncodedText =[Convert]::ToBase64String($Bytes)
try
{
   $headers = New-Object "System.Collections.Generic.Dictionary[[String],[String]]"
   $headers.Add("Authorization", 'Basic ' + $EncodedText)
   $headers.Add("Accept", 'application/json')
   $headers.Add("content-type", 'application/json')
   $URI = "https://cn1688.awmdm.com/AirWatch/ApnsApplications/TestApnsHttp2Connection?CertificateId=8424436"
   $response = Invoke-WebRequest -Uri $URI -Headers $headers -Method get
   $response
}
catch
{
   Write-Host "StatusCode: $_.Exception.Response.StatusCode.value__ "
   Write-Host "StatusDescription: $_.Exception.Response.StatusDescription"
   Write-Host $_.Exception.message
}

oAuth (Recommended. Needs to setup an oAuth client ID in WS1 UEM to use this)

$client_id = "" 
$client_secret = ""

$acces_token_url = "https://na.uemauth.vmwservices.com/connect/token"

try
{

$oAuthbody = @{ 
    grant_type = "client_credentials" 
    client_id = $client_id 
    client_secret = $client_secret 
}

   $response = Invoke-WebRequest -Method Post -Uri $acces_token_url -Body $oAuthbody 
   $oAuthToken = $response | ConvertFrom-Json 
   $oAuthToken = $oAuthToken.access_token

   $headers = New-Object "System.Collections.Generic.Dictionary[[String],[String]]"
   $headers.Add("Authorization", "Bearer $oAuthToken")
   $headers.Add("Accept", 'application/json')
   $headers.Add("content-type", 'application/json')

   $URI = "https://cn1688.awmdm.com/AirWatch/ApnsApplications/TestApnsHttp2Connection?CertificateId=8424436"
   
   $response = Invoke-WebRequest -Uri $URI -Headers $headers -Method get

   $response

}
catch
{
   Write-Host "StatusCode: $_.Exception.Response.StatusCode.value__ "
   Write-Host "StatusDescription: $_.Exception.Response.StatusDescription"
   Write-Host $_.Exception.message
}

Comments

Popular posts from this blog

Windows device lifecycle. Hands-off reprovisioning with Workspace ONE.

Deploying certificates to the Quest 2

Adobe Acrobat Reader customizations for Windows