ottomatt - UncommonSense.PowerShell.TypeData 1.2.0

PowerShell module to help build PowerShell type extension files.

If you're not familiar with the PowerShell type extension file format, please run Get-Help about_Types.ps1xml in your PowerShell console to find additional information.

In an attempt to simplify and shorten the syntax:

  • all the cmdlets in the module have aliases (see below);
  • most (if not all) cmdlet parameters are positional;
  • each parent node has a script block parameter for easily adding child nodes.

PM> Install-Package UncommonSense.PowerShell.TypeData -Version 1.2.0 -Source https://www.myget.org/F/ottomatt/api/v3/index.json

Copy to clipboard

> nuget.exe install UncommonSense.PowerShell.TypeData -Version 1.2.0 -Source https://www.myget.org/F/ottomatt/api/v3/index.json

Copy to clipboard

> dotnet add package UncommonSense.PowerShell.TypeData --version 1.2.0 --source https://www.myget.org/F/ottomatt/api/v3/index.json

Copy to clipboard
<PackageReference Include="UncommonSense.PowerShell.TypeData" Version="1.2.0" />
Copy to clipboard
source https://www.myget.org/F/ottomatt/api/v3/index.json

nuget UncommonSense.PowerShell.TypeData  ~> 1.2.0
Copy to clipboard

> choco install UncommonSense.PowerShell.TypeData --version 1.2.0 --source https://www.myget.org/F/ottomatt/api/v2

Copy to clipboard
Import-Module PowerShellGet
Register-PSRepository -Name "ottomatt" -SourceLocation "https://www.myget.org/F/ottomatt/api/v2"
Install-Module -Name "UncommonSense.PowerShell.TypeData" -RequiredVersion "1.2.0" -Repository "ottomatt" 
Copy to clipboard

UncommonSense.PowerShell.TypeData

PowerShell module to help build PowerShell type extension files

https://ci.appveyor.com/api/projects/status/github/jhoek/UncommonSense.PowerShell.TypeData?svg=true

Installation

  1. Build the project
  2. From the build output folder (depends on your build configuration; typically bin/Debug or bin/Release), copy the following items to a folder called UncommonSense.PowerShell.TypeData anywhere in your PowerShell module path. To find your module path, type $env:PSModulePath -split ';' in a PowerShell console.
  • UncommonSense.PowerShell.TypeData.dll
  • UncommonSense.PowerShell.TypeData.psd1
  • UncommonSense.PowerShell.TypeData.psm1

Alternatively, you could leave the files where they are, and call Import-Module {Full/Path/To/UncommonSense.PowerShell.TypeData.psd1}

Usage

If you're not familiar with the PowerShell type extension file format, please run Get-Help about_Types.ps1xml in your PowerShell console to find additional information.

In an attempt to simplify and shorten the syntax:

  • all the cmdlets in the module have aliases (see below);
  • most (if not all) cmdlet parameters are positional;
  • each parent node has a script block parameter for easily adding child nodes.

Aliases

The following aliases are defined automatically.

Set-Alias -Name Types -Value New-TypeData
Set-Alias -Name _Type -Value New-Type
Set-Alias -Name AliasProperty -Value New-AliasProperty
Set-Alias -Name CodeMethod -Value New-CodeMethod
Set-Alias -Name CodeProperty -Value New-CodeProperty
Set-Alias -name CodeReference -Value New-CodeReference
Set-Alias -Name MemberSet -Value New-MemberSet
Set-Alias -Name NoteProperty -Value New-NoteProperty
Set-Alias -Name PropertySet -Value New-PropertySet
Set-Alias -Name ScriptMethod -Value New-ScriptMethod
Set-Alias -Name ScriptProperty -Value New-ScriptProperty
Set-Alias -Name TypeConverter -Value New-TypeConverter

Note: The alias for New-Type is _Type instead of Type, because PowerShell installs Type at a higher scope level as an alias for Get-Content.

Scenario

A possible usage scenario might look like this (using the cmdlet aliases and leaving out parameter names as much as possible):

Types {
    _Type Foo {
        NoteProperty Baz Bar
        ScriptProperty Qux 'Get-Quux'
        MemberSet Quux {
            NoteProperty Quuux Boink
        }
    }
}

The resulting XML looks like this:

<Types>
  <Type>
    <Name>Foo</Name>
    <Members>
      <NoteProperty>
        <Name>Baz</Name>
        <Value>Bar</Value>
      </NoteProperty>
      <ScriptProperty>
        <Name>Qux</Name>
        <GetScriptBlock>Get-Quux</GetScriptBlock>
      </ScriptProperty>
      <MemberSet>
        <Name>Quux</Name>
        <Members>
            <NoteProperty>
                <Name>Quuux</Name>
                <Value>Boink</Value>
            </NoteProperty>
        <Members />
      </MemberSet>
    </Members>
  </Type>
</Types>

You could then use redirection, Out-File or Set-Content to send the XML to type extension file, which can be loaded into your PowerShell session using Update-TypeData, or can be made part of your own PowerShell module.

Encoding

Additional text, such as an XML declaration specifying the encoding, can be specified in New-TypeData's -PreContent parameter. This text will be emitted before the actual type data.

New-TypeData -PreContent '<?xml version="1.0" encoding="UTF-8"?>' { ... }

When specifying an encoding in -PreContent, make sure you use the same encoding when sending the cmdlet's output to a file or stream.

New-TypeData -PreContent '<?xml version="1.0" encoding="UTF-8"?>' { ... } | Out-File -Encoding Utf8 -FilePath '...'

Owners

Laurent Dardenne

Authors

Jan Hoek

Project URL

https://github.com/jhoek/UncommonSense.PowerShell.TypeData

License

GPL-3.0

Tags

type extended system ets extensions add-member update-typedata ps1xml

Info

68 total downloads
46 downloads for version 1.2.0
Download (34.06 KB)
Found on the current feed only

Package history

Version Size Last updated Downloads Mirrored?
1.2.0 34.06 KB Sun, 16 Apr 2017 09:06:24 GMT 46
1.1.0 31.49 KB Fri, 14 Apr 2017 08:08:10 GMT 11
1.0.0 27.58 KB Wed, 15 Feb 2017 17:18:36 GMT 11