Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

PowerShell task should configure logging streams #15069

Merged
Show file tree
Hide file tree
Changes from 19 commits
Commits
Show all changes
24 commits
Select commit Hold shift + click to select a range
5f13f83
PowerShell task should configure logging streams
Jul 16, 2021
458fc7c
Bump task version
Jul 16, 2021
ff6ab60
Update tests
Jul 22, 2021
597db45
Merge branch 'master' into dev/bradwhit/PowerShell/ConfigureLoggingSt…
Jul 22, 2021
9f7ad2c
Merge branch 'master' into dev/bradwhit/PowerShell/ConfigureLoggingSt…
EzzhevNikita Jul 26, 2021
d55ddc9
Forgot to add VerbosePreference to tests
Jul 26, 2021
6118b9f
Addressing feedback
Aug 4, 2021
2b427d9
Merge branch 'master' into dev/bradwhit/PowerShell/ConfigureLoggingSt…
bradselw Aug 4, 2021
42edd28
Make preferences adjustable via task inputs
Aug 9, 2021
90faa32
Merge branch 'master' into dev/bradwhit/PowerShell/ConfigureLoggingSt…
bradselw Aug 9, 2021
f06ab26
Merge branch 'master' into dev/bradwhit/PowerShell/ConfigureLoggingSt…
Aug 10, 2021
b03d6ca
Merge branch 'master' into dev/bradwhit/PowerShell/ConfigureLoggingSt…
bradselw Aug 13, 2021
333891a
Address PR feedback
Aug 13, 2021
3362421
Trying to fix resource files
Aug 13, 2021
3960fb8
Address feedback
Sep 13, 2021
550f001
Update sprint version
Sep 13, 2021
f335670
Merge branch 'master' into dev/bradwhit/PowerShell/ConfigureLoggingSt…
Sep 13, 2021
37c81dc
Fix tests
Sep 13, 2021
f3d4a3e
Merge branch 'master' into dev/bradwhit/PowerShell/ConfigureLoggingSt…
Sep 14, 2021
0332bc8
Merge branch 'master' into dev/bradwhit/PowerShell/ConfigureLoggingSt…
Sep 16, 2021
9db79a6
Bump to version 2.194.0
Sep 16, 2021
05614fa
Merge branch 'master' into dev/bradwhit/PowerShell/ConfigureLoggingSt…
Sep 17, 2021
c80068f
Merge branch 'master' into dev/bradwhit/PowerShell/ConfigureLoggingSt…
bradselw Sep 20, 2021
1fe909e
Merge branch 'master' into dev/bradwhit/PowerShell/ConfigureLoggingSt…
Sep 21, 2021
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
"loc.description": "Run a PowerShell script on Linux, macOS, or Windows",
"loc.instanceNameFormat": "PowerShell Script",
"loc.releaseNotes": "Script task consistency. Added support for macOS and Linux.",
"loc.group.displayName.preferenceVariables": "Preference Variables",
"loc.group.displayName.advanced": "Advanced",
"loc.input.label.targetType": "Type",
"loc.input.help.targetType": "Target script type: File Path or Inline",
Expand All @@ -13,7 +14,15 @@
"loc.input.help.arguments": "Arguments passed to the PowerShell script. Either ordinal parameters or named parameters.",
"loc.input.label.script": "Script",
"loc.input.label.errorActionPreference": "ErrorActionPreference",
"loc.input.help.errorActionPreference": "Prepends the line `$ErrorActionPreference = 'VALUE'` at the top of your script.",
"loc.input.label.warningPreference": "WarningPreference",
"loc.input.label.informationPreference": "InformationPreference",
"loc.input.label.verbosePreference": "VerbosePreference",
"loc.input.label.debugPreference": "DebugPreference",
"loc.input.help.errorActionPreference": "When not `Default`, prepends the line `$ErrorActionPreference = 'VALUE'` at the top of your script.",
"loc.input.help.warningPreference": "When not `Default`, prepends the line `$WarningPreference = 'VALUE'` at the top of your script.",
"loc.input.help.informationPreference": "When not `Default`, prepends the line `$InformationPreference = 'VALUE'` at the top of your script.",
"loc.input.help.verbosePreference": "When not `Default`, prepends the line `$VerbosePreference = 'VALUE'` at the top of your script.",
"loc.input.help.debugPreference": "When not `Default`, prepends the line `$DebugPreference = 'VALUE'` at the top of your script.",
"loc.input.label.failOnStderr": "Fail on Standard Error",
"loc.input.help.failOnStderr": "If this is true, this task will fail if any errors are written to the error pipeline, or if any data is written to the Standard Error stream. Otherwise the task will rely on the exit code to determine failure.",
"loc.input.label.showWarnings": "Show warnings as Azure DevOps warnings",
Expand All @@ -29,13 +38,13 @@
"loc.messages.GeneratingScript": "Generating script.",
"loc.messages.JS_ExitCode": "PowerShell exited with code '%s'.",
"loc.messages.JS_FormattedCommand": "Formatted command: %s",
"loc.messages.JS_InvalidErrorActionPreference": "Invalid ErrorActionPreference '%s'. The value must be one of: 'Stop', 'Continue', or 'SilentlyContinue'",
"loc.messages.JS_InvalidActionPreference": "Invalid action preference for %s: '%s'. The value must be one of: %s",
"loc.messages.JS_InvalidFilePath": "Invalid file path '%s'. A path to a .ps1 file is required.",
"loc.messages.JS_Stderr": "PowerShell wrote one or more lines to the standard error stream.",
"loc.messages.JS_InvalidTargetType": "Invalid target type '%s'. The value must be one of: 'filepath' or 'inline'",
"loc.messages.PS_ExitCode": "PowerShell exited with code '{0}'.",
"loc.messages.PS_FormattedCommand": "Formatted command: {0}",
"loc.messages.PS_InvalidErrorActionPreference": "Invalid ErrorActionPreference '{0}'. The value must be one of: 'Stop', 'Continue', or 'SilentlyContinue'",
"loc.messages.PS_InvalidActionPreference": "Invalid action preference for {0}: '{1}'. The value must be one of: {2}",
"loc.messages.PS_InvalidFilePath": "Invalid file path '{0}'. A path to a .ps1 file is required.",
"loc.messages.PS_UnableToDetermineExitCode": "Unexpected exception. Unable to determine the exit code from powershell.",
"loc.messages.PS_InvalidTargetType": "Invalid target type '{0}'. The value must be one of: 'filepath' or 'inline'"
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
"loc.releaseNotes": "Cohérence de la tâche de script. Ajout de la prise en charge de macOS et de Linux.",
"loc.group.displayName.advanced": "Avancé",
"loc.input.label.targetType": "Type",
"loc.input.help.targetType": "Type de script cible : chemin de fichier ou inline",
"loc.input.help.targetType": "Type de script cible : chemin de fichier ou inline",
"loc.input.label.filePath": "Chemin du script",
"loc.input.help.filePath": "Chemin du script à exécuter. Il doit s'agir d'un chemin complet ou d'un chemin relatif à $(System.DefaultWorkingDirectory).",
"loc.input.label.arguments": "Arguments",
Expand All @@ -23,13 +23,13 @@
"loc.input.label.workingDirectory": "Répertoire de travail",
"loc.input.help.workingDirectory": "Répertoire de travail où le script est exécuté.",
"loc.messages.GeneratingScript": "Génération du script.",
"loc.messages.JS_ExitCode": "Arrêt de PowerShell. Code de sortie : '%s'.",
"loc.messages.JS_FormattedCommand": "Commande mise en forme : %s",
"loc.messages.JS_ExitCode": "Arrêt de PowerShell. Code de sortie : '%s'.",
"loc.messages.JS_FormattedCommand": "Commande mise en forme : %s",
"loc.messages.JS_InvalidErrorActionPreference": "ErrorActionPreference '%s' non valide. La valeur doit correspondre à 'Stop', 'Continue' ou 'SilentlyContinue'",
"loc.messages.JS_InvalidFilePath": "Chemin de fichier non valide : '%s'. Le chemin d'un fichier .ps1 est obligatoire.",
"loc.messages.JS_InvalidFilePath": "Chemin de fichier non valide : '%s'. Le chemin d'un fichier .ps1 est obligatoire.",
"loc.messages.JS_Stderr": "PowerShell a écrit une ou plusieurs lignes dans le flux d'erreurs standard.",
"loc.messages.PS_ExitCode": "Arrêt de PowerShell. Code de sortie : '{0}'.",
"loc.messages.PS_FormattedCommand": "Commande mise en forme : {0}",
"loc.messages.PS_ExitCode": "Arrêt de PowerShell. Code de sortie : '{0}'.",
"loc.messages.PS_FormattedCommand": "Commande mise en forme : {0}",
"loc.messages.PS_InvalidErrorActionPreference": "ErrorActionPreference '{0}' non valide. La valeur doit correspondre à 'Stop', 'Continue' ou 'SilentlyContinue'",
"loc.messages.PS_InvalidFilePath": "Chemin de fichier '{0}' non valide. Le chemin d'un fichier .ps1 est obligatoire.",
"loc.messages.PS_UnableToDetermineExitCode": "Exception inattendue. Impossible de déterminer le code de sortie de PowerShell."
Expand Down
55 changes: 45 additions & 10 deletions Tasks/PowerShellV2/powershell.ps1
Original file line number Diff line number Diff line change
@@ -1,20 +1,41 @@
[CmdletBinding()]
param()

function Get-ActionPreference {
param (
[Parameter(Mandatory)]
[string]
$VstsInputName,

[Parameter()]
[string]
$DefaultAction = 'Default',

[Parameter()]
[string[]]
$ValidActions = @( 'Default', 'Stop', 'Continue', 'SilentlyContinue' )
)

$result = Get-VstsInput -Name $VstsInputName -Default $DefaultAction

if (-not $ValidActions -contains $result) {
Write-Error (Get-VstsLocString -Key 'PS_InvalidActionPreference' -ArgumentList @( $VstsInputName, $result, ($ValidActions -join ', ') ))
}

return $result
}

Trace-VstsEnteringInvocation $MyInvocation
try {
Import-VstsLocStrings "$PSScriptRoot\task.json"

# Get inputs.
$input_errorActionPreference = Get-VstsInput -Name 'errorActionPreference' -Default 'Stop'
switch ($input_errorActionPreference.ToUpperInvariant()) {
'STOP' { }
'CONTINUE' { }
'SILENTLYCONTINUE' { }
default {
Write-Error (Get-VstsLocString -Key 'PS_InvalidErrorActionPreference' -ArgumentList $input_errorActionPreference)
}
}
$input_errorActionPreference = Get-ActionPreference -VstsInputName 'errorActionPreference' -DefaultAction 'Stop'
$input_warningPreference = Get-ActionPreference -VstsInputName 'warningPreference' -DefaultAction 'Default'
$input_informationPreference = Get-ActionPreference -VstsInputName 'informationPreference' -DefaultAction 'Default'
$input_verbosePreference = Get-ActionPreference -VstsInputName 'verbosePreference' -DefaultAction 'Default'
$input_debugPreference = Get-ActionPreference -VstsInputName 'debugPreference' -DefaultAction 'Default'

$input_showWarnings = Get-VstsInput -Name 'showWarnings' -AsBool
$input_failOnStderr = Get-VstsInput -Name 'failOnStderr' -AsBool
$input_ignoreLASTEXITCODE = Get-VstsInput -Name 'ignoreLASTEXITCODE' -AsBool
Expand Down Expand Up @@ -48,7 +69,21 @@ try {
# Generate the script contents.
Write-Host (Get-VstsLocString -Key 'GeneratingScript')
$contents = @()
$contents += "`$ErrorActionPreference = '$input_errorActionPreference'"
if ($input_errorActionPreference -ne 'Default') {
$contents += "`$ErrorActionPreference = '$input_errorActionPreference'"
}
if ($input_warningPreference -ne 'Default') {
$contents += "`$WarningPreference = '$input_warningPreference'"
}
if ($input_informationPreference -ne 'Default') {
$contents += "`$InformationPreference = '$input_informationPreference'"
}
if ($input_verbosePreference -ne 'Default') {
$contents += "`$VerbosePreference = '$input_verbosePreference'"
}
if ($input_debugPreference -ne 'Default') {
$contents += "`$DebugPreference = '$input_debugPreference'"
}
# Change default error view to normal view. We need this for error handling since we pipe stdout and stderr to the same stream
# and we rely on PowerShell piping back NormalView error records (required because PowerShell Core changed the default to ConciseView)
$contents += "`$ErrorView = 'NormalView'"
Expand Down
40 changes: 30 additions & 10 deletions Tasks/PowerShellV2/powershell.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,20 +5,26 @@ import tl = require('azure-pipelines-task-lib/task');
import tr = require('azure-pipelines-task-lib/toolrunner');
var uuidV4 = require('uuid/v4');

function getActionPreference(vstsInputName: string, defaultAction: string = 'Default', validActions: string[] = [ 'Default', 'Stop', 'Continue', 'SilentlyContinue' ]) {
let result: string = tl.getInput(vstsInputName, false) || defaultAction;

if (validActions.map(actionPreference => actionPreference.toUpperCase()).indexOf(result.toUpperCase()) < 0) {
throw new Error(tl.loc('JS_InvalidActionPreference', vstsInputName, result, validActions.join(', ')))
}

return result
}

async function run() {
try {
tl.setResourcePath(path.join(__dirname, 'task.json'));

// Get inputs.
let input_errorActionPreference: string = tl.getInput('errorActionPreference', false) || 'Stop';
switch (input_errorActionPreference.toUpperCase()) {
case 'STOP':
case 'CONTINUE':
case 'SILENTLYCONTINUE':
break;
default:
throw new Error(tl.loc('JS_InvalidErrorActionPreference', input_errorActionPreference));
}
let input_errorActionPreference: string = getActionPreference('errorActionPreference', 'Stop');
let input_warningPreference: string = getActionPreference('warningPreference', 'Default');
let input_informationPreference: string = getActionPreference('informationPreference', 'Default');
let input_verbosePreference: string = getActionPreference('verbosePreference', 'Default');
let input_debugPreference: string = getActionPreference('debugPreference', 'Default');
let input_showWarnings = tl.getBoolInput('showWarnings', false);
let input_failOnStderr = tl.getBoolInput('failOnStderr', false);
let input_ignoreLASTEXITCODE = tl.getBoolInput('ignoreLASTEXITCODE', false);
Expand Down Expand Up @@ -46,7 +52,21 @@ async function run() {
// Generate the script contents.
console.log(tl.loc('GeneratingScript'));
let contents: string[] = [];
contents.push(`$ErrorActionPreference = '${input_errorActionPreference}'`);
if (input_errorActionPreference.toUpperCase() != 'DEFAULT') {
contents.push(`$ErrorActionPreference = '${input_errorActionPreference}'`);
}
if (input_warningPreference.toUpperCase() != 'DEFAULT') {
contents.push(`$WarningPreference = '${input_warningPreference}'`);
}
if (input_informationPreference.toUpperCase() != 'DEFAULT') {
contents.push(`$InformationPreference = '${input_informationPreference}'`);
}
if (input_verbosePreference.toUpperCase() != 'DEFAULT') {
contents.push(`$VerbosePreference = '${input_verbosePreference}'`);
}
if (input_debugPreference.toUpperCase() != 'DEFAULT') {
contents.push(`$DebugPreference = '${input_debugPreference}'`);
}
let script = '';
if (input_targetType.toUpperCase() == 'FILEPATH') {
script = `. '${input_filePath.replace(/'/g, "''")}' ${input_arguments}`.trim();
Expand Down
75 changes: 71 additions & 4 deletions Tasks/PowerShellV2/task.json
Original file line number Diff line number Diff line change
Expand Up @@ -17,13 +17,18 @@
"author": "Microsoft Corporation",
"version": {
"Major": 2,
"Minor": 190,
"Minor": 193,
"Patch": 0
},
"releaseNotes": "Script task consistency. Added support for macOS and Linux.",
"minimumAgentVersion": "2.115.0",
"showEnvironmentVariables": true,
"groups": [
{
"name": "preferenceVariables",
"displayName": "Preference Variables",
"isExpanded": false
},
{
"name": "advanced",
"displayName": "Advanced",
Expand Down Expand Up @@ -82,11 +87,73 @@
"required": false,
"defaultValue": "stop",
"options": {
"default": "Default",
"stop": "Stop",
"continue": "Continue",
"silentlyContinue": "SilentlyContinue"
},
"helpMarkDown": "When not `Default`, prepends the line `$ErrorActionPreference = 'VALUE'` at the top of your script.",
"groupName": "preferenceVariables"
},
{
"name": "warningPreference",
"type": "pickList",
"label": "WarningPreference",
"required": false,
"defaultValue": "default",
"options": {
"default": "Default",
"stop": "Stop",
"continue": "Continue",
"silentlyContinue": "SilentlyContinue"
},
"helpMarkDown": "When not `Default`, prepends the line `$WarningPreference = 'VALUE'` at the top of your script.",
"groupName": "preferenceVariables"
},
{
"name": "informationPreference",
"type": "pickList",
"label": "InformationPreference",
"required": false,
"defaultValue": "default",
"options": {
"default": "Default",
"stop": "Stop",
"continue": "Continue",
"silentlyContinue": "SilentlyContinue"
},
"helpMarkDown": "When not `Default`, prepends the line `$InformationPreference = 'VALUE'` at the top of your script.",
"groupName": "preferenceVariables"
},
{
"name": "verbosePreference",
"type": "pickList",
"label": "VerbosePreference",
"required": false,
"defaultValue": "default",
"options": {
"default": "Default",
"stop": "Stop",
"continue": "Continue",
"silentlyContinue": "SilentlyContinue"
},
"helpMarkDown": "When not `Default`, prepends the line `$VerbosePreference = 'VALUE'` at the top of your script.",
"groupName": "preferenceVariables"
},
{
"name": "debugPreference",
"type": "pickList",
"label": "DebugPreference",
"required": false,
"defaultValue": "default",
"options": {
"default": "Default",
"stop": "Stop",
"continue": "Continue",
"silentlyContinue": "SilentlyContinue"
},
"helpMarkDown": "Prepends the line `$ErrorActionPreference = 'VALUE'` at the top of your script."
"helpMarkDown": "When not `Default`, prepends the line `$DebugPreference = 'VALUE'` at the top of your script.",
"groupName": "preferenceVariables"
},
{
"name": "failOnStderr",
Expand Down Expand Up @@ -160,13 +227,13 @@
"GeneratingScript": "Generating script.",
"JS_ExitCode": "PowerShell exited with code '%s'.",
"JS_FormattedCommand": "Formatted command: %s",
"JS_InvalidErrorActionPreference": "Invalid ErrorActionPreference '%s'. The value must be one of: 'Stop', 'Continue', or 'SilentlyContinue'",
"JS_InvalidActionPreference": "Invalid action preference for %s: '%s'. The value must be one of: %s",
"JS_InvalidFilePath": "Invalid file path '%s'. A path to a .ps1 file is required.",
"JS_Stderr": "PowerShell wrote one or more lines to the standard error stream.",
"JS_InvalidTargetType": "Invalid target type '%s'. The value must be one of: 'filepath' or 'inline'",
"PS_ExitCode": "PowerShell exited with code '{0}'.",
"PS_FormattedCommand": "Formatted command: {0}",
"PS_InvalidErrorActionPreference": "Invalid ErrorActionPreference '{0}'. The value must be one of: 'Stop', 'Continue', or 'SilentlyContinue'",
"PS_InvalidActionPreference": "Invalid action preference for {0}: '{1}'. The value must be one of: {2}",
"PS_InvalidFilePath": "Invalid file path '{0}'. A path to a .ps1 file is required.",
"PS_UnableToDetermineExitCode": "Unexpected exception. Unable to determine the exit code from powershell.",
"PS_InvalidTargetType": "Invalid target type '{0}'. The value must be one of: 'filepath' or 'inline'"
Expand Down
Loading