Skip to content

Commit 8baa7bb

Browse files
authored
Adding inline argument to PowerShell script (#11503) (#11550)
* Adding inline argument to powershell script * Resolving PR comments
1 parent 427567c commit 8baa7bb

File tree

3 files changed

+22
-23
lines changed

3 files changed

+22
-23
lines changed

Tasks/AzureCLIV2/src/Utility.ts

Lines changed: 20 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -14,9 +14,9 @@ export class Utility {
1414
}
1515
throw new Error(tl.loc('JS_InvalidFilePath', filePath));
1616
}
17-
let tmpDir = tl.getVariable('Agent.TempDirectory') || os.tmpdir();
17+
let tempDirectory = tl.getVariable('Agent.TempDirectory') || os.tmpdir();
1818
let inlineScript: string = tl.getInput("inlineScript", true);
19-
let scriptPath: string = path.join(tmpDir, `azureclitaskscript${new Date().getTime()}.${fileExtensions[0]}`);
19+
let scriptPath: string = path.join(tempDirectory, `azureclitaskscript${new Date().getTime()}.${fileExtensions[0]}`);
2020
await Utility.createFile(scriptPath, inlineScript);
2121
return scriptPath;
2222
}
@@ -32,26 +32,30 @@ export class Utility {
3232
throw new Error(tl.loc('JS_InvalidErrorActionPreference', powerShellErrorActionPreference));
3333
}
3434

35+
// Write the script to disk.
36+
tl.assertAgent('2.115.0');
37+
let tempDirectory = tl.getVariable('Agent.TempDirectory') || os.tmpdir();
38+
3539
let contents: string[] = [];
3640
contents.push(`$ErrorActionPreference = '${powerShellErrorActionPreference}'`);
37-
38-
if (scriptLocation === "scriptPath") {
39-
let filePath: string = tl.getPathInput("scriptPath", true, true);
40-
if (Utility.checkIfFileExists(filePath, fileExtensions)) {
41-
let content: string = `. '${filePath.replace("'", "''")}' `;
42-
if (scriptArguments) {
43-
content += scriptArguments;
44-
}
45-
contents.push(content.trim());
46-
}
47-
else {
41+
let filePath: string = tl.getPathInput("scriptPath", false, true);;
42+
if (scriptLocation === "inlineScript") {
43+
let inlineScript: string = tl.getInput("inlineScript", true);
44+
filePath = path.join(tempDirectory, `azureclitaskscript${new Date().getTime()}_inlinescript.${fileExtensions[0]}`);
45+
await Utility.createFile(filePath, inlineScript);
46+
}
47+
else{
48+
if (!Utility.checkIfFileExists(filePath, fileExtensions)) {
4849
throw new Error(tl.loc('JS_InvalidFilePath', filePath));
4950
}
5051
}
51-
else {
52-
let inlineScript: string = tl.getInput("inlineScript", true);
53-
contents.push(inlineScript);
52+
53+
let content: string = `. '${filePath.replace("'", "''")}' `;
54+
if (scriptArguments) {
55+
content += scriptArguments;
5456
}
57+
contents.push(content.trim());
58+
5559
let powerShellIgnoreLASTEXITCODE: string = tl.getInput('powerShellIgnoreLASTEXITCODE', false);
5660
if (!powerShellIgnoreLASTEXITCODE) {
5761
contents.push(`if (!(Test-Path -LiteralPath variable:\LASTEXITCODE)) {`);
@@ -62,12 +66,7 @@ export class Utility {
6266
contents.push(`}`);
6367
}
6468

65-
// Write the script to disk.
66-
tl.assertAgent('2.115.0');
67-
let tempDirectory = tl.getVariable('agent.tempDirectory');
68-
tl.checkPath(tempDirectory, `${tempDirectory} (agent.tempDirectory)`);
6969
let scriptPath: string = path.join(tempDirectory, `azureclitaskscript${new Date().getTime()}.${fileExtensions[0]}`);
70-
7170
await Utility.createFile(scriptPath, '\ufeff' + contents.join(os.EOL), { encoding: 'utf8' });
7271
return scriptPath;
7372
}

Tasks/AzureCLIV2/task.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@
2121
"version": {
2222
"Major": 2,
2323
"Minor": 0,
24-
"Patch": 2
24+
"Patch": 3
2525
},
2626
"minimumAgentVersion": "2.0.0",
2727
"instanceNameFormat": "Azure CLI $(scriptPath)",

Tasks/AzureCLIV2/task.loc.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@
2121
"version": {
2222
"Major": 2,
2323
"Minor": 0,
24-
"Patch": 2
24+
"Patch": 3
2525
},
2626
"minimumAgentVersion": "2.0.0",
2727
"instanceNameFormat": "ms-resource:loc.instanceNameFormat",

0 commit comments

Comments
 (0)