Use [Environment]::MachineName instead of $env:COMPUTERNAME
Live kernel NetBIOS name instead of the PowerShell process-env cache. $env:COMPUTERNAME is populated when PowerShell starts and does not update if the PC gets renamed (common on Intune-managed Autopilot / AADJ devices that come up with a DESKTOP-XXXXXXXX name and get renamed by policy post-imaging). Until the next reboot, the env var stays stale while 'hostname.exe' already reports the new name. That mismatch showed up live on the first production retrofit: the status.json was written under _outputs/logs/DESKTOP-XXXXXXXX/ instead of under the device's current name, and the TargetHostnames filter and monitor drift-check would likewise see the stale name. [Environment]::MachineName reads from the kernel on each call, so it always returns the current NetBIOS name. Swapped at all five callsites in GE-Enforce.ps1, Register-GEEnforce.ps1, and Install-FromManifest.ps1. Co-Authored-By: Claude Opus 4.7 (1M context) <noreply@anthropic.com>
This commit is contained in:
@@ -375,7 +375,11 @@ function Test-PCTypeMatches {
|
||||
function Test-HostnameMatches {
|
||||
param($App)
|
||||
if (-not $App.TargetHostnames -or $App.TargetHostnames.Count -eq 0) { return $true }
|
||||
$myName = $env:COMPUTERNAME
|
||||
# [System.Environment]::MachineName reads the live NetBIOS name from the
|
||||
# kernel. $env:COMPUTERNAME is cached in the process environment at PS
|
||||
# startup and is stale after a PC rename until the next reboot - which
|
||||
# matters on Intune-managed PCs that get renamed post-imaging.
|
||||
$myName = [System.Environment]::MachineName
|
||||
foreach ($h in $App.TargetHostnames) {
|
||||
if ($h -ieq $myName) { return $true }
|
||||
if ($myName -ilike $h) { return $true } # glob patterns: WJS-*, *-SHOP-*
|
||||
@@ -405,7 +409,7 @@ foreach ($app in $config.Applications) {
|
||||
}
|
||||
|
||||
if (-not (Test-HostnameMatches -App $app)) {
|
||||
Write-InstallLog " TargetHostnames filter: entry targets $($app.TargetHostnames -join ',') but PC is $env:COMPUTERNAME - skipping"
|
||||
Write-InstallLog " TargetHostnames filter: entry targets $($app.TargetHostnames -join ',') but PC is $([System.Environment]::MachineName) - skipping"
|
||||
$pcFiltered++
|
||||
continue
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user