-
Notifications
You must be signed in to change notification settings - Fork 0
/
Set-JCDisplayName.ps1
79 lines (61 loc) · 2.67 KB
/
Set-JCDisplayName.ps1
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
# -----------------------------------------------------------------------------
# Script: Set-JCDisplayName.ps1
# Version: 1.0.0
# Author: Shawn Song
# Reference:
# - https://community.jumpcloud.com/t5/community-scripts/powershell-add-the-systems-to-a-system-group-depends-on-where/m-p/1733#M172
#
# Notes: Don't run this on all devices unless you are 100% sure about the impact!!
# Requirements:
# - The latest JumpCloud PowerShell Module. https://jumpcloud.com/support/install-the-jumpcloud-powershell-module
# - PowerShell 7 and above versions.
# - JumpCloud API keys for both Manager & Read-only roles.
# -----------------------------------------------------------------------------
# Connect to your JC Tenant - Manager role is good enough!
Connect-JCOnline -JumpCloudApiKey $env:JCRW # Strongly suggest storing the API key in the system env variable,
$jcSystems = "system_ID01","system_ID02"
# Don't run this on all devices unless you are 100% sure about the impact!!
#$jcSystems = (Get-JCSystem -returnProperties osFamily | where {($_.osFamily -ne "ios") -and ($_.osFamily -ne "android")})._id # ruling out the mobile devices
# Determine the machine type
function Get-MachineType {
param (
[parameter(Mandatory=$true)]
[string]$systemID
)
$hasBattery = Get-JCSystemInsights -Table Battery -SystemId $systemID
$type = "LT"
if ($null -eq $hasBattery ) {
$type = "DT"
}
return $type
}
# Get the SN (and cap at 10 char) as part of the hostname
function Get-MachineSN{
param (
[Int32]$snCharLimit = 12, # 12 is the hostname hard limit for Windows
[parameter(Mandatory=$true)]
[string]$systemID
)
$SN = (Get-JCSystemInsights -Table SystemInfo -id $systemID | select HardwareSerial).HardwareSerial
if (($SN.Length -gt $snCharLimit) -and ($null -ne $SN)){
$SN = $SN.trim() -replace " ",'' -replace '-','' -replace '\r?\n\z' # removing whitespaces
$SN = $SN.Substring(0,$snCharLimit)
}
return $SN
}
# Create a new device group to gather these systems together
$newGrounName = "NewHostName"
$ng = Get-JCGroup -Type System -name $newGrounName -ErrorAction SilentlyContinue
if ($null -eq $ng){
$ng = New-JCSystemGroup -GroupName "NewHostName"
}
# Executing the change
foreach ($s in $jcSystems){
$displayName = (Get-MachineType -systemID $s) +'-' + (Get-MachineSN -systemID $s)
# Changing the displayname on JC
Write-Host "new name for $s will change to: $displayname"
Set-JCSystem -displayName $displayName
# Add to the system group
write-host "adding $displayname to group $($ng.name)"
Add-JCSystemGroupMember -GroupID $ng.id -SystemID $s
}