how to run as admin powershell.ps1 file calling in batch file from scheduled task

anuja chandgude 1 Reputation point
2021-05-16T08:14:55.75+00:00

I'm running a batch file including the below command from the scheduled task using Bamboo. But I'm not able to process it because it is being stuck.

powershell.exe -noprofile -NoExit -command "&{start-process powershell -ArgumentList '-NoExit -noprofile -file \"%~dp0psfile.ps1\"' -verb RunAs}"

When we run the same batch file from local it opens up one dialogue asking for allowing permissions, as shown below.

96923-permission-dialogue.jpg

Can you please help me resolve this? This batch file internally calls .PS1 script which runs using RS jobs in parallel threads.

Windows for business Windows Server User experience PowerShell
{count} votes

1 answer

Sort by: Most helpful
  1. MotoX80 36,291 Reputation points
    2021-05-16T13:34:19.233+00:00

    I'm running a batch file including the below command from the scheduled task using Bamboo. But I'm not able to process it because it is being stuck.

    I have no idea what Bamboo is or does. You might want to explain more about that and how it is being used.

    In the Task Scheduler, define the task to run as an account that is a member of the administrators group. To prevent UAC issues, select "run with highest privileges".

    https://www.digitalcitizen.life/use-task-scheduler-launch-programs-without-uac-prompts/

    96943-capture.jpg

    powershell.exe -noprofile -NoExit -command "&{start-process powershell -ArgumentList '-NoExit -noprofile -file \"%~dp0psfile.ps1\"' -verb RunAs}"  
    

    You don't need to do that. Just have the 1st PS run the script.

    powershell.exe -noprofile -file "%~dp0psfile.ps1"  
    

    You may also need to add an execution policy if you have not set it globally.

    powershell.exe -noprofile -ExecutionPolicy Bypass -file "%~dp0psfile.ps1"  
    

    Normally you would not want to use -NoExit switch because that will leave Powershell.exe running after the script has terminated. You'll end up with a bunch of PS exe's running and doing nothing other than using up memory.


Your answer

Answers can be marked as Accepted Answers by the question author, which helps users to know the answer solved the author's problem.