LabTest/Invoke-STMLabTest.ps1

<#
.SYNOPSIS
  End-to-end lab test of Intune Scope Tag Manager against a LAB tenant: every object type's write path, verify, and rollback.
 
.DESCRIPTION
  For a lab tenant only. It never runs against a tenant whose id is set in Config/Profiles/Production.json, and it needs the lab
  tenant id typed twice (-TenantId and -ConfirmLabTenant).
 
  What it does, in order:
    1. Signs in with write permissions (delegated) using the lab app registration.
    2. Runs the read-only access check and saves its report.
    3. Creates two test scope tags named STM-Lab-A-<run> and STM-Lab-B-<run>.
    4. Creates small test objects named "STM Lab Test <run>" for the object types that have a simple create call
       (skip with -NoTestObjects). Types it cannot create use one existing object of that type instead.
    5. Plan 1 adds STM-Lab-A to one object of every type (the tool's own plan, apply, verify), with the change cap overridden.
    6. Plan 2 rolls that run back with Undo-STMRun, then every object's live tags are compared with what they were at the start.
    7. Plan 3 tests Replace and Remove on the created test objects only.
    8. Deletes the test objects and the test scope tags (keep them with -KeepTestObjects).
    9. Writes lab-results.md / .json / .csv. With -UpdateRegistry, types that passed every step are marked Supported in
       Config/TypeRegistry.json.
 
  Existing objects get the test tag added and then removed again by the rollback; their other settings are never touched.
  If the run stops part way, the run folders hold rollback.csv files that put every object back.
 
.EXAMPLE
  .\LabTest\Invoke-STMLabTest.ps1 -TenantId <lab tenant id> -ConfirmLabTenant <lab tenant id> -ClientId <lab app client id>
.EXAMPLE
  .\LabTest\Invoke-STMLabTest.ps1 -TenantId <id> -ConfirmLabTenant <id> -ClientId <id> -UseDeviceCode -IncludeDevice -UpdateRegistry
#>

[CmdletBinding(SupportsShouldProcess, ConfirmImpact = 'High', DefaultParameterSetName = 'Tenant')]
param(
    [Parameter(Mandatory, ParameterSetName = 'Tenant')][string]$TenantId,
    [Parameter(Mandatory, ParameterSetName = 'Tenant')][string]$ConfirmLabTenant,
    [Parameter(Mandatory, ParameterSetName = 'Tenant')][string]$ClientId,
    [Parameter(ParameterSetName = 'Tenant')][switch]$UseDeviceCode,
    # Sign in through the web browser instead of the Windows sign-in broker (used when started from the page, with no window).
    [Parameter(ParameterSetName = 'Tenant')][switch]$BrowserSignIn,
    # Runs the whole script against the built-in simulated tenant, to check the script itself. Touches no real tenant.
    [Parameter(Mandatory, ParameterSetName = 'Demo')][switch]$Demo,
    [switch]$NoTestObjects,
    [switch]$KeepTestObjects,
    [switch]$IncludeDevice,
    [string[]]$Type,
    [string]$OutputDirectory,
    [switch]$UpdateRegistry,
    # Started from the page: the lab tenant id was already typed twice there, so do not ask again here.
    [switch]$NoPrompt
)
$ErrorActionPreference = 'Stop'
if ($NoPrompt) { $ConfirmPreference = 'None' }
$root = Split-Path -Parent $PSScriptRoot
Import-Module (Join-Path $root 'IntuneScopeTagManager.psd1') -Force

# ---- Safety: lab tenant only ----
if ($Demo) { $TenantId = '11111111-2222-4333-8444-000000000de0'; $ConfirmLabTenant = $TenantId }
if ($TenantId -ne $ConfirmLabTenant) { throw 'TenantId and ConfirmLabTenant differ. Type the lab tenant id in both.' }
# Shipped Production profile and any sign-in details this user saved for it.
$productionIds = @(& (Get-Module IntuneScopeTagManager) { Get-STMProductionTenantIds })
if ($productionIds -contains $TenantId.ToLowerInvariant()) { throw 'This tenant id is set in the Production profile. The lab test never runs there.' }

$runTag = [DateTime]::UtcNow.ToString('MMddHHmm')
if (-not $OutputDirectory) { $OutputDirectory = Join-Path (Join-Path $root 'LabTest/Results') "$runTag" }
New-Item -ItemType Directory -Path $OutputDirectory -Force | Out-Null
$log = Join-Path $OutputDirectory 'lab-test.log'
function Write-Lab { param([string]$Text) $line = "$([DateTime]::UtcNow.ToString('o')) $Text"; Write-Host $line; Add-Content -LiteralPath $log -Value $line -Encoding utf8 }
function Invoke-Lab { param([string]$Method, [string]$Path, $Body)
    $u = "https://graph.microsoft.com/beta/$Path"
    if ($Demo) {
        $json = if ($null -ne $Body) { $Body | ConvertTo-Json -Depth 20 -Compress } else { $null }
        $r = & (Get-Module IntuneScopeTagManager) { param($m, $x, $b) Invoke-STMMockRequest -Method $m -Uri $x -Body $b } $Method $u $json
        if ($r.Status -ge 400) { throw "HTTP $($r.Status): $($r.Body)" }
        if ($r.Body) { return ($r.Body | ConvertFrom-Json) } else { return $null }
    }
    if ($null -ne $Body) { Invoke-MgGraphRequest -Method $Method -Uri $u -Body ($Body | ConvertTo-Json -Depth 20) -ContentType 'application/json' -OutputType PSObject }
    else { Invoke-MgGraphRequest -Method $Method -Uri $u -OutputType PSObject }
}

if (-not $PSCmdlet.ShouldProcess("lab tenant $TenantId", 'Create test scope tags and objects, change and roll back scope tags, then clean up')) { return }

Write-Lab "Lab test $runTag starting for tenant $TenantId. Signing in; a browser sign-in may open."
if ($Demo) {
    $null = Connect-STMDemo -Mode ReadWrite -Profile Lab -DeviceCount 200 -DataRoot (Join-Path $OutputDirectory 'Data')
    $conn = [pscustomobject]@{ TenantId = $TenantId; Account = 'demo.admin@contoso.example'; MissingScopes = @() }
}
else {
    # DeviceManagementRBAC.ReadWrite.All is only for creating and deleting the run's own two test scope tags.
    $conn = Connect-STMTenant -Mode ReadWrite -TenantId $TenantId -ClientId $ClientId -Profile Lab -UseDeviceCode:$UseDeviceCode -BrowserSignIn:$BrowserSignIn -ExtraScopes 'DeviceManagementRBAC.ReadWrite.All'
}
if ($conn.TenantId -ne $TenantId) { throw "Signed in to tenant $($conn.TenantId), not $TenantId. Stopping." }
Write-Lab "Signed in as $($conn.Account). Missing permissions: $(@($conn.MissingScopes) -join ', ')"
if (@($conn.MissingScopes).Count) { Write-Warning "The lab app is missing: $(@($conn.MissingScopes) -join ', '). Some types will fail." }

$access = Test-STMAccess -OutputDirectory (Join-Path $OutputDirectory 'AccessCheck') -IncludeInventoryCsv
Write-Lab "Access check: $($access.Verdict)"

$created = [System.Collections.Generic.List[object]]::new()   # @{ Kind; Path; Id; Type }
$results = @{}                                                 # type key -> ordered result
$registry = @(Get-Content -LiteralPath (Join-Path $root 'Config/TypeRegistry.json') -Raw | ConvertFrom-Json)
if ($registry.Count -eq 1 -and $registry[0].PSObject.Properties['types']) { $registry = @($registry[0].types) }
$types = @($registry | Where-Object { (-not $Type -or $Type -contains $_.key) -and ($IncludeDevice -or $_.key -ne 'ManagedDevice') })
foreach ($t in $types) { $results[$t.key] = [ordered]@{ Type = $t.key; Label = $t.label; RegistryStatus = $t.status; Object = ''; ObjectSource = ''; AddResult = 'NotRun'; Rollback = 'NotRun'; Restored = $null; ReplaceRemove = 'NotRun'; Verdict = 'NotRun'; Detail = '' } }

try {
    # ---- 3. Test scope tags ----
    $tagA = Invoke-Lab POST 'deviceManagement/roleScopeTags' @{ displayName = "STM-Lab-A-$runTag"; description = 'Intune Scope Tag Manager lab test. Safe to delete.' }
    $created.Add(@{ Kind = 'Tag'; Path = 'deviceManagement/roleScopeTags'; Id = [string]$tagA.id })
    $tagB = Invoke-Lab POST 'deviceManagement/roleScopeTags' @{ displayName = "STM-Lab-B-$runTag"; description = 'Intune Scope Tag Manager lab test. Safe to delete.' }
    $created.Add(@{ Kind = 'Tag'; Path = 'deviceManagement/roleScopeTags'; Id = [string]$tagB.id })
    Write-Lab "Created test tags $($tagA.id) and $($tagB.id)"

    # ---- 4. Test objects ----
    $name = "STM Lab Test $runTag"
    $b64 = { param($s) [Convert]::ToBase64String([Text.Encoding]::UTF8.GetBytes($s)) }
    $recipes = [ordered]@{
        DeviceConfiguration = @{ Path = 'deviceManagement/deviceConfigurations'; Body = @{ '@odata.type' = '#microsoft.graph.windows10CustomConfiguration'; displayName = $name; omaSettings = @(@{ '@odata.type' = '#microsoft.graph.omaSettingString'; displayName = 'STM lab'; omaUri = './Vendor/MSFT/Policy/Config/Experience/AllowCortana'; value = '1' }) } }
        ConfigurationPolicy = @{ Path = 'deviceManagement/configurationPolicies'; Body = @{ name = $name; description = 'STM lab test'; platforms = 'windows10'; technologies = 'mdm'; settings = @() } }
        GroupPolicyConfiguration = @{ Path = 'deviceManagement/groupPolicyConfigurations'; Body = @{ displayName = $name; description = 'STM lab test' } }
        DeviceCompliancePolicy = @{ Path = 'deviceManagement/deviceCompliancePolicies'; Body = @{ '@odata.type' = '#microsoft.graph.windows10CompliancePolicy'; displayName = $name; scheduledActionsForRule = @(@{ ruleName = 'PasswordRequired'; scheduledActionConfigurations = @(@{ actionType = 'block'; gracePeriodHours = 0; notificationTemplateId = ''; notificationMessageCCList = @() }) }) } }
        MobileApp = @{ Path = 'deviceAppManagement/mobileApps'; Body = @{ '@odata.type' = '#microsoft.graph.webApp'; displayName = $name; publisher = 'STM lab'; appUrl = 'https://learn.microsoft.com/mem/intune/'; useManagedBrowser = $false } }
        TargetedManagedAppConfiguration = @{ Path = 'deviceAppManagement/targetedManagedAppConfigurations'; Body = @{ '@odata.type' = '#microsoft.graph.targetedManagedAppConfiguration'; displayName = $name; customSettings = @(@{ name = 'stm.lab'; value = '1' }) } }
        IOSManagedAppProtection = @{ Path = 'deviceAppManagement/iosManagedAppProtections'; Body = @{ '@odata.type' = '#microsoft.graph.iosManagedAppProtection'; displayName = $name } }
        AndroidManagedAppProtection = @{ Path = 'deviceAppManagement/androidManagedAppProtections'; Body = @{ '@odata.type' = '#microsoft.graph.androidManagedAppProtection'; displayName = $name } }
        DeviceManagementScript = @{ Path = 'deviceManagement/deviceManagementScripts'; Body = @{ displayName = $name; description = 'STM lab test'; scriptContent = (& $b64 "Write-Output 'STM lab test'"); runAsAccount = 'system'; enforceSignatureCheck = $false; fileName = 'stm-lab.ps1'; runAs32Bit = $false } }
        DeviceShellScript = @{ Path = 'deviceManagement/deviceShellScripts'; Body = @{ displayName = $name; description = 'STM lab test'; scriptContent = (& $b64 "echo 'STM lab test'"); runAsAccount = 'system'; fileName = 'stm-lab.sh'; executionFrequency = 'PT0S'; retryCount = 0; blockExecutionNotifications = $true } }
        DeviceHealthScript = @{ Path = 'deviceManagement/deviceHealthScripts'; Body = @{ displayName = $name; description = 'STM lab test'; publisher = 'STM lab'; detectionScriptContent = (& $b64 'exit 0'); remediationScriptContent = (& $b64 'exit 0'); runAsAccount = 'system'; enforceSignatureCheck = $false; runAs32Bit = $false } }
        AssignmentFilter = @{ Path = 'deviceManagement/assignmentFilters'; Body = @{ displayName = $name; description = 'STM lab test'; platform = 'windows10AndLater'; rule = '(device.deviceName -startsWith "STMLABNOMATCH")' } }
        TermsAndConditions = @{ Path = 'deviceManagement/termsAndConditions'; Body = @{ displayName = $name; title = 'STM lab test'; bodyText = 'Lab test only.'; acceptanceStatement = 'OK' } }
        NotificationMessageTemplate = @{ Path = 'deviceManagement/notificationMessageTemplates'; Body = @{ displayName = $name; defaultLocale = 'en-US'; brandingOptions = 'none' } }
        DeviceCategory = @{ Path = 'deviceManagement/deviceCategories'; Body = @{ displayName = $name; description = 'STM lab test' } }
    }
    $testObjects = @{}
    if (-not $NoTestObjects) {
        foreach ($k in $recipes.Keys) {
            if (-not $results.ContainsKey($k)) { continue }
            try {
                $o = Invoke-Lab POST $recipes[$k].Path $recipes[$k].Body
                $created.Add(@{ Kind = 'Object'; Path = $recipes[$k].Path; Id = [string]$o.id; Type = $k })
                $testObjects[$k] = [string]$o.id
                Write-Lab "Created test $k $($o.id)"
            }
            catch { $results[$k].Detail = "Could not create a test object: $($_.Exception.Message)"; Write-Lab "Could not create test $k : $($_.Exception.Message)" }
        }
    }

    # ---- 5. Choose one object per type, then add tag A ----
    [void](Update-STMInventory -Type @($types.key | Where-Object { $_ -ne 'ManagedDevice' }))
    $inv = @(Get-STMInventory)
    if ($IncludeDevice) {
        $protIds = @(@((Invoke-Lab GET 'deviceManagement/roleScopeTags').value) | Where-Object { $_.displayName -eq 'PAW-Protected' } | ForEach-Object { [string]$_.id })
        $dev = Invoke-Lab GET "deviceManagement/managedDevices?`$top=100&`$select=id,deviceName,roleScopeTagIds"
        $first = @($dev.value) | Where-Object { -not (@(@($_.roleScopeTagIds) | ForEach-Object { [string]$_ }) | Where-Object { $protIds -contains $_ }) } | Select-Object -First 1
        if ($first) { $inv += [pscustomobject]@{ Type = 'ManagedDevice'; Id = [string]$first.id; Name = [string]$first.deviceName; Supported = $true; ScopeTagIds = (@($first.roleScopeTagIds) -join ';') } }
    }
    $chosen = @{}
    foreach ($t in $types) {
        $r = $results[$t.key]
        $pick = $null
        if ($testObjects.ContainsKey($t.key)) { $pick = $inv | Where-Object { $_.Type -eq $t.key -and $_.Id -eq $testObjects[$t.key] } | Select-Object -First 1; $r.ObjectSource = 'Created test object' }
        if (-not $pick) { $pick = $inv | Where-Object { $_.Type -eq $t.key -and $_.Supported -and "$($_.ScopeTags)" -notmatch 'PAW-Protected' } | Sort-Object Name | Select-Object -First 1; $r.ObjectSource = 'Existing lab object' }
        if (-not $pick) {
            $st = $access.ObjectTypes | Where-Object Type -eq $t.key
            $r.Verdict = if ($st -and $st.Result -eq 'NotAvailable') { 'NotInTenant' } elseif ($st -and $st.Result -eq 'NoPermission') { 'NoPermission' } else { 'NoObject' }
            $r.ObjectSource = ''
            continue
        }
        $r.Object = "$($pick.Name) ($($pick.Id))"
        $chosen[$t.key] = [pscustomobject]@{ Type = $t.key; Id = $pick.Id; Before = @("$($pick.ScopeTagIds)" -split ';' | Where-Object { $_ }) }
    }
    Write-Lab "Objects chosen for $($chosen.Count) of $($types.Count) types"

    $ops = @(foreach ($c in $chosen.Values) { [pscustomobject]@{ Type = $c.Type; Id = $c.Id; Mode = 'Add'; Tags = @([string]$tagA.id); Note = "Lab test $runTag"; Source = 'labtest' } })
    if (-not $ops.Count) { throw 'No objects to test.' }
    $plan1 = New-STMChangePlan -Operations $ops -Title "Lab test ${runTag}: add test tag" -OverrideCap -OverrideReason 'Lab test' -OutputDirectory (Join-Path $OutputDirectory 'Plan1')
    foreach ($i in @($plan1.Items | Where-Object State -ne 'Ready')) { $results[$i.Type].AddResult = "Plan: $($i.State)"; $results[$i.Type].Detail = (@($i.Issues) + @($i.Warnings) -join ' ') }
    $run1 = Invoke-STMChangePlan -Plan $plan1 -Confirm:$false
    Write-Lab "Run 1 $($run1.RunId): $(($run1.Counts.GetEnumerator() | ForEach-Object { "$($_.Key)=$($_.Value)" }) -join ' ')"
    foreach ($res in @($run1.Results)) { $results[$res.Type].AddResult = $res.Result; if ($res.Detail) { $results[$res.Type].Detail = $res.Detail } }

    # ---- 6. Roll back and compare live tags with the start ----
    $applied = @($run1.Results | Where-Object { $_.Result -in 'Applied', 'AppliedUnverified' })
    if ($applied.Count) {
        $plan2 = Undo-STMRun -RunId $run1.RunId
        $run2 = Invoke-STMChangePlan -Plan $plan2 -Confirm:$false
        Write-Lab "Run 2 (rollback) $($run2.RunId): $(($run2.Counts.GetEnumerator() | ForEach-Object { "$($_.Key)=$($_.Value)" }) -join ' ')"
        foreach ($res in @($run2.Results)) { $results[$res.Type].Rollback = $res.Result }
    }
    Start-Sleep -Seconds 5
    [void](Update-STMInventory -Type @($chosen.Keys | Where-Object { $_ -ne 'ManagedDevice' }))
    $after = @{}; foreach ($o in @(Get-STMInventory)) { $after["$($o.Type)|$($o.Id)"] = @("$($o.ScopeTagIds)" -split ';' | Where-Object { $_ }) }
    if ($IncludeDevice -and $chosen.ContainsKey('ManagedDevice')) {
        $d = Invoke-Lab GET "deviceManagement/managedDevices/$($chosen.ManagedDevice.Id)?`$select=id,roleScopeTagIds"
        $after["ManagedDevice|$($chosen.ManagedDevice.Id)"] = @(@($d.roleScopeTagIds) | ForEach-Object { [string]$_ })
    }
    foreach ($c in $chosen.Values) {
        $now = $after["$($c.Type)|$($c.Id)"]
        $norm = { param($a) (@($a | Where-Object { $_ -ne '0' }) | Sort-Object) -join ';' }
        $results[$c.Type].Restored = ($null -ne $now) -and ((& $norm $now) -eq (& $norm $c.Before))
    }

    # ---- 7. Replace and Remove on created test objects only ----
    $own = @($chosen.Values | Where-Object { $testObjects.ContainsKey($_.Type) -and $results[$_.Type].AddResult -eq 'Applied' })
    if ($own.Count) {
        $ops3 = @(foreach ($c in $own) {
            [pscustomobject]@{ Type = $c.Type; Id = $c.Id; Mode = 'Replace'; Tags = @([string]$tagA.id, [string]$tagB.id); Note = 'Lab replace'; Source = 'labtest' }
            [pscustomobject]@{ Type = $c.Type; Id = $c.Id; Mode = 'Remove'; Tags = @([string]$tagA.id); Note = 'Lab remove'; Source = 'labtest' }
        })
        $plan3 = New-STMChangePlan -Operations $ops3 -Title "Lab test ${runTag}: replace and remove" -OverrideCap -OverrideReason 'Lab test'
        $run3 = Invoke-STMChangePlan -Plan $plan3 -Confirm:$false
        Write-Lab "Run 3 (replace/remove) $($run3.RunId): $(($run3.Counts.GetEnumerator() | ForEach-Object { "$($_.Key)=$($_.Value)" }) -join ' ')"
        foreach ($res in @($run3.Results)) { $results[$res.Type].ReplaceRemove = $res.Result }
    }
}
catch { Write-Lab "STOPPED: $($_.Exception.Message)"; Write-Warning "Lab test stopped: $($_.Exception.Message). Run folders keep rollback.csv files." }
finally {
    # ---- 8. Clean up (objects first, then tags) ----
    if (-not $KeepTestObjects) {
        foreach ($c in @($created | Where-Object Kind -eq 'Object')) {
            try { Invoke-Lab DELETE "$($c.Path)/$($c.Id)" | Out-Null; Write-Lab "Deleted test $($c.Type) $($c.Id)" }
            catch { Write-Lab "Could not delete test $($c.Type) $($c.Id): $($_.Exception.Message)" }
        }
        foreach ($c in @($created | Where-Object Kind -eq 'Tag')) {
            try { Invoke-Lab DELETE "$($c.Path)/$($c.Id)" | Out-Null; Write-Lab "Deleted test tag $($c.Id)" }
            catch { Write-Lab "Could not delete test tag $($c.Id) (an object may still carry it): $($_.Exception.Message)" }
        }
    }
}

# ---- 9. Verdicts and report ----
foreach ($r in $results.Values) {
    if ($r.Verdict -ne 'NotRun') { continue }
    $ok = $r.AddResult -eq 'Applied' -and $r.Rollback -eq 'Applied' -and $r.Restored -eq $true
    $r.Verdict = if ("$($r.AddResult)" -like 'Plan:*') { 'BlockedByPlan' } elseif ($ok -and $r.ReplaceRemove -in 'Applied', 'NotRun') { 'Verified' } elseif ($r.AddResult -eq 'AppliedUnverified' -or $r.Rollback -eq 'AppliedUnverified') { 'NeedsRecheck' } else { 'Failed' }
}
$rows = @($results.Values | ForEach-Object { [pscustomobject]$_ } | Sort-Object Label)
$rows | Export-Csv -LiteralPath (Join-Path $OutputDirectory 'lab-results.csv') -NoTypeInformation -Encoding utf8
$rows | ConvertTo-Json -Depth 5 | Set-Content -LiteralPath (Join-Path $OutputDirectory 'lab-results.json') -Encoding utf8
$md = [System.Collections.Generic.List[string]]::new()
$md.Add("# Intune Scope Tag Manager lab test $runTag"); $md.Add('')
$md.Add("- Tenant: $TenantId"); $md.Add("- Signed in as: $($conn.Account)"); $md.Add("- Finished: $([DateTime]::UtcNow.ToString('o')) (UTC)")
foreach ($v in 'Verified', 'Failed', 'BlockedByPlan', 'NeedsRecheck', 'NoObject', 'NotInTenant', 'NoPermission') { $md.Add("- ${v}: $(@($rows | Where-Object Verdict -eq $v).Count)") }
$md.Add(''); $md.Add('| Object type | Verdict | Object used | Add | Rollback | Restored | Replace+Remove | Detail |'); $md.Add('|---|---|---|---|---|---|---|---|')
foreach ($r in $rows) { $md.Add("| $($r.Label) | $($r.Verdict) | $($r.ObjectSource) | $($r.AddResult) | $($r.Rollback) | $($r.Restored) | $($r.ReplaceRemove) | $(([string]$r.Detail) -replace '\|', '/') |") }
$md -join [Environment]::NewLine | Set-Content -LiteralPath (Join-Path $OutputDirectory 'lab-results.md') -Encoding utf8

if ($UpdateRegistry) {
    $path = Join-Path $root 'Config/TypeRegistry.json'
    $raw = Get-Content -LiteralPath $path -Raw
    $changed = 0
    foreach ($r in @($rows | Where-Object { $_.Verdict -eq 'Verified' -and $_.RegistryStatus -eq 'LabVerify' })) {
        $pattern = '("key"\s*:\s*"' + [regex]::Escape($r.Type) + '"[^{}]*?"status"\s*:\s*)"LabVerify"'
        $new = [regex]::Replace($raw, $pattern, '$1"Supported"')
        if ($new -ne $raw) { $raw = $new; $changed++ }
    }
    if ($changed) { Set-Content -LiteralPath $path -Value $raw -Encoding utf8 -NoNewline; Write-Lab "Marked $changed type(s) Supported in TypeRegistry.json" }
}
Write-Lab "Lab test finished. Results: $OutputDirectory"
Get-Content -LiteralPath (Join-Path $OutputDirectory 'lab-results.md')