Score:0

Write Windows Log entries to storage via Task scheduler job and powershell

id flag

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

Pimp Juice IT avatar
ch flag
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.
Chris9834 avatar
id flag
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.
Score:0
id flag

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   
mangohost

Post an answer

Most people don’t grasp that asking a lot of questions unlocks learning and improves interpersonal bonding. In Alison’s studies, for example, though people could accurately recall how many questions had been asked in their conversations, they didn’t intuit the link between questions and liking. Across four studies, in which participants were engaged in conversations themselves or read transcripts of others’ conversations, people tended not to realize that question asking would influence—or had influenced—the level of amity between the conversationalists.