0
votes

I have a simple PowerShell script that just creates a file. Testing this for a bigger PowerShell script.

Running on Windows Server 2012 R2 - 64-bit

its running under the Administrator account.

Run whether user is logged on or not

Run with highest privileges


Action: Program/Script : Powershell.exe (I've tried it this way and the full path)

Add argumetns: -NoProfile -executionpolicy remotesigned -file C:\Scripts\test.ps1


Get-executionPolicy: RemoteSigned

added "Administrator" to: Set-PSSSessionConfiguration -Name Microsoft.PowerShell -ShowSecurityDescriptorUI

"Administrator" has "Log on as batch job" permissions "Administrator" is in the Admins group

PowerShell Script for testing:

    $text = "Hello World"

    $text | Set-Content TestMyFile.txt
    $text | Out-File TestMyFile.txt
    $text > TestMyFile.txt

    # This is to write into a file or append to the text file created:
    $text | Add-Content TestMyFile.txt
    $text | Out-File TestMyFile.txt -Append
    $text >> TestMyFile.txt

Nothing fancy, just trying to make sure Task Scheduler will execute a PowerShell script.

So What am I missing?

1
You aren't using full paths on the files, so it's writing them into the working directory (likely not where the script is stored), so 1) do you have permission to write there and 2) did you actually look there?briantist
He said "running under administrator acc." which probably means:1) yes 2) data will end up at %systemroot%\system32. Is there even an error? What does the "History" tab show?Adil Hindistan
wow, ok I didn't think about the file directory..... I changed the code to use a directory$text | Out-File C:\Script\TestMyFile.txtmr_evans2u

1 Answers

0
votes

Use full paths in your output code, and for any future problems with running scripts from Task Scheduler, you should first put in logging and try/catch blocks to see what errors/outputs the script is generating before seeking further assistance. Doing this will help you find the answer faster & learn faster at the same time.

Tip: Always only name the program in the 'Program to run' aspect of Task Scheduler, as the Task Scheduler uses its own wrapper to execute the action, and in rare occasions, it can produce undesired results if you place the entire execution line in this field. Always put parameters in the parameters field.