Azure DevOps task inline script with powershell does not fails the task on error - powershell

In the Azure DevOps release pipeline I run Inline Script as part of the Post Deployment Action of Azure App Service Deploy task (Task version 4.*).
The inline script looks like this:
powershell -NoProfile -NonInteractive -ExecutionPolicy Bypass -File UpdateCSP.ps1 -ApiUrl "$(API_URL) $(API_TESTING_URL)"
powershell -NoProfile -NonInteractive -ExecutionPolicy Bypass -File GenerateVarsJs.ps1 -ApiUrl "$(API_URL)" -IdamAuthServerUrl "$(AUTH_SERVER)" -IdamClientId "$(CLIENT_ID)" -TestApiUrl "$(API_TESTING_URL)"
One of the scripts was hard terminated (by invoking Throw "some error text") command. The error was written into the task output but the task itself succeeded.
Hard terminated powershell script returns error code 1. Therefore my expectation was that script failure would also fail the task.
I would need the task to fail in case of script failure (nobody is checking the output of succeeded task). How this could be done?

I carried out a simple test. And it worked fine.
I have a simple command throw "This is an error. in the .ps1 file. I call the script in the Post Deployment Action of Azure App Service Deploy task:
The task failed as expected:
However, You can have a try using logging commands #vso[task.complete result=Failed;]Failed in the .ps1 file to manually fail the task. See below:
Throw "some error text"
Write-Host "##vso[task.complete result=Failed;]Failed"
Or You can try using [Environment]::Exit($exitCode) after the Throw command in your .ps1 file.
Throw "some error text"
[Environment]::Exit(1)

Related

how to just trigger a powershell script from azure pipeline without waiting for result of script

I have been tasked to automate test cases using azure pipelines for two applications.
(one is using allure and other is maven).
pipeline uses "PowerShellOnTargetMachines" task to remote into azure windows vm and executes a powershell script containing the test case commands.
since I am using Microsoft hosted agent and these tests run for more than 2 hrs pipeline fails with time limit 60mins.
so I used start-process in the pipeline which will trigger the powershell script in vm as background process and return. no waiting for pipeline. But script is only partially successful doesn't run entire commands.
and for the other application which uses maven all I have to do is run this single command "mvn clean install" which runs test cases and generates reports. running remotely from pipeline is not generating reports properly.
pipeline code:
- task: PowerShellOnTargetMachines#3
inputs:
Machines: '$(machineurl)'
UserName: '$(username)'
UserPassword: '$(password)'
InlineScript: |
cd <path>
start-process powershell.exe -WindowStyle Hidden -ArgumentList ". .\allure-report-html.ps1"
allure-report-html.ps1 script contains:
cd <proj-path>
behave -f allure_behave.formatter:AllureFormatter -o testcasejson "features/main.feature" "features/announcements.feature"
allure generate testcasejson
allure-combine .\allure-report
$sdate = (Get-Date -format "yyyy_MM_dd hh.mm")
$report_path = '<report_path>'
$project_path = '<project_path>'
Compress-Archive -Path $project_path\$sdate -DestinationPath $report_path\$sdate.zip -Force
this works completely fine inside VM using same start-process command. but when running from pipeline I can only see "testcasejson" folder generated from behave command but I can't see "allure-report" folder which will be generated from allure commands.
I just need to trigger a PowerShell script in VM from pipeline without waiting for it to complete.
is there any other method to achieve this

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

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 not running via Task Schedular

I have the following script written and saved via PowerShell.
Invoke-RestMethod http://10.39.60.23:8085/home/index -Method GET
In the Task Scheduler within Action under Program/Script I have the following string
C:\Windows\System32\WindowsPowerShell\v1.0\powershell.exe
Lastly under Add Argument I have the following
C:\TSC.ps1
which is the location of the script.
Unfortunately the script does not return the data I expect it to.
I finally found a way out.
I had not changed the execution policy. So I ran the following command
Set-ExecutionPolicy RemoteSigned
Then with the same parameters in check under the Task Scheduler, it was able to run successfully.

security error when trying to run a powershell script from Task scheduler

I started by creating a new basic task in task scheduler: program/scripts: powershell.exe
The file path was put in “add arguments”: -noprofile -executionpolicy bypass D:\DATA-MEDION\soft_ontwikkeling\powershell\zendRecentPGP16.ps1 .
Before doing this I opened a powershell window as admin and have put the executionPolicy to unrestricted.
I still get the “cannot be loaded ...” security error when running the scheduled task. (Ultrashort powershell window opening and closing).
I ran the same script in a powerscript command box and in powershell ISE and it was OK.
Any idea's?