Skip to content

Instantly share code, notes, and snippets.

@sandytsang
Created November 26, 2019 00:40
Show Gist options
  • Select an option

  • Save sandytsang/13ebfcbcfb7c3a8f43c30c4ca209bd5a to your computer and use it in GitHub Desktop.

Select an option

Save sandytsang/13ebfcbcfb7c3a8f43c30c4ca209bd5a to your computer and use it in GitHub Desktop.
#Set executionPolicy
Set-ExecutionPolicy -ExecutionPolicy Bypass -Scope Process -Force
#Install or update Microsoft Graph Intune module
try {
Write-Verbose -Message "Attempting to locate Microsoft.Graph.Intune module on local system"
$GraphModule = Get-Module -Name "Microsoft.Graph.Intune" -ListAvailable -ErrorAction Stop -Verbose:$false
if ($GraphModule -ne $null) {
Write-Verbose -Message "Microsoft.Graph.Intune module detected, checking for latest version"
$LatestModuleVersion = (Find-Module -Name Microsoft.Graph.Intune -ErrorAction Stop -Verbose:$false).Version
if ($LatestModuleVersion -gt $GraphModule.Version) {
Write-Verbose -Message "Latest version of Microsoft.Graph.Intune module is not installed, attempting to install: $($LatestModuleVersion.ToString())"
$UpdateModuleInvocation = Update-Module -Name Microsoft.Graph.Intune -Scope AllUsers -Force -ErrorAction Stop -Confirm:$false -Verbose:$false
}
}
}
catch [System.Exception] {
Write-Warning -Message "Unable to detect Microsoft.Graph.Intune module, attempting to install from online repository"
try {
# Install NuGet package provider
$PackageProvider = Install-PackageProvider -Name NuGet -Force -Verbose:$false
# Install Microsoft.Graph.Intune module
Install-Module -Name Microsoft.Graph.Intune -Scope AllUsers -Force -ErrorAction Stop -Confirm:$false -Verbose:$false
Write-Verbose -Message "Successfully installed Microsoft.Graph.Intune"
}
catch [System.Exception] {
Write-Warning -Message "An error occurred while attempting to install Microsoft.Graph.Intune module. Error message: $($_.Exception.Message)"; break
}
}
#Get device serial number
#$Serial = (Get-WmiObject -Class Win32_BIOS).SerialNumber
#Device Serial number, change this!
$Serial = "PC03SVKGXXX"
$Tenants = @("tenant1.onmicrosoft.com", "tenant2.onmicrosoft.com", "tenant3.onmicrosoft.com") #List here your tenants
$AppID = "10f9be91-4c9b-4427-aa71-143d18abafb5" #Change this to your own app ID
$AppSecret = "vJctTjssIeidhuzNQDkfWUIF/Pe+ae+adfafafafaf" #Change this to your own App Secret
foreach ($Tenant in $Tenants) {
Write-Host "checking from tenant : $Tenant" -ForegroundColor Yellow
#Update Intune Graph Schema to beta, because Autopilot graph is in beta Schema, update
Update-MSGraphEnvironment -SchemaVersion "beta" -AppId "$AppID" -AuthUrl "https://login.microsoftonline.com/$Tenant" -Quiet
#Connect to Graph with App Secret
Connect-MSGraph -ClientSecret "$AppSecret" -Quiet
#URL for query autopilot device, always use filter, this moment only support contains as filter
$url = "https://graph.microsoft.com/beta/deviceManagement/windowsAutopilotDeviceIdentities?`$filter=contains(serialNumber,'$Serial')"
$AutopilotDevice = Invoke-MSGraphRequest -HttpMethod GET -Url $url -ErrorAction Continue
if ($AutopilotDevice.'@odata.count' -eq "1")
{
write-host "found device serial $Serial in tenant $Tenant" -ForegroundColor Green
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment