Task Scheduler - Powershell elevated task - powershell

I have a script which works when it is manually executed the script works under these conditions:
1) I start start.ps1 which then elevates with admin user then executes main.ps1 script
2) the start.ps1 is being run without any rights and I am elevating it by using
Start-Process -FilePath Powershell -LoadUserProfile -Credential $credential -ArgumentList '-File', $script
Everything works fine, please note that new powershell window appear and main.ps1 is being executed there.
now, when I create a scheduled task, then run it - I can see in the task manager that main.ps1 is being executed but noting is happening apart from task being stuck in scheduler. I cant use admin user in task scheduler it has to be executed as non privileged user.
Do you have any ideas how to fix this issue?
I have tried running it under local user, with/or without execution policy set to bypass/unrestricted etc nothing helped.
Thanks in advance

Related

Get-RDUsersession does not work from Task Scheduler

So this has me perplexed. We have a Powershell script that calls the following command:
$id = get-rdusersession
When we run the script from the Powershell console everything works just fine. However, when we run this very same script from the Windows Task Scheduler things fail. In the transcript is the following:
get-rdusersession : A Remote Desktop Services deployment does not exist on OurServer.OurDomain.com.
This deployment definitely exists- and the entire scripts runs fine from the Powershell console. I have tried all manner of
-CollectionBroker
to no avail. The scheduled task is set to run as administrator with elevated permissions. also the command parameters
-noprofile -executionpolicy unrestricted -noninteractive
are all set.
Anyone have any idea at all what the problem is?

Powershell script runs but task scheduler wont run it

I have a powershell script that runs in ISE but wont run in Task Scheduler. It is a simple powershell script to move a file... I have it set to run at a specific time in task scheduler, but it wont run. When I try to run it manually from task scheduler, it gets stuck on running. Its moving the file to a mapped drive. I have the task scheduler set to run whether the user is logged on or not and with higest priviledges and its also under an admin account... Based upon research, I found articles about the PSDrive cmdlet if you are using mapped drives but I dont know how to incorporate it into my script...Can anyone help me incorporate the PSDrive cmdlet if this is the issue, thanks.
Set-ExecutionPolicy Bypass -scope Process -Force
$_SourcePath = "C:\lilsis\seniors_allcourses.csv"
$_DestinationPath = "Z:\"
Copy-item –path $_SourcePath –destination $_DestinationPath -force
enter image description here

Powershell script triggers process only when invoked manually. Timing out when triggered via Scheduled Task

On a Windows 2012 R2 server there is a Powershell script that I can manually invoke to start a process on some EXE, this works just fine.
But when trying to trigger it via a scheduled task (invoking the same script) the start-process within the script just doesn't trigger or finish. Causing the task scheduler to terminate the task due to exceeding the timeout threshold.
Here's the core section of the script:
$exe = "c:\some\app.exe"
$arguments = "-user me -pwd secret"
$process = Start-Process $exe -ArgumentList $arguments -PassThru -Wait
return $process
Is there some way I can get some insights into what start-process is doing or why the same script works when invoked manually but not programmatically?
I want to emphasize that the way the script is invoked from the scheduled task is not a problem! The script triggers because the corresponding log file populates.
Any insights or help on this is greatly appreciated!
quick update on this since I found the problem. It turns out, it had nothing to do with either the powershell script or the scheduled task itself...
On the machine the script is running on, there is a network share that is mapped as the z:\ drive. I use it to save logs to. Now apparently that mapping/mounting is handled differently depending on whether the script is invoked interactively or programatically, because in the latter case it appears that the resoultion of the network path \\network\share\folder1 does not succeed, however there is nothing complaining about it, the process just silently does not start. If however, I point the logs to a physical local path or the explicit full network path itself, there is no problem running the script.
Lesson learned, never trust OS' drive mapping of network paths :D
Cheers

Task scheduler PowerShell Start-Process is not running

I have this simple script on Windows 10, which works fine when just executing, but fails to start notepad when running from task scheduler. Stop-Process works perfectly, Start-Process does not run. When I run it on demand, it closes the notepad and then keeps running without opening notepad, the task does not close also.
Stop-Process -processname notepad
Start-Process "C:\Windows\system32\notepad.exe"
This is how it is configured to run.
Things I have tried, but still does not work.
First of all, I am running under administrator account.
In task schduler, run with highest privileges is checked.
I have tried -ExecutionPolicy Bypass and -ExecutionPolicy RemoteSigned
Under security policy have given my user Logon as batch job permission
Turn UAC off
The application was ran in background. To make it run on foreground, had to check the box Run only when user is logged on.

Powershell's Invoke-Command not working when called from Windows Task Scheduler

I have a Powershell script, which is working just fine, when called from command line, but only partially fine when executed by the Windows Task Scheduler. The script looks as follows:
# Do things, which are always working.
$session = new-pssession -computername SRV
Invoke-Command -session $session -scriptblock { D:\script.bat }
# Do things, which are always working.
The task defined in the Task Scheduler is completed without errors. As you see, all parts before and after Invoke-Command are working, also when called by the Task Scheduler. Only the Invoke-Command itself is only working when called from the command line.
My only guess is, that the Powershell script is exiting prematurely, but I didn't find any way to confirm this or even solve the issue.
it is likely to be a permissions issue - as a test, run the task using the same credentials as when it is run at the command line...
I had the same issue with Invoke-Command when I called a script from Task Scheduler to run certain scriptblocks on multiple computers. I don't know why, but setting the "Start in" folder of the action to the folder of the script solved it.