cancel
Showing results for 
Search instead for 
Did you mean: 

Windows: Command for bound username+serial (or something else) to hostname script

JuergenKlaassen
Rising Star II
Rising Star II

Hi

I came across a question yesterday to have @Fulgubbe 's script for Windows as well.
So i gave it a shot right now to construct a similar (not equivalent) approach for Windows-devices:

# JC API Key
$JCAPIKEY = '<YOUR API KEY HERE>'

# Install and import the JumpCloud Powershell Module
Write-Host "Installing the JumpCloud PowerShell Module" -BackgroundColor Black -ForegroundColor Cyan
Install-Module -Name JumpCloud -Force

# Connect to the JumpCloud API
Write-Host "Connecting to the JumpCloud API" -BackgroundColor Black -ForegroundColor Cyan
Connect-JCOnline $JCAPIKEY

# Acquire SystemID from jcagent.conf
Write-Host "Acquiring the System ID" -BackgroundColor Black -ForegroundColor Cyan
$agentconf = Get-Content "C:\Program Files (x86)\JumpCloud\Plugins\Contrib\jcagent.conf" | ConvertFrom-Json
$agentconf.systemKey

# Get the system and systemuser objects
$system = Get-JCSystem -SystemID $agentconf.systemKey
$systemuser = Get-JCSystemUser -SystemID $agentconf.systemKey
$system.serialNumber
$systemuser.Username

# Create the hostname
$hostname = $systemuser.Username+"-"+$system.serialNumber
$hostname

# Set the hostname
Write-Host "Setting the hostname" -BackgroundColor Black -ForegroundColor Cyan

#Set-ComputerName -ComputerName $hostname
Set-JCSystem $agentconf.systemKey -displayName $hostname

One important note: 
Not reflected in this script yet, but I'd recommend to have the API-key stored in a file attached to the command and then read it from there for execution. By having it in a file attached, it won't surface in the Command Results post execution and therefore limits the risk of potential exposure. Important then is - of course - also to remove the file from the host at the end of the script. 

Have a great weekend ahead
- Juergen

3 REPLIES 3

ncarmichael
Rising Star I

This great Juergen 🙂

Most Windows machines will be 64-bit know so it would be worth having the script also check "C:\Program Files\JumpCloud\Plugins\Contrib\jcagent.conf" too

 

Keep up the good work,

Neil

Nepalas
Novitiate I

I fount this problemetic as it wont work and it throws that "WARNING: The installed version of PowerShell does not support Parallel functionality. Consider up
dating to PowerShell 7"

 

windows 11 + ps 7.3.6

as well it throws back that the api is "Unauthorized: api key user not found"

Nepalas
Novitiate I

I was able to fix and run this one

please notice that windows restrict names over 15 characters

 

#Open as Ps7
pwsh

# JC API Key
$JCAPIKEY = 'YOUE APIKEY HERE'

# Install and import the JumpCloud Powershell Module
Write-Host "Installing the JumpCloud PowerShell Module" -BackgroundColor Black -ForegroundColor Cyan
Install-Module -Name JumpCloud -Force

# Connect to the JumpCloud API
Write-Host "Connecting to the JumpCloud API" -BackgroundColor Black -ForegroundColor Cyan
Connect-JCOnline $JCAPIKEY

# Acquire SystemID from jcagent.conf
Write-Host "Acquiring the System ID" -BackgroundColor Black -ForegroundColor Cyan
$agentconf = Get-Content "C:\Program Files\JumpCloud\Plugins\Contrib\jcagent.conf" | ConvertFrom-Json
$agentconf.systemKey

# Get the system and systemuser objects
$system = Get-JCSystem -SystemID $agentconf.systemKey
$systemuser = Get-JCSystemUser -SystemID $agentconf.systemKey
$systemuser.Username

# Create the hostname
$hostname = $systemuser.Username + '-win'

# Set the hostname
Write-Host "Setting the hostname" -BackgroundColor Black -ForegroundColor Cyan

#Set-ComputerName -ComputerName $hostname
Set-JCSystem $agentconf.systemKey -displayName $hostname

 

You Might Like

New to the site? Take a look at these additional resources:

Community created scripts:

Our new Radical Admin blog:

Keep up with Product News:

Read our community guidelines

Ready to join us? You can register here.