Get-RDUsersession does not work from Task Scheduler - powershell

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?

Related

Task Scheduler - Powershell script not firing?

I've created numerous scripts in PowerShell that are working as intended if I execute them directly, however, when I try and setup a schedule to run these in Task Scheduler (to run with highest privileges) it doesn't seem to be running anything at all.
I'm running the following in my actions:
powershell.exe -ExecutionPolicy Bypass -File C:\PS\Mailboxes\CheckForwardingList.ps1
I'm getting a "Last Run Result" of 0x0 and the particular purpose of the above script is to generate a TXT file from EXO which it then mails out via SMTP and I've yet to receive any emails and I also don't see any TXT being generated in the folder where the script is located.
I do have two additional scripts setup which aren't running but once I've addressed the issue on the above this should quickly rectify the problems.
I like to test my PowerShell scripts from a command prompt first.
For example a script called C:\Tests\Test-PowerShellScriptsRunning.ps1 that only contains the following one liner helps me to test if scripts can run successfully on a machine
Write-Host -ForegroundColor Yellow "If you see this, then your script is running"
Next, I run this script from a command prompt, to get the syntax right in my scheduled task:
powershell.exe -nologo -file c:\Tests\Test-PowerShellScriptsRunning.ps1
Of course, you can add the -Executionpolicy bypass parameter, but I prefer to test the execution policy first.
However, as you are running a script that connects to ExchangeOnline, I suspect it has to do with the user you are running this task under. Does the script run if you run this task under your credentials or are the credentials stored on the system or in the script?
You might want to check this article to see how you can register an app and authenticate without storing your credentials on the machine to run the script unattended: App-only authentication for unattended scripts in the EXO V2 module

How can I bypass execution policy when running scripts from Powershell ISE

So I can write a script in Powershell ISE, not save it, and it will run (F5/green arrow on the ISE). Once I save it, I get the error saying I can't run saved scripts. If I open a Powershell window in the directory the saved script exists, I can run it with
powershell -ExecutionPolicy ByPass -File script.ps1
But is there a way I can get this to work when running it via the ISE's green arrow/F5? I don't have admin access to this PC
Edit: Windows 10
Ok so I just found out you can set Execution Policy for yourself (current user) without having admin rights. So if you're coming here from google do this:
Set-ExecutionPolicy -Scope "CurrentUser" -ExecutionPolicy "RemoteSigned"
Also you can run individual script without setting Execution Policy for current user, by passing Execution Policy only for file script.
For example:
Powershell -executionpolicy RemoteSigned -File "C:\scripts\script.ps1"
Very convenient for scheduled tasks in the Windows Task Scheduler to run PowerShell commands (scripts).
That's my addition for google users

Task Scheduler - Powershell elevated task

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

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.

How can I run this Powershell script as a user?

I am trying to run the following script upon startup of my machine because I cannot get a service to start on startup. I can schedule it to run. The problem I am facing seems to be a user vs administrator issue.
This is for a Windows 7 machine with PowerShell 4.0.
I have set the execution policy to unrestricted.
When I try to run the script as the User I designated to have administrative privileges I see the following error.
When I right click the powershell icon, run as administrator the command works fine.
The command is
get-service -name "*vmauthd*" |start-service
Is it possible to run this as my user account?
Solution I was able to get this script to run on startup as I initially desired. I turned off UAC and set the execution policy to unrestricted. I am not sure if the UAC was the issue before but the script runs now. I created a cmd file with this in the code. I set the cmd file to run at startup using Windows Task Scheduler and set the task to run whether logged in or not.
PowerShell -Command "Set-ExecutionPolicy Unrestricted"
PowerShell -Command "get-service -name "vmauthd" | start-service"
pause
Here is an image of the cmd file