You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

请求编写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]::Size is a reliable way to detect architecture: it returns 4 for 32-bit processes and 8 for 64-bit processes.
  • Using throw ensures 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

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.05.14 07:57:40