How to securely acquire a secret key from within Powershell required for the installation of the JumpCloud agent (in various scenarios including MDT)
Challenge:
I was looking for a secure option to acquire a the connect key for the JumpCloud agent during a Task Sequence with MDT while imaging a Windows10/11 with a custom ISO.
During the Task Sequence , the JumpCloud agent installer requires a connect key which needs to be passed as a parameter. As I don't want to hardcode or store the connect key anywhere on the image within scripts or by using half-baked obfuscation techniques, I needed an approach which allows me to authenticate as a authorised administrator (or user).
There were of course several approaches to it. A simple one could have been to halt the Task Sequence, login to the JumpCloud Console, copy the key, and then paste it over to the installer and proceed - too many clicks and a little cumbersome as well because the Task Sequence is usually running in a low resolution. I considered options like password-protecting the installer including the connect key, but these methods are rather antiquated and do seldom cater for similar scenarios on deployed and provisioned systems as they often trigger false positives with EDR-solutions in place. I had to do some more research and start exploring vaulting solutions such as Vault by Hashicorp , AWS Secrets Manager or Azure Key Vault . All were considered here as they do have Powershell-modules available . I did choose Azure Key Vault as I didn't have to sign-up for anything new and I'm using Azure for other purposes as well.
Requirements:
Solution:
Note: This is rather a quick solution right now and not perfect, but it's confirmed working
Once you've setup your Key Vault on Azure, you're ready to consume the secret within scripts:
This script which will be used within the Task Sequence (more about that later) doesn't require a change of the Execution-Policy (this is by default elevated in a TS):
Connect-AzAccount -Tenant 'xxxx-xxxx-xxxx-xxxx' -SubscriptionId 'yyyy-yyyy-yyyy-yyyy
Account? ? ? ? ? ? ? ? SubscriptionName TenantId? ? ? ? ? ? ? ? Environment
-------? ? ? ? ? ? ? ? ---------------- --------? ? ? ? ? ? ? ? -----------
[email protected]? Subscription1? ? xxxx-xxxx-xxxx-xxxx? ? ?AzureCloud'
领英推荐
Install-Module -Name Az.KeyVault -Force
Import-Module Az.KeyVault
Connect-AzAccount
$key = Get-AzKeyVaultSecret -VaultName 'MDT-creds' -Name 'JumpCloud-Connect-Key' -AsPlainText
cd $env:temp | Invoke-Expression; Invoke-RestMethod -Method Get -URI https://raw.githubusercontent.com/TheJumpCloud/support/master/scripts/windows/InstallWindowsAgent.ps1 -OutFile InstallWindowsAgent.ps1 | Invoke-Expression; ./InstallWindowsAgent.ps1 -JumpCloudConnectKey $key?
To run this script successfully within a Task Sequence you will need to make sure that the newly imaged installation has internet access. In some cases you need to ingest drivers before or give windows some time to detect the hardware and get the connection up.
There are multiple ways to ensure a connection before running the script, a simple one is to wait for a connection by using
do
?$ping = test-connection -comp 8.8.8.8 -count 1 -Quiet
} until ($ping){
...as this will halt the execution until 8.8.8.8 was pinged successfully.
Usually you will run this script as one of the last steps within a Task Sequence under the section 'State Restore':
Once the script was successfully executed, the agent will register the newly imaged machine as a new device on the JumpCloud Administrator Console:
Notes:
Thanks for reading!
Technology Executive // Engineer, Researcher, Builder, Breaker, Dreamer // he/him
2 年Great job on the article!
Tech Enthusiast
2 年Awesome, nice article Pak.