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

Ask your Windows 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.
Locked
User avatar
ITEngineer
Posts: 145
Joined: Wed Oct 12, 2011 10:52 am

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

Post by ITEngineer » Tue Aug 28, 2018 8:11 pm

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 */

User avatar
jvierra
Posts: 13718
Joined: Tue May 22, 2007 9:57 am
Contact:

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

Post by jvierra » 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?

User avatar
ITEngineer
Posts: 145
Joined: Wed Oct 12, 2011 10:52 am

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

Post by ITEngineer » Tue Aug 28, 2018 10:08 pm

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 */

User avatar
jvierra
Posts: 13718
Joined: Tue May 22, 2007 9:57 am
Contact:

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

Post by jvierra » 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.

User avatar
ITEngineer
Posts: 145
Joined: Wed Oct 12, 2011 10:52 am

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

Post by ITEngineer » Tue Aug 28, 2018 11:37 pm

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 */

User avatar
jvierra
Posts: 13718
Joined: Tue May 22, 2007 9:57 am
Contact:

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

Post by jvierra » Wed Aug 29, 2018 12:05 am

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.

Locked