Scripts/Intune.ps1



Set-ExecutionPolicy -ExecutionPolicy Unrestricted -Scope Process -Verbose -Force
Install-PackageProvider -Name NuGet -MinimumVersion 2.8.5.201 -Force -Verbose
Set-PSRepository PSGallery -InstallationPolicy Trusted -Verbose
Install-Module PowershellGet -Force -Verbose
Install-Script -Name Get-WindowsAutoPilotInfo -Force -Confirm:$false -Verbose

$Serialnumber = Get-CimInstance win32_bios | Select-Object -ExpandProperty SerialNumber
$OutputFile = "C:\$($Serialnumber).csv"

. "C:\Program Files\WindowsPowerShell\Scripts\Get-WindowsAutopilotInfo.ps1" -OutputFile $OutputFile -Verbose

Get-Content $OutputFile -Verbose






intunemanagementextension://syncapp





# Sync all devices

$Devices = Get-MgBetaDeviceManagementManagedDevice -All
$Devices | ForEach-Object -ThrottleLimit 10 -Parallel {
    Write-Host -ForegroundColor Yellow "Syncing $($_.id) - $($_.DeviceName)"
    Sync-MgBetaDeviceManagementManagedDevice -ManagedDeviceId $_.id
}

while ($true) {
    (Get-Date).DateTime
    Get-MgBetaDeviceManagementManagedDevice -All | ForEach-Object -ThrottleLimit 10 -Parallel {
        Write-Host -ForegroundColor Yellow "Syncing $($_.id) - $($_.DeviceName)"
        Sync-MgBetaDeviceManagementManagedDevice -ManagedDeviceId $_.id
    }
    (Get-Date).DateTime
    Start-Sleep 30
}


# Device compliance

$MgDeviceManagementManagedDevice = Get-MgBetaDeviceManagementManagedDevice -All
$NonCompliantDevices = $MgDeviceManagementManagedDevice | Where-Object ComplianceState -NE "compliant" #| Where-Object LastSyncDateTime -GT ((Get-Date).AddDays(-30))

$Devices = @()

$NonCompliantDevices | ForEach-Object {
    $obj = [PSCustomObject]@{
        ID                  = $_.Id
        DeviceName          = $_.DeviceName
        ComplianceState     = $_.ComplianceState
        NonCompliantSetting = (Get-MgBetaDeviceManagementManagedDeviceNonCompliantSetting -ManagedDeviceId $_.id -ea 0 | Select-Object -Unique Setting).Setting -join "`n"
        LastSyncDateTime    = $_.LastSyncDateTime
        EnrolledDateTime    = $_.EnrolledDateTime
        Model               = $_.Model
        OSVersion           = $_.OSVersion
        UserPrincipalName   = $_.UserPrincipalName
    }
    $Devices += $obj
}

$Devices


# Primary user

$Devices | Where-Object UserPrincipalName -EQ "admin@test.nl" | ForEach-Object {

    $uri = "https://graph.microsoft.com/beta/deviceManagement/managedDevices('$($_.Id)')/users"
    $primaryUser = Invoke-MgGraphRequest -Uri $uri -Method Get -Verbose:$false
    $primaryUser
    # $primaryUser.value.userPrincipalName


    $DeleteUri = "https://graph.microsoft.com/beta/deviceManagement/managedDevices('$($_.Id)')/users/`$ref"
    Write-Output "Device: $($_.DeviceName) - Removing primary user: $($primaryUser.value.userPrincipalName)"
    Invoke-MgGraphRequest -Uri $DeleteUri -Method DELETE

}


# App versions

$Apps = Get-MgDeviceAppManagementMobileApp -All
$Apps = $Apps | Where-Object { ($_.AdditionalProperties."@odata.type" -ne "#microsoft.graph.managedIOSStoreApp") -and ($_.AdditionalProperties."@odata.type" -ne "#microsoft.graph.managedAndroidStoreApp") }
$Apps | Select-Object id, DisplayName


# Apps


$AllApps = Get-MgDeviceAppManagementMobileApp -All

$AllApps | Format-Table

$AllApps = $AllApps | Where-Object { ($_.AdditionalProperties."@odata.type" -ne "#microsoft.graph.managedIOSStoreApp") -and ($_.AdditionalProperties."@odata.type" -ne "#microsoft.graph.managedAndroidStoreApp") }
$AllApps = $AllApps | Where-Object { ($_.AdditionalProperties.'@odata.type' -EQ "#microsoft.graph.iosVppApp") }

$AllApps | Format-Table

$SelectedApps = $AllApps | Select-Object '@odata.type', id, displayName, publisher | Out-GridView -OutputMode Multiple -Title "Select Apps"

$SelectedApps | Format-Table

# New-MgDeviceAppManagementMobileAppAssignment -MobileAppId -Intent available -



Invoke-GraphRequest -Uri "https://graph.microsoft.com/beta/deviceManagement/managedDevices/" | Select-Object -ExpandProperty value | ForEach-Object {
    # Write-Host $_.id
    Invoke-GraphRequest -Uri "https://graph.microsoft.com/beta/deviceManagement/managedDevices/$($_.id)" | Select-Object deviceName, model, chassisType
} | Format-Table -AutoSize




$result = @()
Get-MgBetaDeviceManagementManagedDevice -All -Filter "(OperatingSystem eq 'Windows')" | ForEach-Object {
    $thisDevice = Get-MgBetaDeviceManagementManagedDevice -ManagedDeviceId $_.Id -Property HardwareInformation
    $obj = [PSCustomObject]@{
        DeviceName        = $_.DeviceName
        TotalStorageSpace = ($thisDevice.HardwareInformation.TotalStorageSpace / 1024 / 1024 / 1024)
        FreeStorageSpace  = ($thisDevice.HardwareInformation.FreeStorageSpace / 1024 / 1024 / 1024)
    }
    $result += $obj
}
$result


##############


$AP = Get-MgDeviceManagementWindowAutopilotDeviceIdentity -All
$AP.Count
$AP | Where-Object { $_.Grouptag -ne "Shared" -and $_.Grouptag -ne "Personal" }
($AP | Where-Object Model -EQ "HP ProBook 450 G8 Notebook PC").Count
($AP | Where-Object Model -EQ "HP ProBook 440 G8 Notebook PC").Count
($AP | Where-Object Model -EQ "HP Elite Mini 600 G9 Desktop PC").Count

($AP | Where-Object DeploymentProfileAssignedDateTime -GT (Get-Date).AddDays(-1) ).count


$MD = Get-MgDeviceManagementManagedDevice -All -Filter "OperatingSystem eq 'Windows'"
$MD.Count
($MD | Where-Object Model -EQ "HP ProBook 450 G8 Notebook PC").Count
($MD | Where-Object Model -EQ "HP ProBook 440 G8 Notebook PC").Count
($MD | Where-Object Model -EQ "HP Elite Mini 600 G9 Desktop PC").Count




#


Sync-MgBetaDeviceManagementWindowsAutopilotSetting


#