microsoft/TypeAgent
Publicmirrored fromhttps://github.com/microsoft/TypeAgentAvailable
ts/tools/scripts/elevate.js
71lines · modecode
| 1 | #!/usr/bin/env node |
| 2 | // Copyright (c) Microsoft Corporation. |
| 3 | // Licensed under the MIT License. |
| 4 | |
| 5 | const config = require("./elevate.config.json"); |
| 6 | |
| 7 | async function elevate(startDateTime) { |
| 8 | const { getClient } = await import("./lib/pimClient.mjs"); |
| 9 | |
| 10 | // get PIM client |
| 11 | const client = await getClient(); |
| 12 | |
| 13 | // elevate |
| 14 | await config.forEach((element) => { |
| 15 | var v = client.elevate({ |
| 16 | requestType: element.properties.RequestType, |
| 17 | roleName: element.roleName, |
| 18 | expirationType: element.properties.ScheduleInfo.Expiration.Type, |
| 19 | expirationDuration: |
| 20 | element.properties.ScheduleInfo.Expiration.Duration, |
| 21 | startDateTime: startDateTime, |
| 22 | }); |
| 23 | }); |
| 24 | } |
| 25 | |
| 26 | (async () => { |
| 27 | const command = process.argv[2]; |
| 28 | switch (command) { |
| 29 | case "help": |
| 30 | console.log("elevate"); |
| 31 | console.log( |
| 32 | "Uses the logged in account to elevate. Will prompt for login if the user is not logged in.", |
| 33 | ); |
| 34 | console.log("Uses configuration from elevate.config.json."); |
| 35 | return; |
| 36 | default: |
| 37 | let date; |
| 38 | if (command === "tomorrow_morning") { |
| 39 | date = new Date(); |
| 40 | date.setDate(date.getDate() + 1); |
| 41 | date.setHours(8, 0, 0); |
| 42 | } else if (command !== undefined && command !== "now") { |
| 43 | try { |
| 44 | date = new Date(Date.parse(command)); |
| 45 | } catch { |
| 46 | console.log( |
| 47 | `Unable to parse date '${command}'. The expected format is '2024-09-30T09:01:00'`, |
| 48 | ); |
| 49 | return; |
| 50 | } |
| 51 | } |
| 52 | |
| 53 | await elevate(date?.toISOString()); |
| 54 | break; |
| 55 | } |
| 56 | })().catch((e) => { |
| 57 | if ( |
| 58 | e.message.includes( |
| 59 | "'az' is not recognized as an internal or external command", |
| 60 | ) |
| 61 | ) { |
| 62 | console.error( |
| 63 | `ERROR: Azure CLI is not installed. Install it and run 'az login' before running this tool.`, |
| 64 | ); |
| 65 | |
| 66 | exit(0); |
| 67 | } |
| 68 | |
| 69 | console.error(`FATAL ERROR: ${e.stack}`); |
| 70 | process.exit(-1); |
| 71 | }); |
| 72 | |