• This repository has been archived on 27/Sep/2019
  • Stars
    star
    164
  • Rank 230,032 (Top 5 %)
  • Language
    PowerShell
  • License
    Other
  • Created about 8 years ago
  • Updated over 5 years ago

Reviews

There are no reviews yet. Be the first to send feedback to the community and the maintainers!

Repository Details

The cmdlet generator from OpenAPI (f.k.a Swagger) specification

PSSwagger

A PowerShell module with commands to generate the PowerShell commands for a given RESTful Web Services using Swagger/OpenAPI documents.

PSSwagger is no longer being developed, but do not fret! AutoRest is now able to generate cmdlets directly, without needing PSSwagger. Please refer to: https://devblogs.microsoft.com/powershell/cmdlets-via-autorest/

This repository remains available for reference but pull requests and new issues will not be monitored.

PSSwagger Documentation

PSSwagger commands

Customizing PowerShell Metadata

Testing generated module

Supported Platforms

Usage Platforms
Developer Windows, PowerShell 5.1+, Latest PowerShell Core
Module Publisher PowerShell 5.1+
Module Consumer PowerShell 5.1+, Latest PowerShell Core

Future note: We plan on supporting PowerShell 4+ for module consumers in the future; that scenario is untested currently. Testing note: While any full PowerShell version is fine for development, we recommend using PowerShell 5.1+ to enable testing our implementation of Get-FileHash.

Dependencies

Dependency Version Description
AutoRest 0.17.3 or newer Tool to generate C# SDK from Swagger spec
CSC.exe (Microsoft.Net.Compilers) 2.3.1 or newer C# Compiler to generate C# SDK assembly on Windows PowerShell
Newtonsoft.Json Full CLR: 6.0.8, Core CLR: 9.0.1 NuGet package containing Newtonsoft.Json assembly, required for all modules
Microsoft.Rest.ClientRuntime 2.3.4 or newer NuGet package containing Microsoft.Rest.ClientRuntime assembly, required for all modules
Microsoft.Rest.ClientRuntime.Azure 3.3.4 or newer NuGet package containing Microsoft.Rest.ClientRuntime.Azure assembly, required for Microsoft Azure modules
AzureRM.Profile 2.0.0 or newer Module containing authentication helpers, required for Microsoft Azure modules on PowerShell
AzureRM.Profile.NetCore.Preview * Module containing authentication helpers, required for Microsoft Azure modules on PowerShell Core

Usage

NOTE: In the short term, for best performance, the operation IDs in your Open API specifications should be of the form "_". For example, the operation ID "Resource_GetByName" gets a resource named Resource by name.

  1. Get PSSwagger!

    • Install from PowerShellGallery.com
      Install-Module -Name PSSwagger
    • Clone the repository
      git clone https://github.com/PowerShell/PSSwagger.git
  2. Ensure AutoRest is installed and available in $env:PATH

    • Follow the instructions provided at AutoRest github repository.
    • Ensure AutoRest.cmd path is in $env:Path
      $env:path += ";$env:APPDATA\npm"
      Get-Command -Name AutoRest
  3. Ensure CSC.exe is installed and available in $env:PATH

    • If CSC.exe is not installed already, install Microsoft.Net.Compilers package
      Install-Package -Name Microsoft.Net.Compilers -Source https://www.nuget.org/api/v2 -Scope CurrentUser
    • Add CSC.exe path to $env:Path
      $package = Get-Package -Name Microsoft.Net.Compilers
      $env:path += ";$(Split-Path $package.Source -Parent)\tools"
      Get-Command -Name CSC.exe
  4. If you plan on pre-compiling the generated assembly, ensure you have the module AzureRM.Profile or AzureRM.NetCore.Preview available to PackageManagement if you are on PowerShell or PowerShell Core, respectively.

  5. Run the following in a PowerShell console from the directory where you cloned PSSwagger in

    # 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.
    $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 = 'https://raw.githubusercontent.com/Azure/azure-rest-api-specs/master/arm-batch/2015-12-01/swagger/BatchManagement.json'
      # Output the generated module to this path
      Path           = 'C:\GeneratedModules\'
      # Name of the generated module
      Name           = 'AzBatchManagement'
      # This specification is for a Microsoft Azure service, so use Azure-specific functionality
      UseAzureCsharpGenerator = $true
    }
    
    # You may be prompted to download missing dependencies
    New-PSSwaggerModule @params

    The generated module will be in the C:\Temp\GeneratedModule\Generated.AzureRM.BatchManagement folder. For more New-PSSwaggerModule options, check out the documentation.

  6. Your generated module is now ready! For production modules (i.e. modules you will publish to PowerShellGallery.com), we recommend using the -IncludeCoreFxAssembly option to generate the Core CLR assembly, strong name signing assemblies in the generated module, and authenticode signing the module. Optionally, you can remove the generated C# code (under the Generated.CSharp folder) for even more security.

Metadata Generation

There are many cases where module generation doesn't result in the most optimal names for things like parameters or commands. To enable this and many other customization options, we've introduced additional PowerShell-specific Open API extensions that can be specified separately from your main specification. For more information, check out PowerShell Extensions and New-PSSwaggerMetadataFile.

Dynamic generation of C# assembly

When importing the module for the first time, the packaged C# files will be automatically compiled if the expected assembly doesn't exist.

Microsoft.Rest.ServiceClientTracing support

To enable Microsoft.Rest.ServiceClientTracing support, pass -Debug into any generated command.

The included PowerShell tracer uses Write-Debug to write tracing messages.

When the module is imported to older version of PowerShell, the following steps will need to be taken:

  1. Implement either Microsoft.PowerShell.Commands.PSSwagger.PSSwaggerClientTracing or Microsoft.Rest.IServiceClientTracingInterceptor
  2. Call: [Microsoft.Rest.ServiceClientTracing]::AddTracingInterceptor($myTracer)
  3. If you use the Verbose or Debug streams of PowerShell, set $VerbosePreference or $DebugPreference, respectively

Note 1: We're not sure why yet, but something is setting [Microsoft.Rest.ServiceClientTracing]::IsEnabled to true before all cmdlet calls

Symbol Path

If the module's full CLR assembly is precompiled, the symbols folder will contain:

  1. Generated.cs - The C# code used to generate the assembly
  2. *.pdb - The corresponding PDB file that defines Generated.cs as the source file

Paging

When the "x-ms-pageable" extension is specified in the Swagger spec, paging is enabled in the generated module. This is true for both fragment URLs (when operationName is specified) and full URLs (when operationName is not specified).

A cmdlet that supports paging will have two additional optional parameters: -Paging: A switch parameter that specifies the cmdlet should only return the first page. To access the items, use $returnValue.Page -Page: Takes as input the last page return value and outputs the next page return value (again, to access the items, use $returnValue.Page). If the return value is null, no additional pages exist.

If -Paging is not specified and the cmdlet supports paging, the cmdlet will automatically unroll all pages. Assigning the result to a variable will result in all items being retrieved. Piping the cmdlet will result in pages being retrieved on-demand.

Silent execution when missing dependency packages

When dependency packages are expected to be missing, silent execution (bypassing the missing packages prompt) can be achieved by calling:

Import-Module PSSwaggerUtility
PSSwaggerUtility\Initialize-PSSwaggerDependencies -AcceptBootstrap -Azure

For Microsoft Azure modules, or:

Import-Module PSSwaggerUtility
PSSwaggerUtility\Initialize-PSSwaggerDependencies -AcceptBootstrap

For all other modules.

Notes

  1. Swagger Specification is at: http://swagger.io/specification/
  2. Azure ARM based Swagger documents at: https://github.com/Azure/azure-rest-api-specs
  3. AutoRest Generators: https://github.com/Azure/autorest/tree/master/src/generator

Developers

Testing

You can run tests right after cloning the repository. The test scripts should install the required packages. To run the tests, navigate to the Tests directory, then run:

.\run-tests.ps1 -TestFramework <framework> -Verbose -TestSuite <TestSuite> -TestName <TestName>

TestFramework should be one of "net452" | "netstandard1.7". If you are on Windows, you can use either net452, which uses the full CLR, or netstandard1.7, which uses the Core CLR. For Linux, Darwin, or Nano Server users, you will have to use netstandard1.7.

TestFramework defaults to net452

Use TestSuite or TestName parameters to filter by Pester Tag or test name.

What does the script do?

The script will ensure dependencies exist on your machine, like AutoRest, node.js, npm, json-server, and dotnet CLI. Then it will run pester tests in a separate PS session and validate no tests failed.

If dotnet CLI is already in your PS session's path, and you don't need to upgrade, you can use the parameter -SkipBootstrap to skip dotnet CLI bootstrapping and save lots of time.

Unit tests

Scenario Description
Get-InternalFileHash Tests that Get-InternalFileHash is equivalent to Get-FileHash in PS 5.1+

Scenario tests

The scenario test suite contains tests that hit actual (local) web API endpoints. The following scenarios are covered:

Scenario Description
PsSwaggerTestsBasic A very basic test of a single string-only path using get and post.

PSSwagger.LiveTestFramework

The PSSwagger implementation of the Azure Live Test Framework protocol is currently located in this repository in a subdirectory. Once PSSwaggerUtility is published to PowerShellGallery.com, the PSSwagger.LiveTestFramework code will be moved to a separate repository. You can find the readme for the PSSwagger.LiveTestFramework module here.

Code of Conduct

This project has adopted the Microsoft Open Source Code of Conduct. For more information see the Code of Conduct FAQ or contact [email protected] with any additional questions or comments.

More Repositories

1

PowerShell

PowerShell for every system!
C#
45,459
star
2

Win32-OpenSSH

Win32 port of OpenSSH
7,290
star
3

PSReadLine

A bash inspired readline implementation for PowerShell
C#
3,700
star
4

PSScriptAnalyzer

Download ScriptAnalyzer from PowerShellGallery
C#
1,842
star
5

vscode-powershell

Provides PowerShell language and debugging support for Visual Studio Code
TypeScript
1,687
star
6

DscResources

Central repository for PowerShell Desired State Configuration (DSC) resources.
772
star
7

ConsoleGuiTools

Modules that mix PowerShell and GUIs/CUIs!
C#
772
star
8

platyPS

Write PowerShell External Help in Markdown
C#
765
star
9

PowerShellEditorServices

A common platform for PowerShell development support in any editor or application!
C#
623
star
10

Polaris

A cross-platform, minimalist web framework for PowerShell
PowerShell
507
star
11

PSResourceGet

PSResourceGet is the package manager for PowerShell
C#
483
star
12

PowerShellGetv2

PowerShellGet is the Package Manager for PowerShell
PowerShell
430
star
13

PowerShell-RFC

RFC (Request for Comments) documents for community feedback on design changes and improvements to PowerShell ecosystem
PowerShell
430
star
14

PowerShell-Docker

Repository for building PowerShell Docker images
Dockerfile
400
star
15

Crescendo

a module for wrapping native applications in a PowerShell function and module
PowerShell
397
star
16

SecretManagement

PowerShell module to consistent usage of secrets through different extension vaults
C#
321
star
17

JEA

Just Enough Administration
PowerShell
254
star
18

PowerShellGallery

PowerShell
228
star
19

Operation-Validation-Framework

PowerShell
223
star
20

DSC

This repo is for the DSC v3 project
Rust
192
star
21

SHiPS

Simple Hierarchy in PowerShell - developing PowerShell provider got so much easier
C#
188
star
22

PowerShellStandard

C#
157
star
23

SecretStore

C#
155
star
24

PSPrivateGallery

DSC Resources and Configurations to deploy and manage Private PowerShell Gallery
PowerShell
148
star
25

CompletionPredictor

C#
145
star
26

GPRegistryPolicyParser

PowerShell
140
star
27

WindowsCompatibility

Module that allows Windows PowerShell Modules to be used from PSCore6
PowerShell
137
star
28

PowerShell-Tests

Pester based tests for testing PowerShell
PowerShell
130
star
29

PowerShell-IoT

Interact with I2C, SPI & GPIO devices using PowerShell Core!
C#
130
star
30

PSDscResources

PowerShell
129
star
31

EditorSyntax

PowerShell syntax highlighting for editors (VS Code, Atom, SublimeText, TextMate, etc.) and GitHub!
PowerShell
127
star
32

Modules

C#
111
star
33

Phosphor

A library and PowerShell module for generating user interfaces from PowerShell modules
TypeScript
109
star
34

GPRegistryPolicy

PowerShell
102
star
35

Community-Blog

Submissions for posts to the PowerShell Community Blog -https://devblogs.microsoft.com/powershell-community
102
star
36

psl-omi-provider

PSRP Linux support library
C
100
star
37

Microsoft.PowerShell.Archive

Archive PowerShell module contains cmdlets for working with ZIP archives
C#
93
star
38

PSArm

PSArm is a PowerShell module that provides a PowerShell-embedded domain-specific language (DSL) for Azure Resource Manager (ARM) templates
C#
77
star
39

Remotely

Enable remote execution of scripts. Works with Pester.
PowerShell
74
star
40

TextUtility

Microsoft.PowerShell.TextUtility module
C#
64
star
41

PSDesiredStateConfiguration

Source for https://www.powershellgallery.com/packages/PSDesiredStateConfiguration module
PowerShell
59
star
42

ProjectMercury

An interactive shell to work with AI-powered assistance providers
C#
56
star
43

Demo_CI

PowerShell
55
star
44

PowerShellGet

This module provide functions used with PowerShellGet v3 to provide compatibility with scripts expecting PowerShellGet v2
PowerShell
55
star
45

DscResource.Tests

Common meta tests for PowerShell DSC resources repositories.
PowerShell
51
star
46

PowerShell-Native

C++
48
star
47

Whitepapers

Staging area for new documents and whitepapers
46
star
48

AzurePSDrive

Navigate Azure Resources Just like A FileSystem
PowerShell
41
star
49

ContainerProvider

ContainerImageProvider
PowerShell
38
star
50

PrivateCloud.DiagnosticInfo

PowerShell
38
star
51

PackageManagementProviderResource

Modules with DSC resources for the PackageManagement(aka OneGet) providers.
PowerShell
37
star
52

Pager

Project for console pager, which is published as a NuGet package Microsoft.PowerShell.Pager
C#
34
star
53

MarkdownRender

C#
32
star
54

DSC-Samples

Samples and tutorials for DSC v3
Go
29
star
55

PlasterBuild

Provides common build tasks for PowerShell module projects
PowerShell
29
star
56

PowerShell-Snap

PowerShell
28
star
57

SelfSignedCertificate

A module for generating self-signed certificates in PowerShell Core
PowerShell
27
star
58

CimPSDrive

SHiPS based provider to navigate CIM classes and namespaces
PowerShell
27
star
59

DscConfigurations

PowerShell
27
star
60

tree-sitter-PowerShell

JavaScript
25
star
61

GitHub-Actions

PowerShell
25
star
62

MMI

C#
24
star
63

Microsoft.PowerShell.Kubectl

PowerShell module to manage Kubernetes
PowerShell
23
star
64

LibreSSL

C
22
star
65

ODataUtils

PowerShell Module to generate cmdlets from an OData endpoint
PowerShell
22
star
66

ThreadJob

PowerShell
22
star
67

WmiNamespaceSecurityDsc

This module contains DSC resources to manage WMI Namespace Security.
PowerShell
21
star
68

FileUtility

C#
21
star
69

UnixCompleters

C#
20
star
70

Homebrew-Tap

Ruby
19
star
71

underhanded-powershell

Underhanded PowerShell Contest Repository
PowerShell
18
star
72

Hardware-Management-Module

PowerShell
17
star
73

generator-powershell

Create PowerShell modules and scripts using Yeoman!
JavaScript
16
star
74

Announcements

PowerShell Team Announcements
15
star
75

JsonAdapter

C#
14
star
76

PowerShellModuleCoverage

Track issues related to using Windows PowerShell modules with PowerShell
13
star
77

whatsnew

PowerShell
13
star
78

PSRelease

PowerShell
11
star
79

AwsDscToolkit

AWS DSC Toolkit
PowerShell
10
star
80

DscResource.Template

PowerShell
10
star
81

command-not-found

C#
10
star
82

TemplateConfig

Provides template for organizing DSC configuration scripts and supporting modules
PowerShell
10
star
83

SystemLocaleDsc

DSC Resource for configuring Windows System Locale
PowerShell
9
star
84

SmartPackageProvider

A PackageManagement provider to find and install packages using search engines
C#
9
star
85

xDisk

PowerShell
8
star
86

psl-pester

Fork of Pester for compatibility with PowerShell on Linux.
PowerShell
8
star
87

PowerShell-Blog-Samples

Samples for the PowerShell blog
PowerShell
8
star
88

ZLib

C
7
star
89

AWSBootStrapper

PowerShell
6
star
90

WSAProvider

A PackageManagement provider to find and install packages for Appx based packaging format
PowerShell
6
star
91

PowerShellGetDsc

DSCResources for the PowerShellGet module
PowerShell
5
star
92

DscConfiguration.Tests

Test automation scripts for evaluating the quality of DSC Configurations using Azure virtual machines and Azure Automation DSC.
PowerShell
5
star
93

SILDeploymentHelper

PowerShell
4
star
94

vscode-powershellise-keybindings

A Visual Studio Code extension that configures PowerShell ISE keybindings.
TypeScript
4
star
95

PSPackageProject

PowerShell
4
star
96

Compliance

3
star
97

.github

3
star
98

ISEAddOnExamples

3
star
99

pscore-docs-dotnet

PowerShell
2
star
100

JsonAdapterFeedbackPredictor

1
star