Powershell has became a defacto standard for automation, and administration of .Net applications.
It is now an important requirement, when it comes to building .net enterprise applications. Providing a UI interface is not enough. Administrators and system integrators demand Powershell to automate configurations and deployments
The vast majority of the applications are remote in nature, which means they expose a web technology to allow remote access to the system. More often than not a Restful api is used to expose such an interface.
For simple api’s you can use Invoke-WebRequest and Invoke-RestMethod. These cmdlets allow the consumption of rest calls from Powershell. This option works for simple apis but for large api’s Powershell scripts became very complex to read a maintain.
This is where PSSwagger comes to the rescue. PSSwagger is a PowerShell module with commands to generate the PowerShell cmdlets for a RESTful Web Services using Swagger/OpenAPI documents.
If the rest api is build using an OpenAPI specification this tool will autogenerate cmdlets that represent the rest interface. Which means that you only need to develop the rest api. The Powershell interface is automatically generated for you. How cool is that !!!.
The following is the list of requirements need to setup PSSwagger.
- Install AutoRest and make it available in $env:PATH. AutoRest tool generates client libraries for accessing RESTful web services. It will be used by PSSwagger to generate a client library to be consumed by the generated cmdlets. Follow the steps outlined in the AutoRest github repository.
- Make sure .net compiler is available on the box.
- Install from PowerShellGallery.com and git clone the PSSwagger.
- Install-Module –Name PSSwagger
- git clone https://github.com/PowerShell/PSSwagger.git
- Execute the following script in the path where the PSSwagger is cloned.
[code]
# Import PSSwagger module
Import-Module PSSwagger
# If you are trying from a clone of this repository, follow below steps to import the PSSwagger module
# Ensure PSSwaggerUtility module is available in $env:PSModulePath
# Please note the trialing back slash (‘\’) to ensure PSSwaggerUtility module is available.</span>
$PSSwaggerFolderPath = Resolve-Path ‘.\PSSwagger\’
$env:PSModulePath="$PSSwaggerFolderPath;$env:PSModulePath"
Import-Module .\PSSwagger
# Ensure PSSwagger module is loaded into the current session
Get-Module PSSwagger
# Prepare input parameters for cmdlet generation
$null = New-Item -ItemType Directory -Path C:\GeneratedModules -Force
$params = @{
# Download the Open API v2 Specification from this location
SpecificationUri = ‘Url to open api spec of Restful api’
# Output the generated module to this path
Path = ‘C:\GeneratedModules\’
# Name of the generated module
Name=’ModuleName’
}
# You may be prompted to download missing dependencies
New-PSSwaggerModule @params
[/code]
The following three parameters have to be set.
- SpecificationUri. Defines the uri to the open specification of the Restful api.
- Path. Output path of generated assemblies
- ModuleName. Defines the name of the module
Hope this blog was helpful. If you need assistance or consultancy feel free to contact me on [email protected].