PsExec 與 Get-WinEvent:系統找不到指定的文件

PsExec 與 Get-WinEvent:系統找不到指定的文件

我嘗試Get-WinEvent在遠端 PC 上運行psexec,但出現以下錯誤:

PS C:\> psexec \\pc28 Get-WinEvent -?
Starting Get-EventLog on pc28...
PsExec could not start Get-EventLog on pc28:
The system cannot find the file specified.

但是,如果我在另一台 PC 上本地運行相同的命令,它可以正常運行:

PS C:\> Get-WinEvent -?

NAME
    Get-WinEvent

SYNTAX
    Get-WinEvent [[-LogName] <string[]>]...
(etc.)

看來我可以在遠端 PC 上運行其他命令,例如systeminfowmic但沒有任何Get-*命令。

本機和遠端PC都運行Windows 10和PowerShell 5.1。我嘗試psexec在本地 PC 上運行版本 2.2 和 2.33。 (奇怪的是,2.2 版會出現上述錯誤,而 2.33 版即使使用管理員憑證也會出現登入失敗訊息。)

回答:正如 Robert 在下面的評論中指出的那樣,psexec用於運行可執行文件,而不是 PowerShell 命令。請參閱下面標記的答案,以了解如何使用Invoke-CommandPSRemoting來做同樣的事情。

答案1

您不需要psexec對其他電腦遠端執行 PowerShell 命令。確保遠端機器有PS遠端處理啟用,然後使用以下命令對遠端電腦執行 PowerShell 命令調用命令

啟用 PSRemoting在遠端機器上

Enable-PSRemoting -Force

執行遠端 PowerShell 命令

Invoke-Command -Computer pc28 -Scriptblock { Get-WinEvent -? }

支持資源

  • 啟用 PSRemoting

  • 調用命令

    -ScriptBlock scriptblock

    要運行的命令。

    將命令括在大括號中{ }以建立腳本區塊。此參數是必需的。

相關內容