login/lib/PowerShell/Utilities/ScriptBuilder.js
2020-03-19 19:27:30 +05:30

61 lines
2.6 KiB
JavaScript

"use strict";
var __importStar = (this && this.__importStar) || function (mod) {
if (mod && mod.__esModule) return mod;
var result = {};
if (mod != null) for (var k in mod) if (Object.hasOwnProperty.call(mod, k)) result[k] = mod[k];
result["default"] = mod;
return result;
};
var __importDefault = (this && this.__importDefault) || function (mod) {
return (mod && mod.__esModule) ? mod : { "default": mod };
};
Object.defineProperty(exports, "__esModule", { value: true });
const core = __importStar(require("@actions/core"));
const Constants_1 = __importDefault(require("../Constants"));
class ScriptBuilder {
constructor() {
this.script = "";
}
getAzPSLoginScript(scheme, tenantId, args) {
let command = `Clear-AzContext -Scope Process;
Clear-AzContext -Scope CurrentUser -Force -ErrorAction SilentlyContinue;`;
if (scheme === Constants_1.default.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_1.default.Subscription) {
command += `Set-AzContext -SubscriptionId ${args.subscriptionId} -TenantId ${tenantId};`;
}
}
command += `Get-AzContext`;
this.script += `try {
$$ErrorActionPreference = "Stop";
$$output = @{};
${command}
$$output['${Constants_1.default.Success}'] = "true";
}
catch {
$$output['${Constants_1.default.Error}'] = $$_.exception.Message;
}
return ConvertTo-Json $$a`;
core.debug(`Azure PowerShell Login Script: ${this.script}`);
return this.script;
}
getLatestModuleScript(moduleName) {
const command = `Get-Module -Name ${moduleName} -ListAvailable | Sort-Object Version -Descending | Select-Object -First 1`;
this.script += `try {
$$ErrorActionPreference = "Stop";
$$output = @{};
$$data = ${command};
$$output['${Constants_1.default.AzVersion}'] = $$data.Version.ToString();
$$output['${Constants_1.default.Success}'] = "true";
}
catch {
$$output['${Constants_1.default.Error}'] = $$_.exception.Message;
}
return ConvertTo-Json $$a`;
core.debug(`GetLatestModuleScript: ${this.script}`);
return this.script;
}
}
exports.default = ScriptBuilder;