Forum Discussion
Jaime_IT
Dec 21, 2023Brass Contributor
Device Bulk Rename Struggle
We've struggled with this for quite some time now. What we need to do is, discover all Intune devices with one of the following elements true: 10.1.x.x IP address 10.111.x.x IP address Then re...
Jaime_IT
Dec 21, 2023Brass Contributor
Here is the script so far:
# Define App Registration details
$appId = "appId"
$tenantId = "tenantId"
$clientSecret = "clientSecret"
$resource = "https://graph.microsoft.com/"
# Get OAuth token
$tokenUrl = "https://login.microsoftonline.com/$tenantId/oauth2/v2.0/token"
$body = @{
grant_type = "client_credentials"
client_id = $appId
client_secret = $clientSecret
scope = "https://graph.microsoft.com/.default"
}
$Response = Invoke-RestMethod -Uri $tokenUrl -Method Post -Body $Body
$token = $Response.access_token
# Define the CSV file path
$csvPath = "H:\IT\Jaime\IntuneDevices_ja4t3.csv"
# Define the API URL to get Intune managed devices
$apiUrl = "https://graph.microsoft.com/beta/deviceManagement/managedDevices"
# Initialize an array to hold the devices info
$devicesInfo = @()
# Call the API and filter the devices
do {
$response = Invoke-RestMethod -Headers @{Authorization = "Bearer $token"} -Uri $apiUrl
foreach ($device in $response.value) {
if ($device.deviceName -like "laptop-*") {
$hardwareInfo = Invoke-RestMethod -Headers @{Authorization = "Bearer $token"} -Uri "https://graph.microsoft.com/beta/deviceManagement/manageddevices"
$devicesInfo += [PSCustomObject]@{
DeviceName = $device.deviceName
SerialNumber = $device.serialNumber.PadLeft(9, '0')
User = $device.usersLoggedOn[-1].userId
WiFiIPv4 = $device.hardwareInformation.ipAddressV4
WiredIPv4 = $device.hardwareInformation.wiredIPv4Addresses
}
}
}
$apiUrl = $response.'@odata.nextLink'
} while ($apiUrl)
# Export the devices info to a CSV file
$devicesInfo | Export-Csv -Path $csvPath -NoTypeInformation