├── .editorconfig ├── .eslintrc.js ├── .eslintrc.prepublish.js ├── .gitignore ├── .prettierrc.js ├── LICENSE ├── README.md ├── gulpfile.js ├── index.js ├── nodes └── PowerShell │ ├── PowerShell.node.json │ ├── PowerShell.node.ts │ └── powershell.svg ├── package-lock.json ├── package.json ├── tsconfig.json └── tslint.json /.editorconfig: -------------------------------------------------------------------------------- 1 | root = true 2 | 3 | [*] 4 | charset = utf-8 5 | indent_style = tab 6 | indent_size = 2 7 | end_of_line = lf 8 | insert_final_newline = true 9 | trim_trailing_whitespace = true 10 | 11 | [package.json] 12 | indent_style = space 13 | indent_size = 2 14 | 15 | [*.yml] 16 | indent_style = space 17 | indent_size = 2 -------------------------------------------------------------------------------- /.eslintrc.js: -------------------------------------------------------------------------------- 1 | module.exports = { 2 | root: true, 3 | 4 | env: { 5 | browser: true, 6 | es6: true, 7 | node: true, 8 | }, 9 | 10 | parser: '@typescript-eslint/parser', 11 | parserOptions: { 12 | project: ['./tsconfig.json'], 13 | sourceType: 'module', 14 | extraFileExtensions: ['.json'], 15 | }, 16 | ignorePatterns: [ 17 | '.eslintrc.js', 18 | '**/*.js', 19 | '**/node_modules/**', 20 | '**/dist/**', 21 | ], 22 | 23 | overrides: [ 24 | { 25 | files: ['package.json'], 26 | plugins: ['eslint-plugin-n8n-nodes-base'], 27 | extends: ['plugin:n8n-nodes-base/community'], 28 | rules: { 29 | 'n8n-nodes-base/community-package-json-name-still-default': 'off', 30 | } 31 | }, 32 | { 33 | files: ['./credentials/**/*.ts'], 34 | plugins: ['eslint-plugin-n8n-nodes-base'], 35 | extends: ['plugin:n8n-nodes-base/credentials'], 36 | rules: { 37 | 'n8n-nodes-base/cred-class-field-documentation-url-missing': 'off', 38 | 'n8n-nodes-base/cred-class-field-documentation-url-miscased': 'off', 39 | }, 40 | }, 41 | { 42 | files: ['./nodes/**/*.ts'], 43 | plugins: ['eslint-plugin-n8n-nodes-base'], 44 | extends: ['plugin:n8n-nodes-base/nodes'], 45 | rules: { 46 | 'n8n-nodes-base/node-execute-block-missing-continue-on-fail': 'off', 47 | 'n8n-nodes-base/node-resource-description-filename-against-convention': 'off', 48 | 'n8n-nodes-base/node-param-fixed-collection-type-unsorted-items': 'off', 49 | 'n8n-nodes-base/node-execute-block-operation-missing-singular-pairing': 'off', 50 | 'n8n-nodes-base/node-execute-block-operation-missing-plural-pairing': 'off', 51 | }, 52 | }, 53 | ], 54 | }; -------------------------------------------------------------------------------- /.eslintrc.prepublish.js: -------------------------------------------------------------------------------- 1 | module.exports = { 2 | extends: "./.eslintrc.js", 3 | overrides: [ 4 | { 5 | files: ['package.json'], 6 | plugins: ['eslint-plugin-n8n-nodes-base'], 7 | rules: { 8 | 'n8n-nodes-base/community-package-json-name-still-default': 'error', 9 | }, 10 | }, 11 | ], 12 | }; 13 | -------------------------------------------------------------------------------- /.gitignore: -------------------------------------------------------------------------------- 1 | node_modules 2 | dist -------------------------------------------------------------------------------- /.prettierrc.js: -------------------------------------------------------------------------------- 1 | module.exports = { 2 | /** 3 | * https://prettier.io/docs/en/options.html#semicolons 4 | */ 5 | semi: true, 6 | 7 | /** 8 | * https://prettier.io/docs/en/options.html#trailing-commas 9 | */ 10 | trailingComma: 'all', 11 | 12 | /** 13 | * https://prettier.io/docs/en/options.html#bracket-spacing 14 | */ 15 | bracketSpacing: true, 16 | 17 | /** 18 | * https://prettier.io/docs/en/options.html#tabs 19 | */ 20 | useTabs: true, 21 | 22 | /** 23 | * https://prettier.io/docs/en/options.html#tab-width 24 | */ 25 | tabWidth: 2, 26 | 27 | /** 28 | * https://prettier.io/docs/en/options.html#arrow-function-parentheses 29 | */ 30 | arrowParens: 'always', 31 | 32 | /** 33 | * https://prettier.io/docs/en/options.html#quotes 34 | */ 35 | singleQuote: true, 36 | 37 | /** 38 | * https://prettier.io/docs/en/options.html#quote-props 39 | */ 40 | quoteProps: 'as-needed', 41 | 42 | /** 43 | * https://prettier.io/docs/en/options.html#end-of-line 44 | */ 45 | endOfLine: 'lf', 46 | 47 | /** 48 | * https://prettier.io/docs/en/options.html#print-width 49 | */ 50 | printWidth: 100, 51 | }; -------------------------------------------------------------------------------- /LICENSE: -------------------------------------------------------------------------------- 1 | MIT License 2 | 3 | Copyright (c) 2022 Deborah 4 | 5 | Permission is hereby granted, free of charge, to any person obtaining a copy 6 | of this software and associated documentation files (the "Software"), to deal 7 | in the Software without restriction, including without limitation the rights 8 | to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 9 | copies of the Software, and to permit persons to whom the Software is 10 | furnished to do so, subject to the following conditions: 11 | 12 | The above copyright notice and this permission notice shall be included in all 13 | copies or substantial portions of the Software. 14 | 15 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 16 | IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 17 | FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 18 | AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 19 | LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 20 | OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE 21 | SOFTWARE. 22 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # n8n-nodes-powershell 2 | 3 | This is a custom node for [n8n](https://n8n.io/). It allows you to execute PowerShell commands within an n8n workflow. 4 | 5 | ## Purpose 6 | 7 | n8n provides an Execute Command node, which allows you to execute scripts on your system's default shell. On Windows, this is Command Prompt (cmd). There is currently no option to use PowerShell, other than changing your system's default shell. This node provides an alternative. It copies _heavily_ from the Execute Command node (source for that node is [here](https://github.com/n8n-io/n8n/tree/master/packages/nodes-base/nodes/ExecuteCommand) if you're curious just how blatantly I've copied). 8 | 9 | ## Installation 10 | 11 | 1. Make sure you have PowerShell installed on the same machine as n8n. The node gives you the option to use either your default PowerShell installation, or your default PowerShell Core installation. You can't choose between multiple PowerShell (or PowerShell Core) versions. 12 | 2. Follow the n8n documentation to [install community nodes](https://docs.n8n.io/integrations/community-nodes/installation/). 13 | 14 | ## Limitations 15 | 16 | ### Not available on Cloud or Desktop 17 | 18 | Like the Execute Command node, the PowerShell node won't work on n8n Cloud. 19 | 20 | Community nodes are currently unavailable on n8n Desktop. 21 | 22 | ### Requires PowerShell 23 | 24 | You must have PowerShell installed on the same machine as n8n. 25 | 26 | ### Limited PowerShell selection 27 | 28 | You can choose either PowerShell or PowerShell Core to run your script. The node uses the default installation. This means if you have PowerShell 3.x, 4.x, and 5.x installed, it will automatically use the default. You can't use the node to select an alternative installation. This shouldn't be a problem with PowerShell, as the latest version has good backwards compatibility, but you may need to be aware of differences between the main PowerShell Core versions. 29 | 30 | ## Technical background 31 | 32 | This node uses `execPromise`, an n8n function that promisifies Node.js' `child_process.exec()`. Refer to the [Node.js 16.x documentation](https://nodejs.org/docs/latest-v16.x/api/child_process.html#child_processexeccommand-options-callback) for more background. 33 | 34 | > **Note:** because we're using `exec` rather than `spawn`, there is no option to detach the child process. This means the n8n workflow waits for the PowerShell script to complete before proceeding. 35 | 36 | > **Note:** `exec` is designed to handle commands with small outputs. Be cautious about using this node for memory-heavy operations. 37 | 38 | 39 | -------------------------------------------------------------------------------- /gulpfile.js: -------------------------------------------------------------------------------- 1 | const path = require('path'); 2 | const { task, src, dest } = require('gulp'); 3 | 4 | task('build:icons', copyIcons); 5 | 6 | function copyIcons() { 7 | const nodeSource = path.resolve('nodes', '**', '*.{png,svg}'); 8 | const nodeDestination = path.resolve('dist', 'nodes'); 9 | 10 | src(nodeSource).pipe(dest(nodeDestination)); 11 | 12 | const credSource = path.resolve('credentials', '**', '*.{png,svg}'); 13 | const credDestination = path.resolve('dist', 'credentials'); 14 | 15 | return src(credSource).pipe(dest(credDestination)); 16 | } -------------------------------------------------------------------------------- /index.js: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/StarfallProjects/n8n-nodes-powershell/618e726f5b0c61b4339e59b58f040402c2b2138e/index.js -------------------------------------------------------------------------------- /nodes/PowerShell/PowerShell.node.json: -------------------------------------------------------------------------------- 1 | { 2 | "node": "n8n-nodes-base.PowerShell", 3 | "nodeVersion": "1.0", 4 | "codexVersion": "1.0", 5 | "categories": [ 6 | "Development" 7 | ], 8 | "resources": { 9 | "primaryDocumentation": [ 10 | { 11 | "url": "https://github.com/StarfallProjects/n8n-nodes-powershell" 12 | } 13 | ] 14 | } 15 | } 16 | -------------------------------------------------------------------------------- /nodes/PowerShell/PowerShell.node.ts: -------------------------------------------------------------------------------- 1 | import { IExecuteFunctions } from 'n8n-core'; 2 | import { 3 | INodeExecutionData, 4 | INodeType, 5 | INodeTypeDescription, 6 | NodeOperationError, 7 | } from 'n8n-workflow'; 8 | 9 | import { exec } from 'child_process'; 10 | 11 | export interface IExecReturnData { 12 | exitCode: number; 13 | error?: Error; 14 | stderr: string; 15 | stdout: string; 16 | } 17 | 18 | /** 19 | * Promisifiy exec manually to also get the exit code 20 | * 21 | * @param {string} command 22 | * @param {string} shellChoice 23 | * @returns {Promise} 24 | */ 25 | function execPromise(command: string, shellChoice: string): Promise { 26 | const returnData: IExecReturnData = { 27 | error: undefined, 28 | exitCode: 0, 29 | stderr: '', 30 | stdout: '', 31 | }; 32 | 33 | return new Promise((resolve, reject) => { 34 | exec(command, { 'shell': shellChoice }, (error, stdout, stderr) => { 35 | returnData.stdout = stdout.trim(); 36 | returnData.stderr = stderr.trim(); 37 | 38 | if (error) { 39 | returnData.error = error; 40 | } 41 | 42 | resolve(returnData); 43 | }).on('exit', (code) => { 44 | returnData.exitCode = code || 0; 45 | }); 46 | }); 47 | } 48 | 49 | export class PowerShell implements INodeType { 50 | description: INodeTypeDescription = { 51 | displayName: 'PowerShell', 52 | name: 'PowerShell', 53 | icon: 'file:powershell.svg', 54 | group: [], 55 | version: 1, 56 | subtitle: '', 57 | description: 'Run PowerShell commands from n8n', 58 | defaults: { 59 | name: 'PowerShell', 60 | }, 61 | inputs: ['main'], 62 | outputs: ['main'], 63 | properties: [ 64 | { 65 | displayName: 'Choose PowerShell Type', 66 | name: 'shellChoice', 67 | description: 'Choose PowerShell to use your default full PowerShell installation (probably PowerShell 5), or PowerShell Core to use your default PowerShell Core (PowerShell 6 or 7) installation', 68 | type: 'options', 69 | default: 'powershell.exe', 70 | options: [ 71 | { 72 | name: 'PowerShell', 73 | value: 'powershell.exe', 74 | }, 75 | { 76 | name: 'PowerShell Core', 77 | value: 'pwsh', 78 | }, 79 | ], 80 | }, 81 | { 82 | displayName: 'Command', 83 | name: 'command', 84 | type: 'string', 85 | default: '', 86 | typeOptions: { 87 | rows: 10, 88 | }, 89 | placeholder: 'Write-Output "Hello World"', 90 | description: 'Write a command to execute', 91 | }, 92 | { 93 | displayName: 'Execute Once', 94 | name: 'executeOnce', 95 | type: 'boolean', 96 | default: true, 97 | description: 'Whether to execute only once (enabled) instead of once for each entry (disabled)', 98 | }, 99 | ], 100 | }; 101 | 102 | 103 | 104 | 105 | async execute(this: IExecuteFunctions): Promise { 106 | let items = this.getInputData(); 107 | let command: string; 108 | let shellChoice: string; 109 | const executeOnce = this.getNodeParameter('executeOnce', 0) as boolean; 110 | 111 | if (executeOnce === true) { 112 | items = [items[0]]; 113 | } 114 | const returnItems: INodeExecutionData[] = []; 115 | for (let itemIndex = 0; itemIndex < items.length; itemIndex++) { 116 | try { 117 | command = this.getNodeParameter('command', itemIndex) as string; 118 | shellChoice = this.getNodeParameter('shellChoice', itemIndex) as string; 119 | 120 | const { error, exitCode, stdout, stderr } = await execPromise(command, shellChoice); 121 | 122 | if (error !== undefined) { 123 | throw new NodeOperationError(this.getNode(), error.message, { itemIndex }); 124 | } 125 | 126 | returnItems.push({ 127 | json: { 128 | exitCode, 129 | stderr, 130 | stdout, 131 | }, 132 | pairedItem: { 133 | item: itemIndex, 134 | }, 135 | }); 136 | } catch (error) { 137 | if (this.continueOnFail()) { 138 | returnItems.push({ 139 | json: { 140 | error: error.message, 141 | }, 142 | pairedItem: { 143 | item: itemIndex, 144 | }, 145 | }); 146 | continue; 147 | } 148 | throw error; 149 | } 150 | } 151 | 152 | return this.prepareOutputData(returnItems); 153 | } 154 | 155 | } 156 | -------------------------------------------------------------------------------- /nodes/PowerShell/powershell.svg: -------------------------------------------------------------------------------- 1 | 2 | 3 | 6 | 7 | 11 | 15 | 19 | 24 | 28 | 29 | -------------------------------------------------------------------------------- /package.json: -------------------------------------------------------------------------------- 1 | { 2 | "name": "@starfallprojects/n8n-nodes-powershell", 3 | "version": "1.0.0", 4 | "description": "Run PowerShell scripts within an n8n workflow.", 5 | "keywords": [ 6 | "n8n-community-node-package", "powershell" 7 | ], 8 | "license": "MIT", 9 | "homepage": "https://github.com/StarfallProjects/n8n-nodes-powershell", 10 | "author": { 11 | "name": "Deborah Barnard", 12 | "website": "https://starfallprojects.co.uk/" 13 | }, 14 | "repository": { 15 | "type": "git", 16 | "url": "https://github.com/StarfallProjects/n8n-nodes-powershell.git" 17 | }, 18 | "main": "index.js", 19 | "scripts": { 20 | "build": "tsc && gulp build:icons", 21 | "dev": "tsc --watch", 22 | "format": "prettier nodes --write", 23 | "lint": "tslint -p tsconfig.json -c tslint.json && eslint nodes package.json", 24 | "lintfix": "tslint --fix -p tsconfig.json -c tslint.json && eslint nodes package.json --fix", 25 | "prepublishOnly": "npm run build && npm run lint -c .eslintrc.prepublish.js nodes package.json" 26 | }, 27 | "files": [ 28 | "dist" 29 | ], 30 | "n8n": { 31 | "n8nNodesApiVersion": 1, 32 | "nodes": [ 33 | "dist/nodes/PowerShell/PowerShell.node.js" 34 | ] 35 | }, 36 | "devDependencies": { 37 | "@types/express": "^4.17.6", 38 | "@types/request-promise-native": "~1.0.15", 39 | "@typescript-eslint/parser": "^5.29.0", 40 | "eslint-plugin-n8n-nodes-base": "^1.5.4", 41 | "gulp": "^4.0.2", 42 | "n8n-core": "^0.125.0", 43 | "n8n-workflow": "^0.107.0", 44 | "prettier": "^2.7.1", 45 | "tslint": "^6.1.2", 46 | "typescript": "~4.6.0" 47 | } 48 | } 49 | -------------------------------------------------------------------------------- /tsconfig.json: -------------------------------------------------------------------------------- 1 | { 2 | "compilerOptions": { 3 | "lib": [ 4 | "es2017", 5 | "es2019" 6 | ], 7 | "types": [ 8 | "node", 9 | ], 10 | "module": "commonjs", 11 | "noImplicitAny": true, 12 | "removeComments": true, 13 | "strictNullChecks": true, 14 | "strict": true, 15 | "preserveConstEnums": true, 16 | "resolveJsonModule": true, 17 | "declaration": true, 18 | "outDir": "./dist/", 19 | "target": "es2019", 20 | "sourceMap": true, 21 | "esModuleInterop": true, 22 | "useUnknownInCatchVariables": false, 23 | }, 24 | "include": [ 25 | "credentials/**/*", 26 | "nodes/**/*", 27 | "nodes/**/*.json", 28 | "package.json", 29 | ], 30 | } -------------------------------------------------------------------------------- /tslint.json: -------------------------------------------------------------------------------- 1 | { 2 | "linterOptions": { 3 | "exclude": [ 4 | "node_modules/**/*" 5 | ] 6 | }, 7 | "defaultSeverity": "error", 8 | "jsRules": {}, 9 | "rules": { 10 | "array-type": [ 11 | true, 12 | "array-simple" 13 | ], 14 | "arrow-return-shorthand": true, 15 | "ban": [ 16 | true, 17 | { 18 | "name": "Array", 19 | "message": "tsstyle#array-constructor" 20 | } 21 | ], 22 | "ban-types": [ 23 | true, 24 | [ 25 | "Object", 26 | "Use {} instead." 27 | ], 28 | [ 29 | "String", 30 | "Use 'string' instead." 31 | ], 32 | [ 33 | "Number", 34 | "Use 'number' instead." 35 | ], 36 | [ 37 | "Boolean", 38 | "Use 'boolean' instead." 39 | ] 40 | ], 41 | "class-name": true, 42 | "curly": [ 43 | true, 44 | "ignore-same-line" 45 | ], 46 | "forin": true, 47 | "jsdoc-format": true, 48 | "label-position": true, 49 | "indent": [ 50 | true, 51 | "tabs", 52 | 2 53 | ], 54 | "member-access": [ 55 | true, 56 | "no-public" 57 | ], 58 | "new-parens": true, 59 | "no-angle-bracket-type-assertion": true, 60 | "no-any": true, 61 | "no-arg": true, 62 | "no-conditional-assignment": true, 63 | "no-construct": true, 64 | "no-debugger": true, 65 | "no-default-export": true, 66 | "no-duplicate-variable": true, 67 | "no-inferrable-types": true, 68 | "ordered-imports": [ 69 | true, 70 | { 71 | "import-sources-order": "any", 72 | "named-imports-order": "case-insensitive" 73 | } 74 | ], 75 | "no-namespace": [ 76 | true, 77 | "allow-declarations" 78 | ], 79 | "no-reference": true, 80 | "no-string-throw": true, 81 | "no-unused-expression": true, 82 | "no-var-keyword": true, 83 | "object-literal-shorthand": true, 84 | "only-arrow-functions": [ 85 | true, 86 | "allow-declarations", 87 | "allow-named-functions" 88 | ], 89 | "prefer-const": true, 90 | "radix": true, 91 | "semicolon": [ 92 | true, 93 | "always", 94 | "ignore-bound-class-methods" 95 | ], 96 | "switch-default": true, 97 | "trailing-comma": [ 98 | true, 99 | { 100 | "multiline": { 101 | "objects": "always", 102 | "arrays": "always", 103 | "functions": "always", 104 | "typeLiterals": "ignore" 105 | }, 106 | "esSpecCompliant": true 107 | } 108 | ], 109 | "triple-equals": [ 110 | true, 111 | "allow-null-check" 112 | ], 113 | "use-isnan": true, 114 | "quotes": [ 115 | "error", 116 | "single" 117 | ], 118 | "variable-name": [ 119 | true, 120 | "check-format", 121 | "ban-keywords", 122 | "allow-leading-underscore", 123 | "allow-trailing-underscore" 124 | ] 125 | }, 126 | "rulesDirectory": [] 127 | } --------------------------------------------------------------------------------