@microsoft/windows-admin-center-sdk
Version:
Microsoft - Windows Admin Center Shell
214 lines (212 loc) • 8.92 kB
JavaScript
;
Object.defineProperty(exports, "__esModule", { value: true });
const tslib_1 = require("tslib");
const fancy_log_1 = tslib_1.__importDefault(require("fancy-log"));
const plugin_error_1 = tslib_1.__importDefault(require("plugin-error"));
const through2_1 = tslib_1.__importDefault(require("through2"));
const vinyl_1 = tslib_1.__importDefault(require("vinyl"));
const util = tslib_1.__importStar(require("../utilities"));
const PLUGIN_NAME = 'gulp-ps-cim';
function gulpPowerShellCim(_) {
let collection = null;
return through2_1.default.obj(
/**
* Transform
*/
function (file, encoding, callback) {
collection = JSON.parse(file.contents.toString()).collection;
return callback();
},
/**
* Flush
*/
function (callback) {
try {
for (const mapData of collection) {
const contents = cimScript(mapData).join('\r\n');
const scopePath = mapData.scope ? mapData.scope + '/' : '';
const scriptFile = new vinyl_1.default({
cwd: './',
path: scopePath + mapData.name + '.ps1',
contents: Buffer.from(contents, 'utf8')
});
this.push(scriptFile);
}
}
catch (e) {
const error = (!e.plugin || (e.plugin !== PLUGIN_NAME)) ?
util.extendError(new plugin_error_1.default({ plugin: PLUGIN_NAME, message: e.message }), e) : e;
fancy_log_1.default.error(error);
}
callback();
});
}
function cimScript(mapData) {
let line;
const bodySection = [];
const commentSection = [];
let paramSection = [];
let parameters;
commentSection.push('<#', '', '.SYNOPSIS', mapData.description, '', '.DESCRIPTION', mapData.description, '', '.ROLE');
commentSection.push(...mapData.roles),
commentSection.push('', '#>', '', '##SkipCheck=true##');
switch (mapData.type) {
case 'getInstanceMultiple':
case 'getBatchInstanceMultiple':
// namespace the cim namespace.
// className the class name.
line = `Get-CimInstance -Namespace ${mapData.namespace} -ClassName ${mapData.className}`;
bodySection.push(line);
break;
case 'getInstanceSingle':
case 'getBatchInstanceSingle':
// namespace the cim namespace.
// lassName the class name.
// keyProperties the key properties object
parameters = keyArgumentsParameters(mapData);
paramSection = parameters.paramSection;
line = `$keyInstance = New-CimInstance -Namespace ${mapData.namespace} -ClassName ${mapData.className}`;
line += ` -Key @(${parameters.keyLine}) -Property @{${parameters.propertyLine}} -ClientOnly`;
bodySection.push(line);
line = 'Get-CimInstance $keyInstance';
bodySection.push(line);
break;
case 'invokeMethodInstance':
case 'invokeBatchMethodInstance':
// namespace the cim namespace.
// className the class name.
// methodName the method name.
// keyProperties the key properties object.
// data the method input data.
parameters = keyArgumentsParameters(mapData);
paramSection = parameters.paramSection;
line = `$keyInstance = New-CimInstance -Namespace ${mapData.namespace} -ClassName ${mapData.className}`;
line += ` -Key @(${parameters.keyLine}) -Property @{${parameters.propertyLine}} -ClientOnly`;
bodySection.push(line);
line = `Invoke-CimMethod $keyInstance -MethodName ${mapData.methodName}`;
if (parameters.argumentsLine) {
line += ` -Arguments @{${parameters.argumentsLine}}`;
}
bodySection.push(line);
break;
case 'invokeMethodStatic':
case 'invokeBatchMethodStatic':
// namespace the cim namespace.
// className the class name.
// methodName the method name.
// data the method input data.
parameters = keyArgumentsParameters(mapData);
paramSection = parameters.paramSection;
line = `Invoke-CimMethod -Namespace ${mapData.namespace} -ClassName ${mapData.className}`;
line += ` -MethodName ${mapData.methodName}`;
if (parameters.argumentsLine) {
line += ` -Arguments @{${parameters.argumentsLine}}`;
}
bodySection.push(line);
break;
case 'setInstance':
case 'setBatchInstance':
// namespace the cim namespace.
// lassName the class name.
// keyProperties the key properties object.
// data the method input data.
parameters = keyArgumentsParameters(mapData);
paramSection = parameters.paramSection;
line = `New-CimInstance -Namespace ${mapData.namespace} -ClassName ${mapData.className}`;
line += ` -Key @(${parameters.keyLine}) -Property @{${parameters.propertyLine}${parameters.argumentsLine}}`;
bodySection.push(line);
break;
case 'modifyInstance':
case 'modifyBatchInstance':
// namespace the cim namespace.
// className the class name.
// keyProperties the key properties object.
// data the method input data.
parameters = keyArgumentsParameters(mapData);
paramSection = parameters.paramSection;
line = `$instance = New-CimInstance -Namespace ${mapData.namespace} -ClassName ${mapData.className}`;
line += ` -Key @(${parameters.keyLine}) -Property @{${parameters.propertyLine}} -ClientOnly`;
bodySection.push(line);
line = '$instance = Get-CimInstance $instance';
bodySection.push(line);
bodySection.push(...parameters.updateSection);
line = 'Set-CimInstance $instance';
bodySection.push(line);
break;
case 'deleteInstance':
case 'deleteBatchInstance':
// namespace the cim namespace.
// className the class name.
// keyProperties the key properties object.
parameters = keyArgumentsParameters(mapData);
paramSection = parameters.paramSection;
line = `$instance = New-CimInstance -Namespace ${mapData.namespace} -ClassName ${mapData.className}`;
line += ` -Key @(${parameters.keyLine}) -Property @{${parameters.propertyLine}} -ClientOnly`;
bodySection.push(line);
line = 'Remove-CimInstance $instance';
bodySection.push(line);
break;
case 'getInstanceQuery':
case 'getBatchInstanceQuery':
// namespace the cim namespace.
// query the WQL string.
parameters = keyArgumentsParameters(mapData);
paramSection = parameters.paramSection;
line = `Get-CimInstance -Namespace ${mapData.namespace} -Query "${mapData.query}"`;
bodySection.push(line);
break;
}
const content = [];
content.push(...commentSection);
content.push('');
content.push(...paramSection);
content.push('');
content.push('import-module CimCmdlets');
content.push('');
content.push(...bodySection);
content.push('');
return content;
}
function keyArgumentsParameters(mapData) {
const paramSection = [];
paramSection.push('Param(');
let keyLine = null;
let keySeparator = '';
let propLine = null;
let dataLine = null;
if (mapData.keyProperties && Array.isArray(mapData.keyProperties)) {
keyLine = '';
propLine = '';
for (const key of mapData.keyProperties) {
paramSection.push(`[${key.type}]$${key.name}`);
keyLine += `${keySeparator}'${key.name}'`;
propLine += `${key.name}=$${key.name};`;
keySeparator = ',';
}
}
const updateSection = [];
updateSection.push('');
if (mapData.arguments) {
dataLine = '';
for (const data of mapData.arguments) {
paramSection.push(`[${data.type}]$${data.name}`);
dataLine += `${data.name}=$${data.name};`;
updateSection.push(`$instance.${data.name}=$${data.name}`);
}
}
if (paramSection.length > 2) {
for (let i = 1; i < paramSection.length - 1; i++) {
paramSection[i] += ',';
}
}
paramSection.push(')');
return {
keyLine: keyLine,
propertyLine: propLine,
argumentsLine: dataLine,
updateSection: updateSection,
paramSection: paramSection
};
}
module.exports = gulpPowerShellCim;
//# sourceMappingURL=index.js.map