# 探测 Siemens NX 的 NXBIN 目录 (含 run_journal.exe), 供启动脚本调用。 # 用法: # powershell -NoProfile -ExecutionPolicy Bypass -File tools\find-nxbin.ps1 # 输出 NXBIN 绝对路径 # powershell ... -File tools\find-nxbin.ps1 -Quiet # 只输出路径, 失败无输出 # powershell ... -File tools\find-nxbin.ps1 -Explain # 输出探测过程 # # 探测顺序 (先到先用): # 1. 环境变量 ONEBOT_NXBIN —— 手动指定优先, 便于自定义安装路径 # 2. 项目内 nxbin.txt (单行路径) —— 便于把结果固化到部署机器 # 3. C:\Program Files\Siemens\NX*\NXBIN —— 官方默认安装位置, 多版本取版本号最大者 # # 退出码: 0 = 找到 (stdout 为路径); 1 = 未找到 (stdout 为空) param( [switch]$Quiet, [switch]$Explain ) $ErrorActionPreference = 'SilentlyContinue' function Say($m) { if (-not $Quiet) { Write-Output $m } } function Test-NxBin($dir) { if ([string]::IsNullOrWhiteSpace($dir)) { return $false } return Test-Path (Join-Path $dir 'run_journal.exe') } # 1) 环境变量 $env_dir = $env:ONEBOT_NXBIN if ($env_dir) { Say ("[1] 环境变量 ONEBOT_NXBIN = " + $env_dir) if (Test-NxBin $env_dir) { Write-Output $env_dir; exit 0 } Say " 该目录下没有 run_journal.exe, 继续探测" } else { Say "[1] 环境变量 ONEBOT_NXBIN 未设置" } # 2) 项目内 nxbin.txt $root = Split-Path $PSScriptRoot -Parent $cfg = Join-Path $root 'nxbin.txt' if (Test-Path $cfg) { $line = (Get-Content $cfg -TotalCount 1) if ($line) { $line = $line.Trim() } Say ("[2] nxbin.txt = " + $line) if (Test-NxBin $line) { Write-Output $line; exit 0 } Say " 该目录下没有 run_journal.exe, 继续探测" } else { Say "[2] 无 nxbin.txt" } # 3) 扫描官方安装位置 (多版本取版本号最大的) $roots = @( (Join-Path ${env:ProgramFiles} 'Siemens'), (Join-Path ${env:ProgramFiles(x86)} 'Siemens'), 'C:\Program Files\Siemens' ) | Where-Object { $_ -and (Test-Path $_) } | Select-Object -Unique $cands = @() foreach ($r in $roots) { Say ("[3] 扫描 " + $r) foreach ($ver in (Get-ChildItem $r -Directory -Filter 'NX*' -ErrorAction SilentlyContinue)) { $bin = Join-Path $ver.FullName 'NXBIN' if (Test-NxBin $bin) { $cands += $bin Say (" 找到 " + $bin) } } } if ($cands.Count -gt 0) { # 按 NX 后面的版本号排序取最大 (如 NX2506 < NX2512); 无版本号的排最后 $best = $cands | Sort-Object { $m = [regex]::Match($_, 'NX(\d+)') if ($m.Success) { [long]$m.Groups[1].Value } else { 0 } } -Descending | Select-Object -First 1 Write-Output $best exit 0 } Say "未找到 NX (含 run_journal.exe 的 NXBIN 目录)" Say "可手动指定: set ONEBOT_NXBIN=D:\path\to\NXBIN 或 写一行到 nxbin.txt" exit 1