PsExec with Get-WinEvent: The system cannot find the file specified
I'm trying to run Get-WinEvent
on a remote PC using psexec
but I'm getting the following error:
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.
However, if I run the same command locally on the other PC it runs fine:
PS C:\> Get-WinEvent -?
NAME
Get-WinEvent
SYNTAX
Get-WinEvent [[-LogName] <string[]>]...
(etc.)
It appears that I can run other commands on the remote PC like systeminfo
and wmic
but none of the Get-*
commands.
Both the local and remote PCs are running Windows 10 and PowerShell 5.1. I've tried running psexec
version 2.2 and 2.33 on the local PC. (Curiously, version 2.2 gives the error above while version 2.33 gives a logon failure message even with administrator credentials.)
ANSWER: As Robert pointed out in the comments below, psexec
is for running executables, not PowerShell commands. See the marked answer below for how to do the same thing but with Invoke-Command
and PSRemoting
instead.
Solution 1:
You don't need psexec
to run PowerShell commands remotely against other machines. Ensure that the remote machine has PSRemoting enabled and then run PowerShell commands against the remote machine using invoke-command.
Enable-PSRemoting on remote machine
Enable-PSRemoting -Force
Running remote PowerShell commands
Invoke-Command -Computer pc28 -Scriptblock { Get-WinEvent -? }
Supporting Resources
-
Enable-PSRemoting
-
Invoke-Command
-ScriptBlock scriptblock
The commands to run.
Enclose the commands in curly braces
{ }
to create a script block. This parameter is required.