-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathBatchRenameWindowsBySerial.ps1
58 lines (45 loc) · 1.99 KB
/
BatchRenameWindowsBySerial.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
<#
Version: 1.1
Author: Jay Williams
Script: BatchRenameWindows.ps1
Description:
Uses Graph API to rename devices by serial numbers in a CSV.
Permissions needed are DeviceManagementManagedDevices.Read.All and DeviceManagementManagedDevices.PriviligedOperations.All.
The script is provided "AS IS" with no warranties.
#>
$deviceName = "" #Can use {{rand:x}} or {{serialnumber}}
$csvPath = ""
$tenentId = ""
$clientid = ""
$redirectURI = ""
$serialNumbers = Import-Csv -Path $csvPath
$Token = Get-MsalToken -ClientId $clientid -TenantId $tenentId -Interactive -RedirectUri $redirectURI
# Gets deviceId by filtering serialNumber
$restResponses = @()
$deviceIds = @()
foreach ($serialNumber in $serialNumbers) {
$apiUrl = "https://graph.microsoft.com/beta/deviceManagement/managedDevices?filter=serialnumber eq '"+$serialNumber.serialNumber+"'"
$restResponse = Invoke-RestMethod -Headers @{Authorization = "Bearer $($Token.AccessToken)"} -Uri $apiUrl -Method Get
$restResponses += @($restResponse)
}
# Loops through $deviceIds array limited to 100 devices at a time
$deviceIds = $restResponses.value.id
$apiUrl = "https://graph.microsoft.com/beta/deviceManagement/managedDevices/executeAction"
for ($i = 0; $i -ile $deviceIds.Count; $i++) {
$deviceNameValue = @{}
foreach ($deviceId in $deviceIds[$i..($i+99)]) {
$deviceNameValue.Add($deviceId, $deviceName)
$body = @{
deviceName = $deviceNameValue | ConvertTo-Json -Compress
platform = "windows"
restartNow = $False
actionName = "setDeviceName"
action = "setDeviceName"
deviceIds = $deviceIds[$i..($i+99)]
realAction = "setDeviceName"
}
}
$bodyJson = $body | ConvertTo-Json -Compress
$restPost = Invoke-RestMethod -Headers @{Authorization = "Bearer $($Token.AccessToken)"} -Uri $apiUrl -Method Post -Body $bodyJson -ContentType 'application/json'
$i+=99
}