0

i have created a task in Taskplaner that, whenever a new log-entry got created, it executes the following PS-script that should write the newly created eventlog-entry in a csv-file on storage.

$date = Get-Date
$pdate = Get-Date -UFormat %Y-%d-%m
$Name = 'Microsoft-Windows-TerminalServices-Gateway/Operational'
$Log = [System.Diagnostics.EventLog]$Name

$Action = {
    # get the original event entry that triggered the event
    $entry = $event.SourceEventArgs.Entry
    # do something based on the event
    $entry | select TimeCreated, Id, LevelDisplayName, message  | Export-Csv -Path c:\TestLog.csv -append

Unfortunately the file did not get updated even the task scheduler said that the script was called and no error happend. Task is run as Domainadmin and user has write access to file. Starting the script manually works, even no output is written as no logitem-object exists when running manually.

The script seems to work fine when using it manually (as it is possible to test at all)

Can someone pls. point me to the mistake i have made?

Thanks.

Here are the settings in Task Scheduler

enter image description here

enter image description here

2
  • Here's what I would do to troubleshoot next. Add Start-Transcript -Path C:\SomeFolder\SomeFile.txt to the process and ensure that it writes to that transcript file and look how it looks when you run it manually and it works. Now run the process and check it when it does not work. Next set the scheduled task to run as "SYSTEM" and Whether logged on or not selected. Now run the process and check the transcript. If problem resolved, comment out the ` Start-Transcript` and Stop-Transcript commands. I find this simple and usually an effective way to troubleshoot these cases. Commented Mar 18, 2022 at 3:56
  • I think the whole code for catching the event just does not work. Need to find another example or some explaination how to catch a event that triggers a task scheduler within that task.
    – Chris9834
    Commented Mar 18, 2022 at 11:27

1 Answer 1

0

The code catching the event seems not to work. I have solved it via a much more basic way, just by reading the latest event item.

That's the PS code that is working now

$Name = 'Microsoft-Windows-TerminalServices-RemoteConnectionManager/Operational'

$entry = Get-WinEvent -LogName Microsoft-Windows-TerminalServices-RemoteConnectionManager/Operational -MaxEvents 1
$entry | select TimeCreated, Id, LevelDisplayName, message | Export-Csv -Path c:\RDPLog.csv -Append   

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .