Johnny
Johnny

Reputation: 869

Scheduling a Powershell process does not yield the same results as when I run it manually

I wrote a small PowerShell script that I am using to query the Server Log, clean the return values and use some of the results to perform some server maintenance. However, when I schedule the save to file piece is not writing the whole content to the file and it is getting truncated, just like what I ma posting below, exactly. As you can observe, the end of the file is truncated with three dots added to replace the missing values:

Login failed for user 'sa'. Reason: An error occurred while evaluating the password. [CLIENT: 2...

However, if I run the code manually with Local Admin access, the content gets saved to the local file like this, exactly:

Login failed for user 'sa'. Reason: An error occurred while evaluating the password. [CLIENT: 112.103.198.2]  

Why is this the case when I schedule the process or PS file to run under a schedule. BTW, I tried to run it under the SYSTEM context with full or highest privileges and even used the same Admin account that I use to run it manually to schedule and still do nt get the full content of the event that I save.

This is creating an issue and I am not able to use the content to process the IP.

Here is the PS code that I am using to query and save the content to file:

    $SQL = 'C:\SQL.txt'

    Remove-Item $SQL -ErrorAction Ignore

    Get-EventLog -LogName Application | Where-Object {$_.EventID -eq 18456} | 
    Select-Object -Property Message | Out-File $SQL 

Upvotes: 0

Views: 62

Answers (1)

Robert Cotterman
Robert Cotterman

Reputation: 2268

The problem lies with out-file because it has a default character limit of 80 per line. You can change it with -width property and give a value of say 200. However set-content doesn't have these limits set in. So it might be a more suitable option.

All that being said, I am not sure why it does it one way when ran manually vs another when the system runs it.

Out-file defaults to unicode when writing files

set-file defaults to ascii when writing files

Upvotes: 1

Related Questions