Intune 混合 - 使用 GraphAPI 和 MSAL 重命名设备。我已经尝试了带有 MSAL 库的 PowerShell 脚本,该库使用 Oauth 访问令牌将 GraphAPI 连接到我的订阅,并使用 $graphResponse = Invoke-RestMethod @graphRequestParams -Method。重命名命令到达 Intune 门户,设备保持重命名保持挂起状态大约 4 小时,但它失败。在 AD 站点上,我已经完成了对计算机 OU 的授权以允许重命名。这是没有变量赋值的代码部分。
while($infoCorrect -ne "y"){
$oldDeviceName = Read-Host "Please enter the devices name.."
$newDeviceName = Read-Host "Please enter the devices new name.."
Write-Host "Old name: $oldDeviceName" -ForegroundColor Red
Write-Host "New name: $newDeviceName" -ForegroundColor Green
$infoCorrect = (Read-Host "Enter 'y' if the info looks correct").ToLower()
}
$infoCorrect = $null
#graph api token request
$authParams = @{
ClientId = ''
TenantId = $TenantId
DeviceCode = $true
}
#check if auth var already set.
#if expired, clear the var "Clear-Variable -name "auth" or $auth = $null
if(!$auth){
$auth = Get-MsalToken @authParams
}
enter code here
#invoke restmethod params for Graph API
$graphRequestParams = @{
Uri = $graphUri
Authentication = 'OAuth'
Token = ($auth.AccessToken | ConvertTo-SecureString -AsPlainText -Force)
ContentType = 'Application/Json'
}
#endregion vars
#region Get devices
$graphResponse = Invoke-RestMethod @graphRequestParams -Method "GET"
#paging in case over 1000 items returned
if($graphResponse.'@odata.nextlink'){
$responseValue = $graphResponse.value
while($graphResponse.'@odata.nextlink'){
$graphRequestParams.Uri = $graphResponse.'@odata.nextLink'
$graphResponse = Invoke-RestMethod @graphRequestParams
$responseValue += $graphResponse.value
}
}
else{
$responseValue += $graphResponse.value
}
#endregion get devices
#region Filter for device
#filter for device
$device = $responseValue | where {($_.deviceName -eq $oldDeviceName) } -ErrorAction SilentlyContinue
#endregion
#region rename device
if($device){
$intuneDeviceId = $device.id
Write-Host "Old Name: $oldDeviceName" -ForegroundColor Red
Write-Host "New Name: $newDeviceName" -ForegroundColor Green
Write-Host "Device ID: $intuneDeviceId" -ForegroundColor Yellow
$graphRequestBody = @{ "deviceName" = $newDeviceName }
#$graphRequestParams.uri = $($("$graphUri)('$intuneDeviceId')/setDeviceName")
$graphRequestParams.uri = "$graphUri('$intuneDeviceId')/setDeviceName"
Write-Host "Updating Intune Device $($device.deviceName) to $($graphRequestBody.deviceName)..." -ForegroundColor Green
Invoke-RestMethod @graphRequestParams -Body ($graphRequestBody | ConvertTo-Json) -Method "POST"
}
else{
Write-Host "Could not find $oldDeviceName" -ForegroundColor Red
}
$device = $null