I'm editing the original question, since it's getting marked as duplicate for the original question and adding single quotes instead of double still has this blank StarTime issue.
I'm trying to run this powershell as a shell command, and for some reason, it has an error in a Windows shell, but in a script it doesn't:
This is my shell command:
powershell "$today=[system.datetime](Get-Date); $startTime=$today.AddHours(-950); $events = Get-WinEvent -FilterHashtable @{LogName='Application';ProviderName='Application Error';Data='application.exe';StartTime=$($startTime);EndTime=$($today);} -ErrorAction SilentlyContinue; foreach ($event in $events) { $crashOccurredTime=$event.TimeCreated; $lookForInitStart = $event.TimeCreated.AddMinutes(-2); $eventInit = {Get-WinEvent -FilterHashtable @{LogName='Application';ProviderName='AppMgr';StartTime=$lookForInitStart;EndTime=$crashOccurredTime;} -ErrorAction SilentlyContinue | Where-Object -PipelineVariable Message -Match 'InitApp'}; if($eventInit -ne $null) { Write-Host 'Found application.exe after Init TimeCreated $($event.TimeCreated) ProviderName $($event.ProviderName) Message $($event.Message)'; }}"
The error is:
At line:1 char:185
+ ... ication Error';Data='Hop2USB69xxPhotoPrinteru.exe';StartTime=;EndTime ...
+ ~
Missing statement after '=' in hash literal.
At line:1 char:194
+ ... rror';Data='application.exe';StartTime=;EndTime=;} -Erro ...
+
...
It looks like the time variables are blank when it tires to use them for the FilterHashtable.
Note that this has to be run as a single shell command, not multiple PS commands. I can't save it as a .ps1 script because it gets deployed to 1000's of devices and we'd rather run it separately on the devices to find this sequence. We get the command output back.
If you want to try duplicating it, you might use google as your application name, since you see google on your computer usually, and maybe don't look for the errors, so you get output.
Update: I tried escaped double quotes and still see the same issue:
powershell "$today=[system.datetime](Get-Date); $startTime=$today.AddHours(-950); $events = Get-WinEvent -FilterHashtable @{LogName='Application';ProviderName='Application Error';Data='application.exe';StartTime=$($startTime);EndTime=$($today);} -ErrorAction SilentlyContinue; foreach ($event in $events) { $crashOccurredTime=$event.TimeCreated; $lookForInitStart = $event.TimeCreated.AddMinutes(-2); $eventInit = {Get-WinEvent -FilterHashtable @{LogName='Application';ProviderName='AppMgr';StartTime=$lookForInitStart;EndTime=$crashOccurredTime;} -ErrorAction SilentlyContinue | Where-Object -PipelineVariable Message -Match 'InitApp'}; if($eventInit -ne $null) { Write-Host \"Found application.exe after Init TimeCreated $($event.TimeCreated) ProviderName $($event.ProviderName) Message $($event.Message)\"; }}"
Error:
At line:1 char:185
+ ... ication Error';Data='application.exe';StartTime=;EndTime ...
+ ~
Missing statement after '=' in hash literal.
At line:1 char:194
+ ... rror';Data='application.exe';StartTime=;EndTime=;} -Erro ...
+ ~
Missing statement after '=' in hash literal.
At line:1 char:238
+ ... artTime=;EndTime=;} -ErrorAction SilentlyContinue; foreach ( in ) { ...
+ ~
Missing variable name after foreach.
At line:1 char:242
+ ... me=;EndTime=;} -ErrorAction SilentlyContinue; foreach ( in ) { =.T ...
+ ~
Unexpected token ')' in expression or statement.
At line:1 char:397
It looks like the variables are empty and causing the issue. Not sure why.
Update2: I'm testing at the command prompt after typing powershell and return, and then
PS E:\> powershell.exe { $today=[system.datetime](Get-Date); $startTime=$today.AddHours(-950); $events = Get-WinEvent -FilterHashtable @{LogName='Application';ProviderName='Application Error';Data='application.exe';StartTime=$($startTime);EndTime=$($today);} -ErrorAction SilentlyContinue; foreach ($event in $events) { $crashOccurredTime=$event.TimeCreated; $lookForInitStart = $event.TimeCreated.AddMinutes(-2); $eventInit = {Get-WinEvent -FilterHashtable @{LogName='Application';ProviderName='AppMgr';StartTime=$lookForInitStart;EndTime=$crashOccurredTime;} -ErrorAction SilentlyContinue | Where-Object -PipelineVariable Message -Match 'InitApp'}; if($eventInit -ne $null) { Write-Host \"Found application.exe after Init TimeCreated $($event.TimeCreated) ProviderName $($event.ProviderName) Message $($event.Message)\"; }} }
and it seems to be working. I'm not sure if that means my shell command should be this when I run it from our application that runs it:
powershell "powershell.exe { $today=[system.datetime](Get-Date); $startTime=$today.AddHours(-950); $events = Get-WinEvent -FilterHashtable @{LogName='Application';ProviderName='Application Error';Data='application.exe';StartTime=$($startTime);EndTime=$($today);} -ErrorAction SilentlyContinue; foreach ($event in $events) { $crashOccurredTime=$event.TimeCreated; $lookForInitStart = $event.TimeCreated.AddMinutes(-2); $eventInit = {Get-WinEvent -FilterHashtable @{LogName='Application';ProviderName='AppMgr';StartTime=$lookForInitStart;EndTime=$crashOccurredTime;} -ErrorAction SilentlyContinue | Where-Object -PipelineVariable Message -Match 'InitApp'}; if($eventInit -ne $null) { Write-Host \"Found application.exe after Init TimeCreated $($event.TimeCreated) ProviderName $($event.ProviderName) Message $($event.Message)\"; }} }"
When I tried running it from outside the powershell prompt there was an error:
powershell.exe { $today=[system.datetime](Get-Date); $startTime=$today.AddHours(-950); $events = Get-WinEvent -FilterHashtable @{LogName='Application';ProviderName='Application Error';Data='application.exe';StartTime=$($startTime);EndTime=$($today);} -ErrorAction SilentlyContinue; foreach ($event in $events) { $crashOccurredTime=$event.TimeCreated; $lookForInitStart = $event.TimeCreated.AddMinutes(-2); $eventInit = {Get-WinEvent -FilterHashtable @{LogName='Application';ProviderName='AppMgr';StartTime=$lookForInitStart;EndTime=$crashOccurredTime;} -ErrorAction SilentlyContinue | Where-Object -PipelineVariable Message -Match 'InitApp'}; if($eventInit -ne $null) { Write-Host \"Found application.exe after Init TimeCreated $($event.TimeCreated) ProviderName $($event.ProviderName) Message $($event.Message)\"; }} }
Error:
'Where-Object' is not recognized as an internal or external command,
operable program or batch file.