UNPKG

@microsoft/windows-admin-center-sdk

Version:

Microsoft - Windows Admin Center Shell

1 lines 10.6 kB
{"version":3,"sources":["../../../packages/tools/gulp-ps-cim/index.ts"],"names":[],"mappings":"","file":"index.d.ts","sourcesContent":["'use strict';\r\n\r\nimport log from 'fancy-log';\r\nimport pluginError from 'plugin-error';\r\nimport through2 from 'through2';\r\nimport Vinyl from 'vinyl';\r\nimport * as util from '../utilities';\r\n\r\nconst PLUGIN_NAME = 'gulp-ps-cim';\r\n\r\ninterface MapData {\r\n name: string;\r\n description: string;\r\n scope: string;\r\n roles: string[];\r\n type: string;\r\n namespace: string;\r\n className: string;\r\n methodName: string;\r\n query: string;\r\n keyProperties: [\r\n {\r\n name: string;\r\n type: string;\r\n }];\r\n property: [\r\n {\r\n name: string;\r\n type: string;\r\n value: any;\r\n }];\r\n arguments: [\r\n {\r\n name: string;\r\n type: string;\r\n value: any;\r\n }];\r\n}\r\n\r\nfunction gulpPowerShellCim(_) {\r\n let collection: MapData[] = null;\r\n\r\n return through2.obj(\r\n /**\r\n * Transform\r\n */\r\n function (file, encoding, callback) {\r\n collection = JSON.parse(file.contents.toString()).collection;\r\n return callback();\r\n },\r\n /**\r\n * Flush\r\n */\r\n function (callback) {\r\n try {\r\n for (const mapData of collection) {\r\n const contents = cimScript(mapData).join('\\r\\n');\r\n const scopePath = mapData.scope ? mapData.scope + '/' : '';\r\n const scriptFile = new Vinyl({\r\n cwd: './',\r\n path: scopePath + mapData.name + '.ps1',\r\n contents: Buffer.from(contents, 'utf8')\r\n });\r\n this.push(scriptFile);\r\n }\r\n } catch (e) {\r\n const error = (!e.plugin || (e.plugin !== PLUGIN_NAME)) ?\r\n util.extendError(new pluginError({ plugin: PLUGIN_NAME, message: e.message }), e) : e;\r\n log.error(error);\r\n }\r\n\r\n callback();\r\n });\r\n}\r\n\r\nfunction cimScript(mapData: MapData): string[] {\r\n let line: string;\r\n const bodySection: string[] = [];\r\n const commentSection: string[] = [];\r\n let paramSection: string[] = [];\r\n let parameters;\r\n commentSection.push(\r\n '<#',\r\n '',\r\n '.SYNOPSIS',\r\n mapData.description,\r\n '',\r\n '.DESCRIPTION',\r\n mapData.description,\r\n '',\r\n '.ROLE');\r\n commentSection.push(...mapData.roles),\r\n commentSection.push(\r\n '',\r\n '#>',\r\n '',\r\n '##SkipCheck=true##');\r\n\r\n switch (mapData.type) {\r\n case 'getInstanceMultiple':\r\n case 'getBatchInstanceMultiple':\r\n // namespace the cim namespace.\r\n // className the class name.\r\n line = `Get-CimInstance -Namespace ${mapData.namespace} -ClassName ${mapData.className}`;\r\n bodySection.push(line);\r\n break;\r\n\r\n case 'getInstanceSingle':\r\n case 'getBatchInstanceSingle':\r\n // namespace the cim namespace.\r\n // lassName the class name.\r\n // keyProperties the key properties object\r\n parameters = keyArgumentsParameters(mapData);\r\n paramSection = parameters.paramSection;\r\n line = `$keyInstance = New-CimInstance -Namespace ${mapData.namespace} -ClassName ${mapData.className}`;\r\n line += ` -Key @(${parameters.keyLine}) -Property @{${parameters.propertyLine}} -ClientOnly`;\r\n bodySection.push(line);\r\n line = 'Get-CimInstance $keyInstance';\r\n bodySection.push(line);\r\n break;\r\n\r\n case 'invokeMethodInstance':\r\n case 'invokeBatchMethodInstance':\r\n // namespace the cim namespace.\r\n // className the class name.\r\n // methodName the method name.\r\n // keyProperties the key properties object.\r\n // data the method input data.\r\n parameters = keyArgumentsParameters(mapData);\r\n paramSection = parameters.paramSection;\r\n line = `$keyInstance = New-CimInstance -Namespace ${mapData.namespace} -ClassName ${mapData.className}`;\r\n line += ` -Key @(${parameters.keyLine}) -Property @{${parameters.propertyLine}} -ClientOnly`;\r\n bodySection.push(line);\r\n line = `Invoke-CimMethod $keyInstance -MethodName ${mapData.methodName}`;\r\n if (parameters.argumentsLine) {\r\n line += ` -Arguments @{${parameters.argumentsLine}}`;\r\n }\r\n\r\n bodySection.push(line);\r\n break;\r\n\r\n case 'invokeMethodStatic':\r\n case 'invokeBatchMethodStatic':\r\n // namespace the cim namespace.\r\n // className the class name.\r\n // methodName the method name.\r\n // data the method input data.\r\n parameters = keyArgumentsParameters(mapData);\r\n paramSection = parameters.paramSection;\r\n line = `Invoke-CimMethod -Namespace ${mapData.namespace} -ClassName ${mapData.className}`;\r\n line += ` -MethodName ${mapData.methodName}`;\r\n if (parameters.argumentsLine) {\r\n line += ` -Arguments @{${parameters.argumentsLine}}`;\r\n }\r\n\r\n bodySection.push(line);\r\n break;\r\n\r\n case 'setInstance':\r\n case 'setBatchInstance':\r\n // namespace the cim namespace.\r\n // lassName the class name.\r\n // keyProperties the key properties object.\r\n // data the method input data.\r\n parameters = keyArgumentsParameters(mapData);\r\n paramSection = parameters.paramSection;\r\n line = `New-CimInstance -Namespace ${mapData.namespace} -ClassName ${mapData.className}`;\r\n line += ` -Key @(${parameters.keyLine}) -Property @{${parameters.propertyLine}${parameters.argumentsLine}}`;\r\n bodySection.push(line);\r\n break;\r\n\r\n case 'modifyInstance':\r\n case 'modifyBatchInstance':\r\n // namespace the cim namespace.\r\n // className the class name.\r\n // keyProperties the key properties object.\r\n // data the method input data.\r\n parameters = keyArgumentsParameters(mapData);\r\n paramSection = parameters.paramSection;\r\n line = `$instance = New-CimInstance -Namespace ${mapData.namespace} -ClassName ${mapData.className}`;\r\n line += ` -Key @(${parameters.keyLine}) -Property @{${parameters.propertyLine}} -ClientOnly`;\r\n bodySection.push(line);\r\n line = '$instance = Get-CimInstance $instance';\r\n bodySection.push(line);\r\n bodySection.push(...parameters.updateSection);\r\n line = 'Set-CimInstance $instance';\r\n bodySection.push(line);\r\n break;\r\n\r\n case 'deleteInstance':\r\n case 'deleteBatchInstance':\r\n // namespace the cim namespace.\r\n // className the class name.\r\n // keyProperties the key properties object.\r\n parameters = keyArgumentsParameters(mapData);\r\n paramSection = parameters.paramSection;\r\n line = `$instance = New-CimInstance -Namespace ${mapData.namespace} -ClassName ${mapData.className}`;\r\n line += ` -Key @(${parameters.keyLine}) -Property @{${parameters.propertyLine}} -ClientOnly`;\r\n bodySection.push(line);\r\n line = 'Remove-CimInstance $instance';\r\n bodySection.push(line);\r\n break;\r\n\r\n case 'getInstanceQuery':\r\n case 'getBatchInstanceQuery':\r\n // namespace the cim namespace.\r\n // query the WQL string.\r\n parameters = keyArgumentsParameters(mapData);\r\n paramSection = parameters.paramSection;\r\n line = `Get-CimInstance -Namespace ${mapData.namespace} -Query \"${mapData.query}\"`;\r\n bodySection.push(line);\r\n break;\r\n }\r\n\r\n const content: string[] = [];\r\n content.push(...commentSection);\r\n content.push('');\r\n content.push(...paramSection);\r\n content.push('');\r\n content.push('import-module CimCmdlets');\r\n content.push('');\r\n content.push(...bodySection);\r\n content.push('');\r\n return content;\r\n}\r\n\r\nfunction keyArgumentsParameters(mapData: MapData): {\r\n keyLine: string,\r\n propertyLine: string,\r\n argumentsLine: string,\r\n updateSection: string[],\r\n paramSection: string[]\r\n} {\r\n const paramSection: string[] = [];\r\n paramSection.push('Param(');\r\n let keyLine: string = null;\r\n let keySeparator = '';\r\n let propLine: string = null;\r\n let dataLine: string = null;\r\n if (mapData.keyProperties && Array.isArray(mapData.keyProperties)) {\r\n keyLine = '';\r\n propLine = '';\r\n for (const key of mapData.keyProperties) {\r\n paramSection.push(`[${key.type}]$${key.name}`);\r\n keyLine += `${keySeparator}'${key.name}'`;\r\n propLine += `${key.name}=$${key.name};`;\r\n keySeparator = ',';\r\n }\r\n }\r\n\r\n const updateSection: string[] = [];\r\n updateSection.push('');\r\n if (mapData.arguments) {\r\n dataLine = '';\r\n for (const data of mapData.arguments) {\r\n paramSection.push(`[${data.type}]$${data.name}`);\r\n dataLine += `${data.name}=$${data.name};`;\r\n updateSection.push(`$instance.${data.name}=$${data.name}`);\r\n }\r\n }\r\n\r\n if (paramSection.length > 2) {\r\n for (let i = 1; i < paramSection.length - 1; i++) {\r\n paramSection[i] += ',';\r\n }\r\n }\r\n\r\n paramSection.push(')');\r\n return {\r\n keyLine: keyLine,\r\n propertyLine: propLine,\r\n argumentsLine: dataLine,\r\n updateSection: updateSection,\r\n paramSection: paramSection\r\n };\r\n}\r\n\r\nmodule.exports = gulpPowerShellCim;\r\n"]}