Functions/Get-IntuneAssignmentReport.ps1


function Get-IntuneAssignmentReport {
    [CmdletBinding()]
    param (
        [Parameter(Mandatory)] [ArgumentCompleter( {
                param ( $commandName, $parameterName, $wordToComplete, $commandAst, $fakeBoundParameters )
                Get-ChildItem "$($env:OneDrive)\Klanten\$wordToComplete*" -Directory | Select-Object Name | Sort-Object Name | ForEach-Object { "`"$($_.Name)`"" }
            })]
        [string] $CustCode,
        [Parameter()] [ValidateSet("Windows", "iOS", "Android")] [string] $OS,
        [Parameter()] [switch] $OpenWhenDone
    )

    $ErrorActionPreference = "stop"

    $FileName = "Inventarisatie Intune $(Get-Date -Format 'yyyy-MM-dd HHmm').xlsx"
    $OutPath = "$($env:OneDrive)\Klanten\$($CustCode)"
    $FilePath = Join-Path $OutPath -ChildPath $FileName

    if (!(Test-Path $OutPath)) { New-Item $OutPath -Type Directory -Confirm:$false }

    $Param = @{}

    if($OS) {
        $Param.Add("OS", $OS)
    }

    $Report = [ordered]@{}
    if ((!$OS) -or ($Param.OS -eq "Windows")) {
        Write-Host "Processing Autopilot..." -ForegroundColor Yellow
        $Report.Autopilot = Get-IntuneAutopilotAssignmentReport
    }
    Write-Host "Processing Apps..." -ForegroundColor Yellow
    $Report.Apps = Get-IntuneMobileAppAssignmentReport @Param
    Write-Host "Processing Configs..." -ForegroundColor Yellow
    $Report.Configs = Get-IntuneDeviceConfigurationPolicyAssignmentReport @Param
    if ((!$OS) -or ($Param.OS -eq "Windows")) {
        Write-Host "Processing Scripts..." -ForegroundColor Yellow
        $Report.Scripts = Get-IntuneDeviceManagementScriptsAssignmentReport
        Write-Host "Processing Remediation Scripts..." -ForegroundColor Yellow
        $Report.RemediationScripts = Get-IntuneRemediationScriptAssignmentReport
    }

    $Report.GetEnumerator() | ForEach-Object {
        $_.Value | Export-Excel -Path $FilePath -WorksheetName $_.Name -FreezeTopRowFirstColumn -AutoSize -AutoFilter -TableStyle Medium6
    }


    if ($OpenWhenDone) {
        Start-Process $FilePath
    }


    # =COUNTIF(C2:G2; "*")

}