Home > OS >  Windows Command Line Powershell error: Missing closing ')' in expression
Windows Command Line Powershell error: Missing closing ')' in expression

Time:07-28

I'm having trouble formatting this to run as a single command line.

powershell ("$today = [system.datetime](Get-Date); $startTime = $today.AddHours(-4); Get-WinEvent -FilterHashtable @{LogName='Application'; ProviderName='Application Error';Data='internalProcess.exe';StartTime=$startTime;EndTime=$today}")

I'm seeing this error:

At line:1 char:38
  ($today = [system.datetime](Get-Date); $startTime = $today.AddHours(- ...
                                       ~
Missing closing ')' in expression.

I'm unclear what the issue is. When I run this in PowershellISE, it's just not finding processes on my laptop with that name.

In powershell ISE:

$today = [system.datetime](Get-Date); $startTime = $today.AddHours(-100); Get-WinEvent -FilterHashtable @{LogName='Application'; ProviderName='Application Error';StartTime=$startTime;EndTime=$today}

Get-WinEvent : No events were found that match the specified selection criteria.
At K:\Cleary\BlobScans\69xxNotFoundEDCS\WinEvents.ps1:21 char:75
  ... ours(-100); Get-WinEvent -FilterHashtable @{LogName='Application'; Pr ...
                  ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
      CategoryInfo          : ObjectNotFound: (:) [Get-WinEvent], Exception
      FullyQualifiedErrorId : NoMatchingEventsFound,Microsoft.PowerShell.Commands.GetWinEventCommand

I'm able to run the command without the date part on the device:

powershell ("Get-WinEvent -FilterHashtable @{LogName='Application'; ProviderName='Application Error';Data='internalProcess.exe'}")

(no errors...it lists 3 Application Errors, dated 7/23/2022)

I tried changing the time to -130 hours in the powershell:

powershell ("$today = [system.datetime](Get-Date); $startTime = $today.AddHours(-4); Get-WinEvent -FilterHashtable @{LogName='Application'; ProviderName='Application Error';Data='internalProcess.exe';StartTime=$startTime;EndTime=$today}")

I still see the same error:

At line:1 char:38
  ($today = [system.datetime](Get-Date); $startTime = $today.AddHours(- ...
                                       ~
Missing closing ')' in expression.

How do I format the line(s) to run at the windows command line so it doesn't think it's missing closing parenthesis?

CodePudding user response:

The parenthesis placement needs to be before the call to run PowerShell.exe instead of after it. As it is PowerShell is interpreting it as part of the syntax, and that isn't right. You need cmd to read it as a grouping to run powershell with the specified command instead. Move the parenthesis before PowerShell to fix the syntax.

  • Related