请求编写32位PowerShell执行报错、仅支持64位的脚本
PowerShell Script to Restrict Execution to 64-bit Environment
Here's a straightforward script that enforces execution only in 64-bit PowerShell, and throws a clear, actionable error if run in 32-bit:
# Detect the underlying PowerShell architecture $ptrSize = [IntPtr]::Size if ($ptrSize -eq 4) { # Throw a terminating error for 32-bit environments throw "Error: This script requires 64-bit PowerShell. Please launch the 64-bit version (usually located at `C:\Windows\System32\WindowsPowerShell\v1.0\powershell.exe`) and try again." } elseif ($ptrSize -eq 8) { # Your 64-bit-only script logic goes here Write-Host "Success: Running in 64-bit PowerShell. Executing main workflow..." # Example 64-bit specific operation (replace with your actual code) # Get-ItemProperty -Path "HKLM:\Software\Your64BitOnlyKey" } else { # Fallback for unexpected architecture scenarios throw "Error: Unsupported PowerShell architecture detected. This script only runs on 64-bit systems." }
Key Details:
[IntPtr]::Sizeis a reliable way to detect architecture: it returns4for 32-bit processes and8for 64-bit processes.- Using
throwensures the script stops immediately in 32-bit mode—no unintended partial execution. - The error message includes a specific path to the 64-bit PowerShell executable to help users quickly resolve the issue.
Customization Tips:
- Tweak the error message to match your script's purpose (e.g., mention specific 64-bit dependencies).
- Replace the example code in the 64-bit block with your actual workflow that requires 64-bit access.
内容的提问来源于stack exchange,提问作者user2256009
相关产品推荐
相关产品推荐

