Autogenerate Powershell cmdlets using PSSwagger

Facebook
Twitter
LinkedIn

Never miss a post!

Sign up for our newsletter and get FREE Development Trends delivered directly to your inbox.

You can unsubscribe any time. Terms & Conditions.
Categories

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.

  1. 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.
  2. Make sure .net compiler is available on the box.
  3. Install from PowerShellGallery.com and git clone the PSSwagger.
    1. Install-Module Name PSSwagger
    2. git clone https://github.com/PowerShell/PSSwagger.git
  4. 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.

  1. SpecificationUri. Defines the uri to the open specification of the Restful api.
  2. Path. Output path of generated assemblies
  3. 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].

Facebook
Twitter
LinkedIn

Our website uses cookies that help it to function, allow us to analyze how you interact with it, and help us to improve its performance. By using our website you agree by our Terms and Conditions and Privacy Policy.