forked from Azure/login
-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathScriptBuilder.ts
More file actions
50 lines (46 loc) · 2.04 KB
/
Copy pathScriptBuilder.ts
File metadata and controls
50 lines (46 loc) · 2.04 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
import * as core from '@actions/core';
import Constants from "../Constants";
export default class ScriptBuilder {
script: string = "";
getAzPSLoginScript(scheme: string, tenantId: string, args: any): string {
let command = `Clear-AzContext -Scope Process;
Clear-AzContext -Scope CurrentUser -Force -ErrorAction SilentlyContinue;`;
if (scheme === Constants.ServicePrincipal) {
command += `Connect-AzAccount -ServicePrincipal -Tenant ${tenantId} -Credential \
(New-Object System.Management.Automation.PSCredential('${args.servicePrincipalId}',(ConvertTo-SecureString ${args.servicePrincipalKey} -AsPlainText -Force))) \
-Environment ${args.environment};`;
if (args.scopeLevel === Constants.Subscription) {
command += `Set-AzContext -SubscriptionId ${args.subscriptionId} -TenantId ${tenantId};`;
}
}
command += `Get-AzContext`;
this.script += `try {
$$ErrorActionPreference = "Stop";
$$output = @{};
${command}
$$output['${Constants.Success}'] = "true";
}
catch {
$$output['${Constants.Error}'] = $$_.exception.Message;
}
return ConvertTo-Json $$a`;
core.debug(`Azure PowerShell Login Script: ${this.script}`);
return this.script;
}
getLatestModuleScript(moduleName: string): string {
const command: string = `Get-Module -Name ${moduleName} -ListAvailable | Sort-Object Version -Descending | Select-Object -First 1`;
this.script += `try {
$$ErrorActionPreference = "Stop";
$$output = @{};
$$data = ${command};
$$output['${Constants.AzVersion}'] = $$data.Version.ToString();
$$output['${Constants.Success}'] = "true";
}
catch {
$$output['${Constants.Error}'] = $$_.exception.Message;
}
return ConvertTo-Json $$a`;
core.debug(`GetLatestModuleScript: ${this.script}`);
return this.script;
}
}