Simple bindings for PowerShell in Node.js using class decorators.
Install @shadow578/powershell-binding
using npm:
npm install @shadow578/powershell-binding
Import the @shadow578/powershell-binding
module using the following code:
import { PowerShellBinding, PowerShellCall } from "@shadow578/powershell-binding";
To create a binding class, create a class extending PowerShellBinding
:
class MyBinding extends PowerShellBinding {
// ...
}
By default, the PowerShellBinding
class uses the DefaultShim
to convert input parameters and output objects to and from PowerShell.
However, custom shims may be used by overriding the shim
property of the PowerShellBinding
class:
class MyBinding extends PowerShellBinding {
get shim(): Shim {
return myShim;
}
}
Custom shims must implement the Shim
interface.
For more details on how to implement a custom shim, see the DefaultShim implementation.
To create a binding method, create a method in the binding class with the @PowerShellCall
decorator:
class MyBinding extends PowerShellBinding {
@PowerShellCall('Write-Output "Hello, $name!"', isString)
greet(name: string) {
return psCall<string>();
}
}
The @PowerShellCall
decorator takes the following parameters:
Name | Description | Default |
---|---|---|
command |
The command to execute in PowerShell | |
typeGuard |
A function that checks if the output is of the correct type | isAny |
options |
Additional options for the PowerShell call | {} |
The command
parameter takes a string containing the command to execute in PowerShell. The command may contain parameters, which are passed to the method as arguments.
The parameters are automatically converted to PowerShell variables and are available in the command.
The typeGuard
parameter takes a function with the following signature:
(input: unknown) => input is T
Such functions may be created using the @shadow578/type-guardian package.
The options
parameter takes the following properties:
Name | Description | Default |
---|---|---|
expandParameters |
Whether to expand the parameters to variables. If false, parameters are only available in the $params variable |
true |
serializationDepth |
How deep the output object should be serialized (See ConvertTo-Json docs). | 2 |
The psCall
function is used to stub the implementation of the binding method.
By itself, it does nothing but throw a Error, but it is replaced by the @PowerShellCall
decorator with the actual implementation.
To call a binding method, create an instance of the binding class and call the method:
const binding = new MyBinding();
const output = await binding.greet("World");
// output === "Hello, World!"
If an exception is thrown in PowerShell, it is re-thrown in JavaScript as a PowerShellError
.
By default PowerShellBinding
constructor creates a new PowerShell
instance using the default options.
Further options may be passed to the PowerShell
constructor using the options
parameter.
See The node-powershell
docs for possible options.
Additionally, a pre-existing PowerShell
instance may be passed to the PowerShellBinding
constructor using the instance
parameter.
In this case, the parameter options
is ignored.
For more examples, see DocumentationExamples.test.ts
and PowerShellCall.test.ts
.
- Input parameters to binding methods must be serializable using
JSON.stringify
and deserializable usingConvertFrom-Json
¹. - Outputs of binding methods must be serializable using
ConvertTo-Json
and deserializable usingJSON.parse
¹. - Outputs of binding methods may only have a depth of 100².
- Commands in binding methods cannot be interactive.
¹ generally, any value of type
string
,number
,boolean
,object
, or a array of those types, should work.
² depends on used shim. For the default shim, the depth is limited to 99.