Skip to content

Commit ad3250b

Browse files
committed
Added changes for review comments
1 parent a2be320 commit ad3250b

9 files changed

Lines changed: 51 additions & 139 deletions

File tree

lib/PowerShell/ServicePrincipalLogin.js

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,7 @@ class ServicePrincipalLogin {
3636
Utils_1.default.setPSModulePath();
3737
const script = new ScriptBuilder_1.default().getLatestModuleScript(Constants_1.default.moduleName);
3838
const outputJson = yield this.getLatestModule(script);
39-
const azLatestVersion = outputJson.Constants.AzPSVersion;
39+
const azLatestVersion = outputJson[Constants_1.default.AzVersion];
4040
if (!(Constants_1.default.Success in outputJson) || !Utils_1.default.isValidVersion(azLatestVersion)) {
4141
throw new Error(`Invalid AzPSVersion: ${azLatestVersion}`);
4242
}
@@ -55,7 +55,7 @@ class ServicePrincipalLogin {
5555
}
5656
};
5757
yield PowerShellToolRunner_1.default.init();
58-
yield PowerShellToolRunner_1.default.executePowerShellCommand(script, options);
58+
yield PowerShellToolRunner_1.default.executePowerShellScriptBlock(script, options);
5959
return JSON.parse(output.trim());
6060
});
6161
}
@@ -78,10 +78,10 @@ class ServicePrincipalLogin {
7878
};
7979
const script = new ScriptBuilder_1.default().getAzPSLoginScript(ServicePrincipalLogin.scheme, this.tenantId, args);
8080
yield PowerShellToolRunner_1.default.init();
81-
yield PowerShellToolRunner_1.default.executePowerShellCommand(script, options);
81+
yield PowerShellToolRunner_1.default.executePowerShellScriptBlock(script, options);
8282
const outputJson = JSON.parse(output.trim());
8383
if (!(Constants_1.default.Success in outputJson)) {
84-
throw new Error(`Azure PowerShell login failed with error: ${outputJson.Constants.Error}`);
84+
throw new Error(`Azure PowerShell login failed with error: ${outputJson[Constants_1.default.Error]}`);
8585
}
8686
});
8787
}

lib/PowerShell/Utilities/PowerShellToolRunner.js

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,11 @@ class PowerShellToolRunner {
2626
}
2727
});
2828
}
29+
static executePowerShellScriptBlock(scriptBlock, options = {}) {
30+
return __awaiter(this, void 0, void 0, function* () {
31+
yield exec.exec(`${PowerShellToolRunner.psPath} -Command`, [scriptBlock], options);
32+
});
33+
}
2934
static executePowerShellCommand(command, options = {}) {
3035
return __awaiter(this, void 0, void 0, function* () {
3136
yield exec.exec(`"${PowerShellToolRunner.psPath}" -Command "${command}"`, [], options);

lib/PowerShell/Utilities/ScriptBuilder.js

Lines changed: 16 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -22,38 +22,39 @@ class ScriptBuilder {
2222
if (scheme === Constants_1.default.ServicePrincipal) {
2323
command += `Connect-AzAccount -ServicePrincipal -Tenant ${tenantId} -Credential \
2424
(New-Object System.Management.Automation.PSCredential('${args.servicePrincipalId}',(ConvertTo-SecureString ${args.servicePrincipalKey} -AsPlainText -Force))) \
25-
-Environment ${args.environment};`;
25+
-Environment ${args.environment} | out-null;`;
2626
if (args.scopeLevel === Constants_1.default.Subscription) {
27-
command += `Set-AzContext -SubscriptionId ${args.subscriptionId} -TenantId ${tenantId};`;
27+
command += `Set-AzContext -SubscriptionId ${args.subscriptionId} -TenantId ${tenantId} | out-null;`;
2828
}
2929
}
30-
command += `Get-AzContext`;
3130
this.script += `try {
32-
$$ErrorActionPreference = "Stop";
33-
$$output = @{};
31+
$ErrorActionPreference = "Stop"
32+
$WarningPreference = "SilentlyContinue"
33+
$output = @{}
3434
${command}
35-
$$output['${Constants_1.default.Success}'] = "true";
35+
$output['${Constants_1.default.Success}'] = "true"
3636
}
3737
catch {
38-
$$output['${Constants_1.default.Error}'] = $$_.exception.Message;
38+
$output['${Constants_1.default.Error}'] = $_.exception.Message
3939
}
40-
return ConvertTo-Json $$a`;
40+
return ConvertTo-Json $output`;
4141
core.debug(`Azure PowerShell Login Script: ${this.script}`);
4242
return this.script;
4343
}
4444
getLatestModuleScript(moduleName) {
4545
const command = `Get-Module -Name ${moduleName} -ListAvailable | Sort-Object Version -Descending | Select-Object -First 1`;
4646
this.script += `try {
47-
$$ErrorActionPreference = "Stop";
48-
$$output = @{};
49-
$$data = ${command};
50-
$$output['${Constants_1.default.AzVersion}'] = $$data.Version.ToString();
51-
$$output['${Constants_1.default.Success}'] = "true";
47+
$ErrorActionPreference = "Stop"
48+
$WarningPreference = "SilentlyContinue"
49+
$output = @{}
50+
$data = ${command}
51+
$output['${Constants_1.default.AzVersion}'] = $data.Version.ToString()
52+
$output['${Constants_1.default.Success}'] = "true"
5253
}
5354
catch {
54-
$$output['${Constants_1.default.Error}'] = $$_.exception.Message;
55+
$output['${Constants_1.default.Error}'] = $_.exception.Message
5556
}
56-
return ConvertTo-Json $$a`;
57+
return ConvertTo-Json $output`;
5758
core.debug(`GetLatestModuleScript: ${this.script}`);
5859
return this.script;
5960
}

lib/PowerShell/Utilities/Utils.js

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -26,8 +26,7 @@ class Utils {
2626
break;
2727
case "macos":
2828
case "darwin":
29-
// TODO: add modulepath
30-
break;
29+
throw new Error(`OS not supported`);
3130
default:
3231
throw new Error(`Unknown os: ${runner.toLowerCase()}`);
3332
}

lib/loginAzurePowerShell.js

Lines changed: 0 additions & 96 deletions
This file was deleted.

src/PowerShell/ServicePrincipalLogin.ts

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@ export class ServicePrincipalLogin implements IAzurePowerShellSession {
2525
Utils.setPSModulePath();
2626
const script: string = new ScriptBuilder().getLatestModuleScript(Constants.moduleName);
2727
const outputJson = await this.getLatestModule(script);
28-
const azLatestVersion: string = outputJson.Constants.AzPSVersion;
28+
const azLatestVersion: string = outputJson[Constants.AzVersion];
2929
if (!(Constants.Success in outputJson) || !Utils.isValidVersion(azLatestVersion)) {
3030
throw new Error(`Invalid AzPSVersion: ${azLatestVersion}`);
3131
}
@@ -43,7 +43,7 @@ export class ServicePrincipalLogin implements IAzurePowerShellSession {
4343
}
4444
};
4545
await PowerShellToolRunner.init();
46-
await PowerShellToolRunner.executePowerShellCommand(script, options);
46+
await PowerShellToolRunner.executePowerShellScriptBlock(script, options);
4747
return JSON.parse(output.trim());
4848
}
4949

@@ -65,10 +65,10 @@ export class ServicePrincipalLogin implements IAzurePowerShellSession {
6565
}
6666
const script: string = new ScriptBuilder().getAzPSLoginScript(ServicePrincipalLogin.scheme, this.tenantId, args);
6767
await PowerShellToolRunner.init();
68-
await PowerShellToolRunner.executePowerShellCommand(script, options);
68+
await PowerShellToolRunner.executePowerShellScriptBlock(script, options);
6969
const outputJson: any = JSON.parse(output.trim());
7070
if (!(Constants.Success in outputJson)) {
71-
throw new Error(`Azure PowerShell login failed with error: ${outputJson.Constants.Error}`);
71+
throw new Error(`Azure PowerShell login failed with error: ${outputJson[Constants.Error]}`);
7272
}
7373
}
7474

src/PowerShell/Utilities/PowerShellToolRunner.ts

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,10 @@ export default class PowerShellToolRunner {
1010
}
1111
}
1212

13+
static async executePowerShellScriptBlock(scriptBlock: string, options: any = {}) {
14+
await exec.exec(`${PowerShellToolRunner.psPath} -Command`, [scriptBlock], options)
15+
}
16+
1317
static async executePowerShellCommand(command: string, options: any = {}) {
1418
await exec.exec(`"${PowerShellToolRunner.psPath}" -Command "${command}"`, [], options);
1519
}

src/PowerShell/Utilities/ScriptBuilder.ts

Lines changed: 16 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -11,39 +11,40 @@ export default class ScriptBuilder {
1111
if (scheme === Constants.ServicePrincipal) {
1212
command += `Connect-AzAccount -ServicePrincipal -Tenant ${tenantId} -Credential \
1313
(New-Object System.Management.Automation.PSCredential('${args.servicePrincipalId}',(ConvertTo-SecureString ${args.servicePrincipalKey} -AsPlainText -Force))) \
14-
-Environment ${args.environment};`;
14+
-Environment ${args.environment} | out-null;`;
1515
if (args.scopeLevel === Constants.Subscription) {
16-
command += `Set-AzContext -SubscriptionId ${args.subscriptionId} -TenantId ${tenantId};`;
16+
command += `Set-AzContext -SubscriptionId ${args.subscriptionId} -TenantId ${tenantId} | out-null;`;
1717
}
1818
}
19-
command += `Get-AzContext`;
2019
this.script += `try {
21-
$$ErrorActionPreference = "Stop";
22-
$$output = @{};
20+
$ErrorActionPreference = "Stop"
21+
$WarningPreference = "SilentlyContinue"
22+
$output = @{}
2323
${command}
24-
$$output['${Constants.Success}'] = "true";
24+
$output['${Constants.Success}'] = "true"
2525
}
2626
catch {
27-
$$output['${Constants.Error}'] = $$_.exception.Message;
27+
$output['${Constants.Error}'] = $_.exception.Message
2828
}
29-
return ConvertTo-Json $$a`;
29+
return ConvertTo-Json $output`;
3030
core.debug(`Azure PowerShell Login Script: ${this.script}`);
3131
return this.script;
3232
}
3333

3434
getLatestModuleScript(moduleName: string): string {
3535
const command: string = `Get-Module -Name ${moduleName} -ListAvailable | Sort-Object Version -Descending | Select-Object -First 1`;
3636
this.script += `try {
37-
$$ErrorActionPreference = "Stop";
38-
$$output = @{};
39-
$$data = ${command};
40-
$$output['${Constants.AzVersion}'] = $$data.Version.ToString();
41-
$$output['${Constants.Success}'] = "true";
37+
$ErrorActionPreference = "Stop"
38+
$WarningPreference = "SilentlyContinue"
39+
$output = @{}
40+
$data = ${command}
41+
$output['${Constants.AzVersion}'] = $data.Version.ToString()
42+
$output['${Constants.Success}'] = "true"
4243
}
4344
catch {
44-
$$output['${Constants.Error}'] = $$_.exception.Message;
45+
$output['${Constants.Error}'] = $_.exception.Message
4546
}
46-
return ConvertTo-Json $$a`;
47+
return ConvertTo-Json $output`;
4748
core.debug(`GetLatestModuleScript: ${this.script}`);
4849
return this.script;
4950
}

src/PowerShell/Utilities/Utils.ts

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
11
import * as os from 'os';
22

33
import Constants from '../Constants';
4-
import PowerShellToolRunner from './PowerShellToolRunner';
54

65
export default class Utils {
76
static setPSModulePath(azPSVersion: string = "") {
@@ -17,8 +16,7 @@ export default class Utils {
1716
break;
1817
case "macos":
1918
case "darwin":
20-
// TODO: add modulepath
21-
break;
19+
throw new Error(`OS not supported`);
2220
default:
2321
throw new Error(`Unknown os: ${runner.toLowerCase()}`);
2422
}

0 commit comments

Comments
 (0)