Skip to content

Commit a09e055

Browse files
authored
Authentication issue in AzurePowerShell task (#19550)
* code fixes * bumped task version * updated file as per changes * updating AzurePowerShell.ts file
1 parent acebd30 commit a09e055

16 files changed

+127
-37
lines changed

Tasks/AzurePowerShellV5/AzurePowerShell.ps1

+5-4
Original file line numberDiff line numberDiff line change
@@ -192,8 +192,9 @@ finally {
192192
if ($__vstsAzPSInlineScriptPath -and (Test-Path -LiteralPath $__vstsAzPSInlineScriptPath) ) {
193193
Remove-Item -LiteralPath $__vstsAzPSInlineScriptPath -ErrorAction 'SilentlyContinue'
194194
}
195-
196-
Import-Module $PSScriptRoot\ps_modules\VstsAzureHelpers_
195+
. "$PSScriptRoot\Utility.ps1"
196+
Import-Module "$PSScriptRoot\ps_modules\VstsAzureHelpers_"
197197
Remove-EndpointSecrets
198-
Disconnect-AzureAndClearContext -ErrorAction SilentlyContinue
199-
}
198+
Update-PSModulePathForHostedAgent
199+
Disconnect-AzureAndClearContext -restrictContext 'True' -ErrorAction SilentlyContinue
200+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
. "$PSScriptRoot/Utility.ps1"
2+
. "$PSScriptRoot/ps_modules/VstsAzureHelpers_/Utility.ps1"
3+
4+
Update-PSModulePathForHostedAgentLinux
5+
Disconnect-AzureAndClearContext -restrictContext 'True'

Tasks/AzurePowerShellV5/azurepowershell.ts

+27-3
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,9 @@ function convertToNullIfUndefined<T>(arg: T): T|null {
1212
}
1313

1414
async function run() {
15+
let input_workingDirectory = tl.getPathInput('workingDirectory', /*required*/ true, /*check*/ true);
16+
let tempDirectory = tl.getVariable('agent.tempDirectory');
17+
tl.checkPath(tempDirectory, `${tempDirectory} (agent.tempDirectory)`);
1518
try {
1619
tl.setResourcePath(path.join(__dirname, 'task.json'));
1720

@@ -35,7 +38,6 @@ async function run() {
3538
let customTargetAzurePs: string = convertToNullIfUndefined(tl.getInput('CustomTargetAzurePs', false));
3639
let serviceName = tl.getInput('ConnectedServiceNameARM',/*required*/true);
3740
let endpointObject= await new AzureRMEndpoint(serviceName).getEndpoint();
38-
let input_workingDirectory = tl.getPathInput('workingDirectory', /*required*/ true, /*check*/ true);
3941
let isDebugEnabled = (process.env['SYSTEM_DEBUG'] || "").toLowerCase() === "true";
4042

4143
// string constants
@@ -99,8 +101,6 @@ async function run() {
99101

100102
// Write the script to disk.
101103
tl.assertAgent('2.115.0');
102-
let tempDirectory = tl.getVariable('agent.tempDirectory');
103-
tl.checkPath(tempDirectory, `${tempDirectory} (agent.tempDirectory)`);
104104
let filePath = path.join(tempDirectory, uuidV4() + '.ps1');
105105

106106
await fs.writeFile(
@@ -160,6 +160,30 @@ async function run() {
160160
catch (err) {
161161
tl.setResult(tl.TaskResult.Failed, err.message || 'run() failed');
162162
}
163+
finally {
164+
try {
165+
const powershell = tl.tool(tl.which('pwsh') || tl.which('powershell') || tl.which('pwsh', true))
166+
.arg('-NoLogo')
167+
.arg('-NoProfile')
168+
.arg('-NonInteractive')
169+
.arg('-ExecutionPolicy')
170+
.arg('Unrestricted')
171+
.arg('-Command')
172+
.arg(`. '{path.join(path.resolve(__dirname),'RemoveAzContext.ps1')}'`);
173+
174+
let options = <tr.IExecOptions>{
175+
cwd: input_workingDirectory,
176+
failOnStdErr: false,
177+
errStream: process.stdout, // Direct all output to STDOUT, otherwise the output may appear out
178+
outStream: process.stdout, // of order since Node buffers it's own STDOUT but not STDERR.
179+
ignoreReturnCode: true
180+
};
181+
await powershell.exec(options);
182+
}
183+
catch (err) {
184+
tl.debug("Az-clearContext not completed due to an error");
185+
}
186+
}
163187
}
164188

165189
run();

Tasks/AzurePowerShellV5/task.json

+1-1
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@
1717
"author": "Microsoft Corporation",
1818
"version": {
1919
"Major": 5,
20-
"Minor": 234,
20+
"Minor": 236,
2121
"Patch": 0
2222
},
2323
"releaseNotes": "Added support for Az Module and cross platform agents.",

Tasks/AzurePowerShellV5/task.loc.json

+1-1
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@
1717
"author": "Microsoft Corporation",
1818
"version": {
1919
"Major": 5,
20-
"Minor": 234,
20+
"Minor": 236,
2121
"Patch": 0
2222
},
2323
"releaseNotes": "ms-resource:loc.releaseNotes",
+2-2
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,2 @@
1-
Default|5.234.0
2-
Node20_229_2|5.234.1
1+
Default|5.236.0
2+
Node20_229_2|5.236.1

_generated/AzurePowerShellV5/AzurePowerShell.ps1

+5-4
Original file line numberDiff line numberDiff line change
@@ -192,8 +192,9 @@ finally {
192192
if ($__vstsAzPSInlineScriptPath -and (Test-Path -LiteralPath $__vstsAzPSInlineScriptPath) ) {
193193
Remove-Item -LiteralPath $__vstsAzPSInlineScriptPath -ErrorAction 'SilentlyContinue'
194194
}
195-
196-
Import-Module $PSScriptRoot\ps_modules\VstsAzureHelpers_
195+
. "$PSScriptRoot\Utility.ps1"
196+
Import-Module "$PSScriptRoot\ps_modules\VstsAzureHelpers_"
197197
Remove-EndpointSecrets
198-
Disconnect-AzureAndClearContext -ErrorAction SilentlyContinue
199-
}
198+
Update-PSModulePathForHostedAgent
199+
Disconnect-AzureAndClearContext -restrictContext 'True' -ErrorAction SilentlyContinue
200+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
. "$PSScriptRoot/Utility.ps1"
2+
. "$PSScriptRoot/ps_modules/VstsAzureHelpers_/Utility.ps1"
3+
4+
Update-PSModulePathForHostedAgentLinux
5+
Disconnect-AzureAndClearContext -restrictContext 'True'

_generated/AzurePowerShellV5/azurepowershell.ts

+27-3
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,9 @@ function convertToNullIfUndefined<T>(arg: T): T|null {
1212
}
1313

1414
async function run() {
15+
let input_workingDirectory = tl.getPathInput('workingDirectory', /*required*/ true, /*check*/ true);
16+
let tempDirectory = tl.getVariable('agent.tempDirectory');
17+
tl.checkPath(tempDirectory, `${tempDirectory} (agent.tempDirectory)`);
1518
try {
1619
tl.setResourcePath(path.join(__dirname, 'task.json'));
1720

@@ -35,7 +38,6 @@ async function run() {
3538
let customTargetAzurePs: string = convertToNullIfUndefined(tl.getInput('CustomTargetAzurePs', false));
3639
let serviceName = tl.getInput('ConnectedServiceNameARM',/*required*/true);
3740
let endpointObject= await new AzureRMEndpoint(serviceName).getEndpoint();
38-
let input_workingDirectory = tl.getPathInput('workingDirectory', /*required*/ true, /*check*/ true);
3941
let isDebugEnabled = (process.env['SYSTEM_DEBUG'] || "").toLowerCase() === "true";
4042

4143
// string constants
@@ -99,8 +101,6 @@ async function run() {
99101

100102
// Write the script to disk.
101103
tl.assertAgent('2.115.0');
102-
let tempDirectory = tl.getVariable('agent.tempDirectory');
103-
tl.checkPath(tempDirectory, `${tempDirectory} (agent.tempDirectory)`);
104104
let filePath = path.join(tempDirectory, uuidV4() + '.ps1');
105105

106106
await fs.writeFile(
@@ -160,6 +160,30 @@ async function run() {
160160
catch (err) {
161161
tl.setResult(tl.TaskResult.Failed, err.message || 'run() failed');
162162
}
163+
finally {
164+
try {
165+
const powershell = tl.tool(tl.which('pwsh') || tl.which('powershell') || tl.which('pwsh', true))
166+
.arg('-NoLogo')
167+
.arg('-NoProfile')
168+
.arg('-NonInteractive')
169+
.arg('-ExecutionPolicy')
170+
.arg('Unrestricted')
171+
.arg('-Command')
172+
.arg(`. '{path.join(path.resolve(__dirname),'RemoveAzContext.ps1')}'`);
173+
174+
let options = <tr.IExecOptions>{
175+
cwd: input_workingDirectory,
176+
failOnStdErr: false,
177+
errStream: process.stdout, // Direct all output to STDOUT, otherwise the output may appear out
178+
outStream: process.stdout, // of order since Node buffers it's own STDOUT but not STDERR.
179+
ignoreReturnCode: true
180+
};
181+
await powershell.exec(options);
182+
}
183+
catch (err) {
184+
tl.debug("Az-clearContext not completed due to an error");
185+
}
186+
}
163187
}
164188

165189
run();

_generated/AzurePowerShellV5/task.json

+3-3
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@
1717
"author": "Microsoft Corporation",
1818
"version": {
1919
"Major": 5,
20-
"Minor": 234,
20+
"Minor": 236,
2121
"Patch": 0
2222
},
2323
"releaseNotes": "Added support for Az Module and cross platform agents.",
@@ -204,7 +204,7 @@
204204
"PS_ExitCode": "PowerShell exited with code '{0}'."
205205
},
206206
"_buildConfigMapping": {
207-
"Default": "5.234.0",
208-
"Node20_229_2": "5.234.1"
207+
"Default": "5.236.0",
208+
"Node20_229_2": "5.236.1"
209209
}
210210
}

_generated/AzurePowerShellV5/task.loc.json

+3-3
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@
1717
"author": "Microsoft Corporation",
1818
"version": {
1919
"Major": 5,
20-
"Minor": 234,
20+
"Minor": 236,
2121
"Patch": 0
2222
},
2323
"releaseNotes": "ms-resource:loc.releaseNotes",
@@ -204,7 +204,7 @@
204204
"PS_ExitCode": "ms-resource:loc.messages.PS_ExitCode"
205205
},
206206
"_buildConfigMapping": {
207-
"Default": "5.234.0",
208-
"Node20_229_2": "5.234.1"
207+
"Default": "5.236.0",
208+
"Node20_229_2": "5.236.1"
209209
}
210210
}

_generated/AzurePowerShellV5_Node20/AzurePowerShell.ps1

+5-4
Original file line numberDiff line numberDiff line change
@@ -192,8 +192,9 @@ finally {
192192
if ($__vstsAzPSInlineScriptPath -and (Test-Path -LiteralPath $__vstsAzPSInlineScriptPath) ) {
193193
Remove-Item -LiteralPath $__vstsAzPSInlineScriptPath -ErrorAction 'SilentlyContinue'
194194
}
195-
196-
Import-Module $PSScriptRoot\ps_modules\VstsAzureHelpers_
195+
. "$PSScriptRoot\Utility.ps1"
196+
Import-Module "$PSScriptRoot\ps_modules\VstsAzureHelpers_"
197197
Remove-EndpointSecrets
198-
Disconnect-AzureAndClearContext -ErrorAction SilentlyContinue
199-
}
198+
Update-PSModulePathForHostedAgent
199+
Disconnect-AzureAndClearContext -restrictContext 'True' -ErrorAction SilentlyContinue
200+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
. "$PSScriptRoot/Utility.ps1"
2+
. "$PSScriptRoot/ps_modules/VstsAzureHelpers_/Utility.ps1"
3+
4+
Update-PSModulePathForHostedAgentLinux
5+
Disconnect-AzureAndClearContext -restrictContext 'True'

_generated/AzurePowerShellV5_Node20/azurepowershell.ts

+27-3
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,9 @@ function convertToNullIfUndefined<T>(arg: T): T|null {
1212
}
1313

1414
async function run() {
15+
let input_workingDirectory = tl.getPathInput('workingDirectory', /*required*/ true, /*check*/ true);
16+
let tempDirectory = tl.getVariable('agent.tempDirectory');
17+
tl.checkPath(tempDirectory, `${tempDirectory} (agent.tempDirectory)`);
1518
try {
1619
tl.setResourcePath(path.join(__dirname, 'task.json'));
1720

@@ -35,7 +38,6 @@ async function run() {
3538
let customTargetAzurePs: string = convertToNullIfUndefined(tl.getInput('CustomTargetAzurePs', false));
3639
let serviceName = tl.getInput('ConnectedServiceNameARM',/*required*/true);
3740
let endpointObject= await new AzureRMEndpoint(serviceName).getEndpoint();
38-
let input_workingDirectory = tl.getPathInput('workingDirectory', /*required*/ true, /*check*/ true);
3941
let isDebugEnabled = (process.env['SYSTEM_DEBUG'] || "").toLowerCase() === "true";
4042

4143
// string constants
@@ -99,8 +101,6 @@ async function run() {
99101

100102
// Write the script to disk.
101103
tl.assertAgent('2.115.0');
102-
let tempDirectory = tl.getVariable('agent.tempDirectory');
103-
tl.checkPath(tempDirectory, `${tempDirectory} (agent.tempDirectory)`);
104104
let filePath = path.join(tempDirectory, uuidV4() + '.ps1');
105105

106106
await fs.writeFile(
@@ -160,6 +160,30 @@ async function run() {
160160
catch (err) {
161161
tl.setResult(tl.TaskResult.Failed, err.message || 'run() failed');
162162
}
163+
finally {
164+
try {
165+
const powershell = tl.tool(tl.which('pwsh') || tl.which('powershell') || tl.which('pwsh', true))
166+
.arg('-NoLogo')
167+
.arg('-NoProfile')
168+
.arg('-NonInteractive')
169+
.arg('-ExecutionPolicy')
170+
.arg('Unrestricted')
171+
.arg('-Command')
172+
.arg(`. '{path.join(path.resolve(__dirname),'RemoveAzContext.ps1')}'`);
173+
174+
let options = <tr.IExecOptions>{
175+
cwd: input_workingDirectory,
176+
failOnStdErr: false,
177+
errStream: process.stdout, // Direct all output to STDOUT, otherwise the output may appear out
178+
outStream: process.stdout, // of order since Node buffers it's own STDOUT but not STDERR.
179+
ignoreReturnCode: true
180+
};
181+
await powershell.exec(options);
182+
}
183+
catch (err) {
184+
tl.debug("Az-clearContext not completed due to an error");
185+
}
186+
}
163187
}
164188

165189
run();

_generated/AzurePowerShellV5_Node20/task.json

+3-3
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@
1717
"author": "Microsoft Corporation",
1818
"version": {
1919
"Major": 5,
20-
"Minor": 234,
20+
"Minor": 236,
2121
"Patch": 1
2222
},
2323
"releaseNotes": "Added support for Az Module and cross platform agents.",
@@ -208,7 +208,7 @@
208208
"PS_ExitCode": "PowerShell exited with code '{0}'."
209209
},
210210
"_buildConfigMapping": {
211-
"Default": "5.234.0",
212-
"Node20_229_2": "5.234.1"
211+
"Default": "5.236.0",
212+
"Node20_229_2": "5.236.1"
213213
}
214214
}

_generated/AzurePowerShellV5_Node20/task.loc.json

+3-3
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@
1717
"author": "Microsoft Corporation",
1818
"version": {
1919
"Major": 5,
20-
"Minor": 234,
20+
"Minor": 236,
2121
"Patch": 1
2222
},
2323
"releaseNotes": "ms-resource:loc.releaseNotes",
@@ -208,7 +208,7 @@
208208
"PS_ExitCode": "ms-resource:loc.messages.PS_ExitCode"
209209
},
210210
"_buildConfigMapping": {
211-
"Default": "5.234.0",
212-
"Node20_229_2": "5.234.1"
211+
"Default": "5.236.0",
212+
"Node20_229_2": "5.236.1"
213213
}
214214
}

0 commit comments

Comments
 (0)