Skip to content

Commit 26229c9

Browse files
authored
Merge pull request #30 from fadwen/feat/native-64-bit-default
feat(win32): default -Architecture to the device's 64-bit host
2 parents 664b64a + 8b4dccd commit 26229c9

15 files changed

Lines changed: 213 additions & 31 deletions

‎CHANGELOG.md‎

Lines changed: 20 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,26 @@ release notes.
1111

1212
## [Unreleased]
1313

14-
Nothing yet.
14+
### Changed
15+
16+
- `Invoke-IntuneDetectionTest`, `Invoke-IntuneRequirementTest` and `Invoke-IntuneWin32AppTest` no
17+
longer default `-Architecture` to `x64`, which Windows on ARM refuses because there is no x64
18+
host there. Left out, the architecture is the device's 64-bit host: `x64` on an x64 device,
19+
`arm64` on Windows on ARM, the host the agent uses for Win32 detection and requirement scripts
20+
with "run as 32-bit" off. `-Architecture x64` and `arm64` still name one host each and are
21+
still refused on the other CPU; the result's `Architecture` says which host ran.
22+
23+
### Fixed
24+
25+
- A Win32 install or uninstall command named as a bare batch file (`install.cmd`) failed with
26+
"'install.cmd' is not recognized" from `Invoke-IntuneWin32AppTest` on a machine where
27+
`NoDefaultCurrentDirectoryInExePath` is set in the caller's environment, a per-user shell
28+
setting that stops `cmd.exe` looking in its working folder. The agent's `cmd.exe` runs without
29+
the caller's profile, so the harness now starts its child processes without the variable.
30+
- `Invoke-IntuneWin32AppTest` warned about the 32-bit host only when `powershell` appeared on the
31+
install or uninstall command line. A `powershell.exe` call inside the batch file the command
32+
line names (`install.cmd`) runs 32-bit just the same, from the agent's 32-bit `cmd.exe`, and now
33+
gets the same warning, naming the file and line.
1534

1635
## [0.29.0] - 2026-10-07
1736

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
function Get-IslHostArchitecture {
2+
<#
3+
.SYNOPSIS
4+
The architecture of this device's 64-bit Windows PowerShell host: x64, or arm64 on Windows on ARM.
5+
6+
.DESCRIPTION
7+
The agent runs Win32 detection and requirement scripts in the device's 64-bit host unless
8+
the rule's "run as 32-bit" option is on. On Windows on ARM that host is the native ARM64
9+
one and there is no x64 PowerShell at all (Findings, "Windows on ARM"), so the harness
10+
commands use this as their default architecture instead of a fixed x64 that an ARM64
11+
device refuses. The 32-bit host (x86) exists on both and is never the answer here.
12+
13+
.EXAMPLE
14+
Get-IslHostArchitecture
15+
16+
x64 on an x64 device, arm64 on a Windows on ARM device.
17+
18+
.OUTPUTS
19+
System.String. x64 or arm64, as Get-IslHostPath and the -Architecture parameters name them.
20+
#>
21+
[CmdletBinding()]
22+
[OutputType([string])]
23+
param()
24+
25+
$osArchitecture = "$([System.Runtime.InteropServices.RuntimeInformation]::OSArchitecture)"
26+
if ($osArchitecture -eq 'Arm64') { 'arm64' } else { 'x64' }
27+
}

‎Private/Invoke-IslProcess.ps1‎

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -85,6 +85,10 @@
8585
if ($desktopModulePath) { $startInfo.Environment['PSModulePath'] = $desktopModulePath }
8686
else { $null = $startInfo.Environment.Remove('PSModulePath') }
8787
}
88+
# With NoDefaultCurrentDirectoryInExePath set in the caller's own environment, cmd.exe does
89+
# not look in its working folder, and a bare 'install.cmd' fails with "not recognized". The
90+
# agent's cmd.exe runs without the caller's profile, so the child runs without the setting
91+
$null = $startInfo.Environment.Remove('NoDefaultCurrentDirectoryInExePath')
8892

8993
$process = [System.Diagnostics.Process]::new()
9094
$process.StartInfo = $startInfo

‎Public/Invoke-IntuneDetectionTest.ps1‎

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,8 +11,9 @@ function Invoke-IntuneDetectionTest {
1111
[Alias('FullName', 'PSPath')]
1212
[string]$Path,
1313

14+
# Left out: the device's 64-bit host (x64, or arm64 on Windows on ARM), the agent's default
1415
[ValidateSet('x86', 'x64', 'arm64')]
15-
[string]$Architecture = 'x64',
16+
[string]$Architecture,
1617

1718
[ValidateSet('User', 'System')]
1819
[string]$Context = 'User',
@@ -35,6 +36,9 @@ function Invoke-IntuneDetectionTest {
3536
throw '-Credential applies to -Context User; System runs as NT AUTHORITY\SYSTEM'
3637
}
3738
Write-Verbose "Starting $($MyInvocation.MyCommand.Name) for $($PSBoundParameters.Keys -join ', ')"
39+
# The agent's default host is the device's 64-bit one: arm64 on Windows on ARM, where no x64
40+
# host exists, and x64 elsewhere
41+
if (-not $Architecture) { $Architecture = Get-IslHostArchitecture }
3842

3943
$reasons = [System.Collections.Generic.List[string]]::new()
4044
$signatureStatus = ''

‎Public/Invoke-IntuneRequirementTest.ps1‎

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,8 +22,9 @@ function Invoke-IntuneRequirementTest {
2222
[AllowEmptyString()]
2323
[string]$Value,
2424

25+
# Left out: the device's 64-bit host (x64, or arm64 on Windows on ARM), the agent's default
2526
[ValidateSet('x86', 'x64', 'arm64')]
26-
[string]$Architecture = 'x64',
27+
[string]$Architecture,
2728

2829
[ValidateSet('User', 'System')]
2930
[string]$Context = 'User',
@@ -41,6 +42,9 @@ function Invoke-IntuneRequirementTest {
4142

4243
process {
4344
Write-Verbose "Starting $($MyInvocation.MyCommand.Name) for $($PSBoundParameters.Keys -join ', ')"
45+
# The agent's default host is the device's 64-bit one: arm64 on Windows on ARM, where no x64
46+
# host exists, and x64 elsewhere
47+
if (-not $Architecture) { $Architecture = Get-IslHostArchitecture }
4448

4549
$scriptRunSplat = @{
4650
Path = $Path

‎Public/Invoke-IntuneWin32AppTest.ps1‎

Lines changed: 25 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -33,8 +33,9 @@ function Invoke-IntuneWin32AppTest {
3333
# uninstalled before this app installs, an update target stays (W32-SUP-OLD-A, W32-SUP-OLD-B)
3434
[hashtable[]]$Supersedes,
3535

36+
# Left out: the device's 64-bit host (x64, or arm64 on Windows on ARM), the agent's default
3637
[ValidateSet('x86', 'x64', 'arm64')]
37-
[string]$Architecture = 'x64',
38+
[string]$Architecture,
3839

3940
[ValidateSet('User', 'System')]
4041
[string]$Context = 'User',
@@ -64,6 +65,9 @@ function Invoke-IntuneWin32AppTest {
6465
[switch]$EnforceSignatureCheck
6566
)
6667
Write-Verbose "Starting $($MyInvocation.MyCommand.Name) for $($PSBoundParameters.Keys -join ', ')"
68+
# The agent's default host is the device's 64-bit one: arm64 on Windows on ARM, where no x64
69+
# host exists, and x64 elsewhere
70+
if (-not $Architecture) { $Architecture = Get-IslHostArchitecture }
6771

6872
if (-not $DetectionPath -and -not $DetectionRule) {
6973
throw 'Give a detection script (-DetectionPath), detection rules (-DetectionRule), or both'
@@ -132,11 +136,29 @@ function Invoke-IntuneWin32AppTest {
132136
# cmd.exe, as the agent's own 32-bit process does
133137
function Invoke-ContentCommand {
134138
param([string]$Phase, [string]$Content, [string]$CommandLine)
135-
if ($CommandLine -match '(?i)(^|[\s"])powershell(\.exe)?([\s"]|$)') {
139+
$source = (Resolve-Path -LiteralPath $Content).ProviderPath
140+
$hostPattern = '(?i)(^|[\s"])powershell(\.exe)?([\s"]|$)'
141+
if ($CommandLine -match $hostPattern) {
136142
$warnings.Add("powershell.exe in the $Phase command runs the 32-bit host (the agent is a 32-bit " +
137143
'process); HKLM:\SOFTWARE and Program Files are redirected there')
138144
}
139-
$source = (Resolve-Path -LiteralPath $Content).ProviderPath
145+
else {
146+
# The same host hides inside a batch file the command line names: install.cmd calling
147+
# powershell.exe runs it 32-bit too, from the agent's 32-bit cmd.exe, without the
148+
# command line showing it
149+
$first = if ($CommandLine -match '^\s*"([^"]+)"') { $Matches[1] }
150+
else { ($CommandLine.Trim() -split '\s+', 2)[0] }
151+
$batch = if ($first -match '(?i)\.(cmd|bat)$') { Join-Path -Path $source -ChildPath $first }
152+
else { $null }
153+
if ($batch -and (Test-Path -LiteralPath $batch -PathType Leaf)) {
154+
$hit = Select-String -LiteralPath $batch -Pattern $hostPattern | Select-Object -First 1
155+
if ($hit) {
156+
$warnings.Add("powershell.exe in $first (line $($hit.LineNumber)), which the $Phase " +
157+
'command runs through the 32-bit cmd.exe, is the 32-bit host (the agent is a 32-bit ' +
158+
'process); HKLM:\SOFTWARE and Program Files are redirected there')
159+
}
160+
}
161+
}
140162
$runId = [guid]::NewGuid().ToString('N')
141163
# Another account cannot reach the caller's temp folder; its copy lives under ProgramData
142164
$cache = if ($Credential) {
Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
#Requires -Modules @{ ModuleName = 'Pester'; ModuleVersion = '6.2.0' }
2+
3+
<#
4+
The default architecture of the Win32 harness commands: the 64-bit host this device has,
5+
which Get-IslHostPath resolves to System32 rather than refusing.
6+
#>
7+
8+
BeforeAll {
9+
$script:ModuleRoot = Split-Path -Parent (Split-Path -Parent (Split-Path -Parent $PSScriptRoot))
10+
Import-Module (Join-Path $script:ModuleRoot 'IntuneScriptLab.psd1') -Force
11+
$osArchitecture = "$([System.Runtime.InteropServices.RuntimeInformation]::OSArchitecture)"
12+
$script:Native = if ($osArchitecture -eq 'Arm64') { 'arm64' } else { 'x64' }
13+
}
14+
15+
AfterAll {
16+
Remove-Module IntuneScriptLab -Force -ErrorAction SilentlyContinue
17+
}
18+
19+
Describe 'Get-IslHostArchitecture' -Tag 'Unit', 'Private' {
20+
21+
It 'names the 64-bit host this device has' {
22+
InModuleScope IntuneScriptLab { Get-IslHostArchitecture } | Should-Be $script:Native
23+
}
24+
25+
It 'names a host Get-IslHostPath resolves instead of refusing' {
26+
$hostInfo = InModuleScope IntuneScriptLab { Get-IslHostPath -Architecture (Get-IslHostArchitecture) }
27+
$hostInfo.Path | Should-BeLikeString '*\WindowsPowerShell\v1.0\powershell.exe'
28+
$hostInfo.Path | Should-NotBeLikeString '*SysWOW64*'
29+
}
30+
}

‎Tests/Unit/Private/Invoke-IslProcess.Tests.ps1‎

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -47,6 +47,26 @@ Describe 'Invoke-IslProcess' -Tag 'Unit', 'Private' {
4747
$result.StdOut.Trim() | Should-Be $TestDrive
4848
}
4949

50+
It 'lets cmd.exe find a batch file in the working folder whatever the caller''s own shell setting' {
51+
# NoDefaultCurrentDirectoryInExePath in the caller's environment stops cmd.exe looking in
52+
# the working folder; the agent's cmd.exe has no such setting, so the child must not either
53+
Set-Content -Path (Join-Path $TestDrive 'install.cmd') -Value '@echo ran' -Encoding ascii
54+
$saved = $env:NoDefaultCurrentDirectoryInExePath
55+
$env:NoDefaultCurrentDirectoryInExePath = '1'
56+
try {
57+
$result = Invoke-Process @{
58+
FilePath = $script:Cmd; Arguments = '/C "install.cmd"'
59+
WorkingDirectory = $TestDrive; WorkFolder = $TestDrive
60+
}
61+
}
62+
finally {
63+
$env:NoDefaultCurrentDirectoryInExePath = $saved
64+
}
65+
$result.ExitCode | Should-Be 0
66+
$result.StdOut.Trim() | Should-Be 'ran'
67+
$result.StdErr.Trim() | Should-Be ''
68+
}
69+
5070
It 'closes stdin so a read returns instead of waiting' {
5171
$result = Invoke-Process @{
5272
FilePath = $script:Cmd; Arguments = '/C set /p answer=name? & echo [%answer%]'

‎Tests/Unit/Public/Invoke-IntuneDetectionTest.Tests.ps1‎

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,8 @@ BeforeAll {
1111
Import-Module (Join-Path $script:ModuleRoot 'IntuneScriptLab.psd1') -Force
1212
. (Join-Path $script:ModuleRoot 'Tests\TestHelpers\TestHelpers.ps1')
1313
$script:Detect = 'C:\lab\detect.ps1'
14+
$osArchitecture = "$([System.Runtime.InteropServices.RuntimeInformation]::OSArchitecture)"
15+
$script:Native = if ($osArchitecture -eq 'Arm64') { 'arm64' } else { 'x64' }
1416
}
1517

1618
AfterAll {
@@ -98,6 +100,17 @@ Describe 'Invoke-IntuneDetectionTest' -Tag 'Unit', 'Public' {
98100
$result.Architecture | Should-Be 'x86'
99101
$result.Context | Should-Be 'User'
100102
}
103+
104+
It 'defaults to the 64-bit host this device has, the one the agent uses for Win32 detection' {
105+
Mock Invoke-IslScriptRun -ModuleName IntuneScriptLab {
106+
[pscustomobject]@{ ExitCode = 0; TimedOut = $false; StdOut = 'x'; StdErr = '' }
107+
}
108+
$result = Invoke-IntuneDetectionTest -Path $script:Detect
109+
Should-Invoke Invoke-IslScriptRun -ModuleName IntuneScriptLab -Exactly -Times 1 -ParameterFilter {
110+
$Architecture -in 'x64', 'arm64'
111+
}
112+
$result.Architecture | Should-Be $script:Native
113+
}
101114
}
102115

103116
Context 'Enforced signature check (W32-DET-SIGCHECK)' {

‎Tests/Unit/Public/Invoke-IntuneRequirementTest.Tests.ps1‎

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,8 @@ BeforeAll {
1111
Import-Module (Join-Path $script:ModuleRoot 'IntuneScriptLab.psd1') -Force
1212
. (Join-Path $script:ModuleRoot 'Tests\TestHelpers\TestHelpers.ps1')
1313
$script:Script = 'C:\lab\requirement.ps1'
14+
$osArchitecture = "$([System.Runtime.InteropServices.RuntimeInformation]::OSArchitecture)"
15+
$script:Native = if ($osArchitecture -eq 'Arm64') { 'arm64' } else { 'x64' }
1416
}
1517

1618
AfterAll {
@@ -40,16 +42,16 @@ Describe 'Invoke-IntuneRequirementTest' -Tag 'Unit', 'Public' {
4042
$results.Applicable | Should-All { $_ }
4143
}
4244

43-
It 'defaults to the 64-bit host, as the portal does for requirement rules' {
45+
It 'defaults to the 64-bit host this device has, as the portal does for requirement rules' {
4446
Mock Invoke-IslScriptRun -ModuleName IntuneScriptLab {
4547
[pscustomobject]@{ ExitCode = 0; TimedOut = $false; StdOut = "ok`r`n"; StdErr = '' }
4648
}
4749
$result = Invoke-IntuneRequirementTest -Path $script:Script -OutputType String -Value 'ok'
48-
$result.Architecture | Should-Be 'x64'
50+
$result.Architecture | Should-Be $script:Native
4951
$result.Context | Should-Be 'User'
5052
$result.Operator | Should-Be 'Equal'
5153
Should-Invoke Invoke-IslScriptRun -ModuleName IntuneScriptLab -Exactly -Times 1 -ParameterFilter {
52-
$Architecture -eq 'x64' -and $Phase -eq 'requirement'
54+
$Architecture -in 'x64', 'arm64' -and $Phase -eq 'requirement'
5355
}
5456
}
5557
}

0 commit comments

Comments
 (0)