UNPKG

@microsoft/windows-admin-center-sdk

Version:

Microsoft - Windows Admin Center Shell

133 lines (131 loc) 5.83 kB
import { PowerShell, PowerShellSession } from './powershell'; import { PowerShellBatch, PowerShellBatchSession } from './powershell-batch'; /** * The PowerShell Connection class. */ export class PowerShellConnection { lifetimeData; nodeConnection; batchConnection; /** * Initializes a new instance of the PowerShellConnection class. * * @param lifetimeService the lifetimeService class instance injected. * @param nodeConnection the nodeConnection class instance injected. * @param batchConnection the batchConnection class instance injected. */ constructor(lifetimeData, nodeConnection, batchConnection) { this.lifetimeData = lifetimeData; this.nodeConnection = nodeConnection; this.batchConnection = batchConnection; } createSession(nodeName, key, requestOptions) { if (MsftSme.isNullOrWhiteSpace(key)) { // [2/7/2019] Switch to automatic recycled session for instant type of session at default. if (requestOptions == null) { // if requestOptions is empty. return this.createAutomaticSession(nodeName, requestOptions); } else if (requestOptions.automatic !== false && requestOptions.authenticationMechanism !== 'Credssp' && requestOptions.powerShellEndpoint !== PowerShell.smePowerShellEndpoint) { // if implicitly not defined automatic = false AND not CredSSP mode AND not JEA mode. return this.createAutomaticSession(nodeName, requestOptions); } return new PowerShellSession(PowerShell.create(nodeName, this.nodeConnection, null, null, requestOptions)); } const lifeTime = this.lifetimeData.manager.createChildLifetime(); return new PowerShellSession(PowerShell.create(nodeName, this.nodeConnection, key, lifeTime, requestOptions), lifeTime); } /** * Create PowerShell Session object with auto managed pool of PowerShell runspace sessions. * - PowerShellSession object isn't required to be disposed. You can call dispose(), but the function is noop call. * * @param nodeName the name of the node to connect to. * @param requestOptions the options to apply to every request in this session. */ createAutomaticSession(nodeName, requestOptions) { requestOptions = requestOptions || {}; requestOptions = { ...requestOptions, ...{ automatic: true } }; return new PowerShellSession(PowerShell.create(nodeName, this.nodeConnection, null, null, requestOptions)); } /** * Runs the powershell command on the session. * * @param sessionOrNodeName the PowerShell session object or node name for automatic session. * @param command the PowerShell command. * @param options the PowerShell command options. */ run(sessionOrNodeName, scriptOrCommand, options) { const command = PowerShell.getPowerShellCommand(scriptOrCommand); if (typeof scriptOrCommand !== 'string' && !command.command) { const message = MsftSme.getStrings().MsftSmeShell.Core.Error.PowerShellRunCommandFormat.message; throw new Error(message); } let session; if (typeof sessionOrNodeName === 'string') { const nodeName = sessionOrNodeName; session = this.createAutomaticSession(nodeName, options); } else { session = sessionOrNodeName; } return session.powerShell.run(command, options); } /** * Find existing PowerShellSession by the node name and key. * * @param nodeName the node name. * @param key the key string. * @return PowerShellSession the powershell session which is not disposable. */ find(nodeName, key) { const ps = PowerShell.find(nodeName, key); if (ps) { return new PowerShellSession(ps); } return null; } /** * Cancel the script run. * @param session the PowerShell session object. */ cancel(session) { return session.powerShell.cancel(); } createBatchSession(nodeNamesList, key, requestOptions) { if (!nodeNamesList || nodeNamesList.length === 0) { throw new Error('CreateBatchSession needs a valid non-empty nodes list.'); } if (MsftSme.isNullOrWhiteSpace(key)) { return new PowerShellBatchSession(PowerShellBatch.create(nodeNamesList, this.batchConnection, null, null, requestOptions)); } const lifeTime = this.lifetimeData.manager.createChildLifetime(); return new PowerShellBatchSession(PowerShellBatch.create(nodeNamesList, this.batchConnection, key, lifeTime, requestOptions), lifeTime); } runBatch(session, scriptOrCommandList, options) { const commandList = []; for (const scriptOrCommand of scriptOrCommandList) { commandList.push(PowerShell.getPowerShellCommand(scriptOrCommand)); } return session.powerShellBatch.run(commandList, options); } runBatchSingleCommand(session, scriptOrCommand, options) { return session.powerShellBatch.runSingleCommand(PowerShell.getPowerShellCommand(scriptOrCommand), options); } /** * Find existing PowerShellBatchSession by the node names list and key. * * @param nodeNames the node names list. * @param key the key string. * @return PowerShellBatchSession the powershell batch session which is not disposable. */ findBatchSession(nodeNamesList, key) { const ps = PowerShellBatch.find(nodeNamesList, key); if (ps) { return new PowerShellBatchSession(ps); } return null; } } //# sourceMappingURL=powershell-connection.js.map