Howdy, Stranger!

It looks like you're new here. If you want to get involved, click one of these buttons!


[SOLVED] How to get Scheduled Task to run Powershell script from %temp% on Win2012R2/2016
New on LowEndTalk? Please Register and read our Community Rules.

All new Registrations are manually reviewed and approved, so a short delay after registration may occur before your account becomes active.

[SOLVED] How to get Scheduled Task to run Powershell script from %temp% on Win2012R2/2016

myhkenmyhken Member
edited August 2017 in General

Hello, I'm stuck, and have used lots of time, and need some help. I just don't get it.
So $10 to the first one with the correct solution, transferred via PayPal. It's a long text, since I want to explain what I try to do, what I have tried to fixed it, and why.

I'm currently working on a script, and just now I'm stuck on Server 2012 R2.
The script is saved in the %temp% folder and a sub folder called CT-TEMP
The path is %temp%\CT-TEMP\script1.ps1

If I in cmd use this command, it run just fine:

powershell -file "%temp%\CT-TEMP\script1.ps1"

But if I create a scheduled task and under Action create a task that run powershell.exe and in add arguments I write -file "%temp%\CT-TEMP\script1.ps1" then windows refuse to run the task. The task is set to system, run whether a user is logon or not and run with highest privileges. I get a 0xfffd0000 error code after it has runned, and it never run the script.

It seems like it can't read the %temp% folder info.

The strange thing is if I go in to the task, and replace %temp% with the actual path, then run the job, everything is working.

If I then change back to %temp% it also runs fine. So it's just the first time it can't read the %temp% path, but it's seems to cache the information if you use the correct path.
My issue are that I'm creating a script that's going to create this task on a lot of servers.
So I can't access each server to find the actual %temp% path, then add it to the task, then run the task, then change back to %temp%.

In PowerShell, I have created a script to create the task. And in that script I use:
$TempFolder = $env:TEMP $action = New-ScheduledTaskAction -Execute 'Powershell.exe' -Argument '-File "%temp%\CT-TEMP\script1.ps1"'

But I can't change the script to actually change the %temp% with $TempFolder, since the argument contains ' '. So if anybody have the correct format to add the argument, but so it's also contains $TempFolder it will most likely solve the issue.

So how to solve this, so the task just run on the first try?

Why not use just a normal directory and not %temp% you say? Some of the servers I want to add this script on, has restrictions to create files and folder on c:\
But I always have write access to the %temp% folder.

Comments

  • BogdacutuuBogdacutuu Member
    edited August 2017

    $action = New-ScheduledTaskAction -Execute 'Powershell.exe' -Argument ('-File "' + $env:TEMP + '\CT-TEMP\script1.ps1"')?

    Thanked by 1myhken
  • @Bogdacutuu That did the trick, thank you. PM me your PayPal e-mail, and $10 is yours.

  • mikhomikho Member, Host Rep

    System doesn't have a %temp% folder as a regular user.

    I think you should look at
    %systemroot%\ServiceProfiles

  • mikhomikho Member, Host Rep

    @myhken said:

    Why not use just a normal directory and not %temp% you say? Some of the servers I want to add this script on, has restrictions to create files and folder on c:\
    But I always have write access to the %temp% folder.

    The system user should have read/write access to all system folders on the server.

  • mikho said: The system user should have read/write access to all system folders on the server.

    Yea, but the system user is only used in the scheduled task. When pushing out the script to all servers, I'm only using the system user (Local credentials).
    That user do not always have access to write/create files and folders on all part of the disk.

Sign In or Register to comment.