From 6d5d696012b88165c14672ebcf442cfbe13b791b Mon Sep 17 00:00:00 2001 From: Liam Steckler Date: Thu, 12 Jun 2025 10:19:06 -0700 Subject: [PATCH] Allow passing connection info as string params, better instructions --- ABMPS.psd1 | 2 +- AppleBusinessManager.psm1 | 19 +++++++++++++------ README.md | 24 ++++++++++++++++++++++-- 3 files changed, 36 insertions(+), 9 deletions(-) diff --git a/ABMPS.psd1 b/ABMPS.psd1 index ba82a5c..87a1b6d 100644 --- a/ABMPS.psd1 +++ b/ABMPS.psd1 @@ -12,7 +12,7 @@ RootModule = 'AppleBusinessManager.psm1' # Version number of this module. -ModuleVersion = '1.0' +ModuleVersion = '1.1' # Supported PSEditions # CompatiblePSEditions = @() diff --git a/AppleBusinessManager.psm1 b/AppleBusinessManager.psm1 index 0f9057b..d553bed 100644 --- a/AppleBusinessManager.psm1 +++ b/AppleBusinessManager.psm1 @@ -1,13 +1,20 @@ #Requires -Version 7.0 #Requires -Module jwtPS function Connect-AppleBusinessManager { - if (-not $Env:AppleBusinessManagerClientId -or -not $Env:AppleBusinessManagerPrivateKeyId -or -not $Env:AppleBusinessManagerPrivateKey) { - throw "Client ID, Private Key ID and Private Key environment variables were not set for Apple Business Manager" + [CmdletBinding(DefaultParameterSetName = 'EnvironmentVariable')] + param( + [string][Parameter(ParameterSetName = 'PrivateKeyAsString', Mandatory)]$ClientId, + [string][Parameter(ParameterSetName = 'PrivateKeyAsString', Mandatory)]$PrivateKey, + [string][Parameter(ParameterSetName = 'PrivateKeyAsString', Mandatory)]$PrivateKeyId + ) + if ($PSCmdlet.ParameterSetName -eq 'EnvironmentVariable') { + if (-not $Env:AppleBusinessManagerClientId -or -not $Env:AppleBusinessManagerPrivateKeyId -or -not $Env:AppleBusinessManagerPrivateKey) { + throw "Client ID, Private Key ID and Private Key environment variables were not set for Apple Business Manager" + } + $Script:ClientId = $Env:AppleBusinessManagerClientId + $Script:PrivateKey = $Env:AppleBusinessManagerPrivateKey + $Script:PrivateKeyId = $Env:AppleBusinessManagerPrivateKeyId } - $Script:ClientId = $Env:AppleBusinessManagerClientId - $Script:PrivateKey = $Env:AppleBusinessManagerPrivateKey - $Script:PrivateKeyId = $Env:AppleBusinessManagerPrivateKeyId - $Header = @{ 'kid' = $Script:PrivateKeyId } diff --git a/README.md b/README.md index dc9f16b..70315bb 100644 --- a/README.md +++ b/README.md @@ -1,3 +1,23 @@ # Apple Business Manager API Module for PowerShell -To use this module, you'll need to convert the private key from Apple to a .NET friendly format: -```openssl pkcs8 -topk8 -inform PEM -outform PEM -in [path to your key from Apple] -out [path to where you want to save the converted key] -nocrypt``` \ No newline at end of file +PowerShell Module that authenticates and gets data from the Apple Business Manager APIs +## Installing the module +This module can be installed [from the PowerShell Gallery][powershell-gallery] using the following command: +```PowerShell +Install-Module -Name AMBPS +``` +## Converting your private key +The private key that you can download from the Apple Business Manager portal isn't in a format that is currently supported. To use this module, you'll need to convert it, for example, using openssl: +```shell +openssl pkcs8 -topk8 -inform PEM -outform PEM -in FROM_APPLE_PATH.pem -out CONVERTED_PATH.pem -nocrypt +``` +# Using the module +After converting, you can connect to Apple Business Manager's APIs with the following commands, then use other commands to retrieve data: +```PowerShell +$PrivateKey = (Get-Content -Path 'CONVERTED_PATH.pem') -join '' + +Connect-AppleBusinessManager -ClientId 'YOUR_CLIENT_ID' -PrivateKey $PrivateKey -PrivateKeyId 'YOUR_PRIVATE_KEY_ID' + +Get-AppleBusinessManagerOrgDevices +``` + +[powershell-gallery]: https://www.powershellgallery.com/packages/ABMPS/ \ No newline at end of file