你可以在PowerShell会话中运行cmd命令吗?

huangapple go评论51阅读模式
英文:

How can I run cmd commands inside a PowerShell session?

问题

无法在PowerShell会话中运行cmd命令,尽管我在命令本身之前键入了"cmd /c",如下所示,我收到了以下错误:

PS C:\Program Files (x86)\FireEye\xagt> cmd /c 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"

cmd : 无法解析参数:95319.txt
At line:1 char:1
+ cmd /c xagt.exe -g "C:\tmp\xagt_%computername%_%date:~-4,4%%date:~-10 ...
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
    + CategoryInfo          : NotSpecified: (无法解析参数:95319.txt:String) [], RemoteException
    + FullyQualifiedErrorId : NativeCommandError

如果我在cmd控制台会话中运行以下命令,如下所示,我得到了成功的结果,即在c:\tmp中生成了名为"xagt_servername_20232405_151403.txt"的文件,此命令生成了一个具有名称"xagt_servername_date_serialnumber"的日志文件:

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"

成功

奇怪的是,如果我在cmd控制台会话中运行完命令后,在PowerShell中运行以下脚本,cmd在PowerShell中成功运行,并在c:\tmp中生成另一个日志文件:

PowerShell脚本如下:

Write-Host "----------------------------------------"
            Write-Host ""##############Microsoft Fireeye   Info###############""
            Write-Host "----------------------------------------`r`n"

Get-ChildItem -Path 'HKLM:\SOFTWARE\Wow6432Node\microsoft\Windows\CurrentVersion\Uninstall'  | Get-ItemProperty |   where {$_.DisplayName -like 'FireEye Endpoint Agent'} | Select DisplayName, Publisher, InstallDate,DisplayVersion

cd "C:\Program Files (x86)\FireEye\xagt\"
cmd /c 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"

我没有收到错误消息,并且在c:\tmp中创建了另一个日志文件。我不知道为什么它在第一次尝试时不起作用。

我尝试使用以下命令在PowerShell中运行cmd命令:

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`""

但它没有按预期生成日志文件。

我还尝试了:

PS 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"

.\xagt.exe : 错误:-4058
At line:1 char:1
+ .\xagt.exe -g "C:\tmp\xagt_%computername%_%date:~-4,4%%date:~-10,2%%d ...
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
    + CategoryInfo          : NotSpecified: (错误:-4058:String) [], RemoteException
    + FullyQualifiedErrorId : NativeCommandError

另一个尝试:

PS C:\Program Files (x86)\FireEye\xagt> cmd /c 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"

cmd : 无法解析参数:95319.txt
At line:1 char:1
+ cmd /c xagt.exe -g "C:\tmp\xagt_%computername%_%date:~-4,4%%date:~-10 ...
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
    + CategoryInfo          : NotSpecified: (无法解析参数:95319.txt:String) [], RemoteException
    + FullyQualifiedErrorId : NativeCommandError
英文:

I can't run cmd command inside PowerShell session although I type "cmd /c" before the command itself as shown below I got the following error:

PS C:\Program Files (x86)\FireEye\xagt> cmd /c 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"

cmd : Fail to parse the argument: 95319.txt
At line:1 char:1
+ cmd /c xagt.exe -g "C:\tmp\xagt_%computername%_%date:~-4,4%%date:~-10 ...
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
    + CategoryInfo          : NotSpecified: (Fail to parse the argument: 95319.txt:String) [], RemoteException
    + FullyQualifiedErrorId : NativeCommandError

If I run the following command in cmd console session as shown below, I got a successful result that a file named "xagt_servername_20232405_151403.txt" has been generated in c:\tmp, this command generates a log file that has the name "xagt_servername_date_serialnumber"

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"

success

The strange thing is, if I run the following script using PowerShell after I have run the command in cmd console session, the cmd inside the PowerShell run successfully and generate another log file in c:\tmp

The PowerShell script as follows:

Write-Host "----------------------------------------"
            Write-Host ""##############Microsoft Fireeye   Info###############""
            Write-Host "----------------------------------------`r`n"

Get-ChildItem -Path 'HKLM:\SOFTWARE\Wow6432Node\microsoft\Windows\CurrentVersion\Uninstall'  | Get-ItemProperty |   where {$_.DisplayName -like 'FireEye Endpoint Agent'} | Select DisplayName, Publisher, InstallDate,DisplayVersion

cd "C:\Program Files (x86)\FireEye\xagt\"
cmd /c 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"

I got no errors and another log file is created in c:\tmp. I don't know why it doesn't work from the first attempt.

I try to run cmd commands usig PowerShell using the following:

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`""

But it didn't generate the log file as expected.

I also tried:

PS 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"

.\xagt.exe : Error: -4058
At line:1 char:1
+ .\xagt.exe -g "C:\tmp\xagt_%computername%_%date:~-4,4%%date:~-10,2%%d ...
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
    + CategoryInfo          : NotSpecified: (Error: -4058:String) [], RemoteException
    + FullyQualifiedErrorId : NativeCommandError

Another try:

PS C:\Program Files (x86)\FireEye\xagt> cmd /c 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"

cmd : Fail to parse the argument: 95319.txt
At line:1 char:1
+ cmd /c xagt.exe -g "C:\tmp\xagt_%computername%_%date:~-4,4%%date:~-10 ...
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
    + CategoryInfo          : NotSpecified: (Fail to parse the argument: 95319.txt:String) [], RemoteException
    + FullyQualifiedErrorId : NativeCommandError

答案1

得分: 4

<!-- language-all: sh -->

评论中提供了很有价值的信息,[JosefZ](https://stackoverflow.com/users/3439404/josefz) 提供了关键指示:您的命令失败,因为 cmd.exe 的动态 `%TIME%` 变量对于所有单个数字(上午)的小时都有一个_前导空格_,例如 ` 9:29:16.15`;这个空格导致传递了一个_额外的参数_。

让我添加一个仅使用 PowerShell 的解决方案,可以绕过您的问题,而且更简单和更高效,因为您不需要通过 `cmd.exe` 调用:

```powershell
xagt.exe -g "C:\tmp\xagt_${env:COMPUTERNAME}_$(Get-Date -Format 'yyyyMMdd_HHmmss').txt"

上面的命令使用了:


<details>
<summary>英文:</summary>

&lt;!-- language-all: sh --&gt;

There&#39;s great information in the comments, and [JosefZ](https://stackoverflow.com/users/3439404/josefz) has provided the crucial pointer: your command failed, because cmd.exe&#39;s dynamic `%TIME%` variable has a _leading space_ for all single-digit (morning) hours; e.g. ` 9:29:16.15`; this space caused an _extra argument_ to be passed.

Let me add a PowerShell-only solution that bypasses your problem and is both simpler and more efficient, given that you don&#39;t need to call via `cmd.exe`:

xagt.exe -g "C:\tmp\xagt_${env:COMPUTERNAME}_$(Get-Date -Format 'yyyyMMdd_HHmmss').txt"


The above uses:

* an [expandable (double-quoted) PowerShell string (`&quot;...&quot;`)](https://learn.microsoft.com/en-us/powershell/module/microsoft.powershell.core/about/about_Quoting_Rules#double-quoted-strings), in which variable references - such as environment-variable reference `$env:COMPUTERNAME` - can be embedded, as well as, via `$(...)`, commands and expressions, such as the `Get-Date` call.

* a call to the [`Get-Date`](https://learn.microsoft.com/en-us/powershell/module/Microsoft.PowerShell.Utility/Get-Date) cmdlet with its `-Format` parameter, which allows you to format a date and time as desired, such as via [custom date and time format strings](https://docs.microsoft.com/en-us/dotnet/standard/base-types/custom-date-and-time-format-strings).

  * Note that the use of `HH` for the hour of the day causes single-digit hours to be left-padded with a `0`, e.g. `09`, and PM hours to be expressed in 24-hour format, e.g. `15` for 3 PM.

</details>



huangapple
  • 本文由 发表于 2023年5月24日 22:52:20
  • 转载请务必保留本文链接:https://go.coder-hub.com/76324832.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定