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

如何在PowerShell中运行指定的cmd命令(FireEye xagt工具)

问题:在PowerShell中运行FireEye xagt命令失败

原CMD命令

可在cmd.exe中正常执行的命令:

"C:\Program Files (x86)\FireEye\xagt\xagt.exe" -g "C:\tmp\xagt_%computername%_%date:~-4,4%%date:~-10,2%%date:~-7,2%_%time:~0,2%%time:~3,2%%time:~6,2%.txt"

尝试的PowerShell方法及报错

方法1:调用运算符&

$cmd = 'C:\Program Files (x86)\FireEye\xagt\xagt.exe'
$arg1 = '-g C:\tmp\xagt_%computername%_%date:~-4,4%%date:~-10,2%%date:~-7,2%_%time:~0,2%%time:~3,2%%time:~6,2%.txt'
& $cmd $arg1

执行未成功,PowerShell无法解析CMD风格的变量语法

方法2:Start-Process

Start-Process -FilePath "xagt.exe" -WorkingDirectory "C:\Program Files (x86)\FireEye\xagt" -ArgumentList "-g "C:\tmp\xagt_%computername%_%date:~-4,4%%date:~-10,2%%date:~-7,2%_%time:~0,2%%time:~3,2%%time:~6,2%.txt""

语法错误,ArgumentList中引号嵌套方式不正确

方法3:错误使用调用运算符&

& 'xagt.exe -g "C:\tmp\xagt_%computername%_%date:~-4,4%%date:~-10,2%%date:~-7,2%_%time:~0,2%%time:~3,2%%time:~6,2%.txt"'

报错信息:

& : The term 'xagt.exe -g "C:\tmp\xagt_%computername%_%date:~-4,4%%date:~-10,2%%date:~-7,2%_%time:~0,2%%time:~3,2%%time:~6,2%.txt"' is not recognized as the name of a cmdlet, function, script file, or operable program. Check the spelling of the name, or if a path was included, verify that the path is correct and try again.
At line:1 char:3
+ & 'xagt.exe -g "C:\tmp\xagt_%computername%_%date:~-4,4%%date:~-10,2%% ...
+   ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
    + CategoryInfo          : ObjectNotFound: (xagt.exe -g "C:...time:~6,2%.txt":String) [], CommandNotFoundException
    + FullyQualifiedErrorId : CommandNotFoundException

方法4:Invoke-Command

Invoke-Command -ScriptBlock {
        xagt.exe -g "C:\tmp\xagt_%computername%_%date:~-4,4%%date:~-10,2%%date:~-7,2%_%time:~0,2%%time:~3,2%%time:~6%.txt"
}

报错信息:

xagt.exe : The term 'xagt.exe' is not recognized as the name of a cmdlet, function, script file, or operable program.
Check the spelling of the name, or if a path was included, verify that the path is correct and try again.
At line:2 char:8
+        xagt.exe -g "C:\tmp\xagt_%computername%_%date:~-4,4%%date:~-10 ...
+        ~~~~~~~~
    + CategoryInfo          : ObjectNotFound: (xagt.exe:String) [], CommandNotFoundException
    + FullyQualifiedErrorId : CommandNotFoundException

问题分析与解决方法

核心问题

  1. 语法差异:%computername%、%date%、%time%是CMD的环境变量和字符串截取语法,PowerShell不识别,需改用PowerShell的变量和日期格式化方式。
  2. 参数传递错误:调用外部程序时,参数需拆分传递,不能将所有参数合并为一个字符串;引号嵌套需用转义或变量替代。
  3. 路径问题:Invoke-Command的脚本块不会自动继承当前工作目录,需指定程序完整路径。

正确实现方式

方式1:调用运算符&(推荐)

# 生成带时间戳的文件名
$timestamp = Get-Date -Format "yyyyMMdd_HHmmss"
$outputFile = "C:\tmp\xagt_$($env:COMPUTERNAME)_$timestamp.txt"

# 执行命令
& "C:\Program Files (x86)\FireEye\xagt\xagt.exe" -g $outputFile

方式2:Start-Process

$timestamp = Get-Date -Format "yyyyMMdd_HHmmss"
$outputFile = "C:\tmp\xagt_$($env:COMPUTERNAME)_$timestamp.txt"

# 启动程序,-NoNewWindow避免新开窗口,-Wait等待执行完成
Start-Process -FilePath "C:\Program Files (x86)\FireEye\xagt\xagt.exe" -ArgumentList "-g", $outputFile -NoNewWindow -Wait

方式3:Invoke-Command(本地执行)

Invoke-Command -ScriptBlock {
    $timestamp = Get-Date -Format "yyyyMMdd_HHmmss"
    $outputFile = "C:\tmp\xagt_$($env:COMPUTERNAME)_$timestamp.txt"
    & "C:\Program Files (x86)\FireEye\xagt\xagt.exe" -g $outputFile
}

内容的提问来源于stack exchange,提问作者user3737708

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.07.27 07:44:56