Problem in executing PowerShell script as Scheduled Task & Run As Administrator

Ask your PowerShell-related questions, including questions on cmdlet development!
Forum rules
Do not post any licensing information in this forum.

Any code longer than three lines should be added as code using the 'Select Code' dropdown menu or attached as a file.
This topic is 5 years and 6 months old and has exceeded the time allowed for comments. Please begin a new topic or use the search feature to find a similar but newer topic.
Locked
User avatar
ITEngineer
Posts: 216
Last visit: Thu Mar 23, 2023 5:45 pm
Has voted: 4 times

Problem in executing PowerShell script as Scheduled Task & Run As Administrator

Post by ITEngineer »

I'm having the problem when trying to execute this Powershell as the Scheduled task.

I have followed this steps: https://blogs.technet.microsoft.com/hey ... ll-script/


and use the following Argument:
-ExecutionPolicy Bypass "C:\Scripts\Backup Report PRDBKP01-VM (Daily).PS1"

Set the Scheduled Task as Run with Highest privileges.
Grant the Service account as Local Administrators of the server where it is running.

Script Source: https://gist.githubusercontent.com/smas ... Report.ps1

The Scheduled Task does not return any error (last code was 0x1).

I have already tried that to encode the large 1000 lines+ script as .BAT script as follows:

Code: Select all

@echo off

echo Executing Backup Summary Report
call :callPS PSScript1

exit /b

:callPS label
powerShell.exe -ExecutionPolicy RemoteSigned -Command "$script = Get-Content '%~f0'; Invoke-Expression -Command ($script[(($script | select-string '::%1::').LineNumber)..(($script | select-string '::%1End::').LineNumber-2)] -join [environment]::NewLine)"

exit /b

::PSScript1::

Add-PSSnapin "VeeamPSSnapIn" -ErrorAction SilentlyContinue
# the rest of the PowerShell script goes here....

::PSScript1End::
But it never works when I double click and even when I right Click Run as Administrator on the .BAT script.
It only works when I execute it under PowerShell ISE Run as Administrator.

Can anyone here please help?
/* IT Engineer */
jvierra
Posts: 15439
Last visit: Tue Nov 21, 2023 6:37 pm
Answers: 30
Has voted: 4 times
Been upvoted: 33 times

Re: Problem in executing PowerShell script as Scheduled Task & Run As Administrator

Post by jvierra »

Many batch scripts cannot be run outside of a UI session. They may need a full profile.

Have you tried to manually create the task with the batch script?
User avatar
ITEngineer
Posts: 216
Last visit: Thu Mar 23, 2023 5:45 pm
Has voted: 4 times

Re: Problem in executing PowerShell script as Scheduled Task & Run As Administrator

Post by ITEngineer »

jvierra wrote: Tue Aug 28, 2018 8:21 pm Many batch scripts cannot be run outside of a UI session. They may need a full profile.

Have you tried to manually create the task with the batch script?
Hi Mr. Vierra,

I have manually created the Scheduled task to call the .PS1 as per the MS Blog article above, but it is still failed to launch with no result at all?
/* IT Engineer */
jvierra
Posts: 15439
Last visit: Tue Nov 21, 2023 6:37 pm
Answers: 30
Has voted: 4 times
Been upvoted: 33 times

Re: Problem in executing PowerShell script as Scheduled Task & Run As Administrator

Post by jvierra »

Why are you using PowerShell for a batch file? Just run the batch file.

An exit code of 1 means that the execution failed.
User avatar
ITEngineer
Posts: 216
Last visit: Thu Mar 23, 2023 5:45 pm
Has voted: 4 times

Re: Problem in executing PowerShell script as Scheduled Task & Run As Administrator

Post by ITEngineer »

jvierra wrote: Tue Aug 28, 2018 10:25 pm Why are you using PowerShell for a batch file? Just run the batch file.

An exit code of 1 means that the execution failed.
It is the other way around,

The Backup report is 4000+ lines of PowerShell code which I have tried to encapsulate into the BATCH file for easy executable, but somehow it is still the same result, it is not working.

It can only work when I run it on PowerShell .ISE as Administrator.
/* IT Engineer */
jvierra
Posts: 15439
Last visit: Tue Nov 21, 2023 6:37 pm
Answers: 30
Has voted: 4 times
Been upvoted: 33 times

Re: Problem in executing PowerShell script as Scheduled Task & Run As Administrator

Post by jvierra »

Why do you need a batch file? Just execute it as PowerShell.

There is no way for us to guess at what is wrong with 4000 line script. You will have to debug it.
This topic is 5 years and 6 months old and has exceeded the time allowed for comments. Please begin a new topic or use the search feature to find a similar but newer topic.
Locked