Skip to content

Commit

Permalink
v1.7.0
Browse files Browse the repository at this point in the history
  • Loading branch information
jdhitsolutions committed Mar 9, 2021
1 parent 4ede25d commit 9668750
Show file tree
Hide file tree
Showing 13 changed files with 415 additions and 112 deletions.
12 changes: 11 additions & 1 deletion Changelog.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,16 @@
# Change Log for PSTypeExtensionTools

## v1.7.0

+ Added `services.types.ps1xml` to samples folder.
+ Updated `README.md`.
+ Updates `README.md` in Samples folder.
+ Changed statements using `Out-Null` to use `[void]`.
+ Modified `Export-PSTypeExtension` to support appending in a very specific use-case. ([Issue #16](https://github.com/jdhitsolutions/PSTypeExtensionTools/issues/16))
+ Added a private function, `_convertTypeName` to convert typename values to properly cased names.
+ Modified `Export-PSTypeExtension` to add a `-Passthru` parameter. ([Issue #18](https://github.com/jdhitsolutions/PSTypeExtensionTools/issues/18))
+ Updated help.

## v1.6.0

+ Re-organized module structure.
Expand All @@ -9,7 +20,6 @@

## v1.5.1


+ Fixed error in exporting aliases.
+ Updated license file.
+ Very minor help corrections.
Expand Down
Binary file modified PSTypeExtensionTools.psd1
Binary file not shown.
33 changes: 17 additions & 16 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ Install-Module PSTypeExtensionTools
Let's say you want to update a number object, but you have no idea what the type name is. Once you have read help for the commands in this module, you could run a PowerShell command like this:

```powershell
PS C:\> 123 | Get-PSType | Add-PSTypeExtension -MemberType ScriptProperty -MemberName SquareRoot -Value { [math]::Sqrt($this)}
123 | Get-PSType | Add-PSTypeExtension -MemberType ScriptProperty -MemberName SquareRoot -Value { [math]::Sqrt($this)}
```

Use `$this` to reference the object instead of `$_`. Now you can get the new property.
Expand All @@ -31,11 +31,10 @@ PS C:\> $x.SquareRoot
Once you know the type name, you can add other type extensions.

```powershell
PS C:\> Add-PSTypeExtension -TypeName system.int32 -MemberType ScriptProperty -MemberName Squared -value { $this*$this}
PS C:\> Add-PSTypeExtension -TypeName system.int32 -MemberType ScriptProperty -MemberName Cubed -value { [math]::Pow($this,3)}
PS C:\> Add-PSTypeExtension -TypeName system.int32 -MemberType ScriptProperty -MemberName Value -value { $this}
PS C:\> Add-PSTypeExtension -TypeName system.int32 -MemberType ScriptMethod -MemberName GetPercent -value {Param([int32]$Total,[int32]$Round=2) [math]::Round(($this/$total)*100,$round)}
Add-PSTypeExtension -TypeName system.int32 -MemberType ScriptProperty -MemberName Squared -value {$this*$this}
Add-PSTypeExtension -TypeName system.int32 -MemberType ScriptProperty -MemberName Cubed -value {[math]::Pow($this,3)}
Add-PSTypeExtension -TypeName system.int32 -MemberType ScriptProperty -MemberName Value -value {$this}
Add-PSTypeExtension -TypeName system.int32 -MemberType ScriptMethod -MemberName GetPercent -value {Param([int32]$Total,[int32]$Round=2) [math]::Round(($this/$total)*100,$round)}
```

Here's how it might look:
Expand All @@ -56,7 +55,7 @@ PS C:\> $x.GetPercent(110,4)
34.5455
```

To see what has been defined, you can use `Get-PSTypeExtension`. You can choose to see all extensions or selected ones by member name.
To see current type extensions, you can use `Get-PSTypeExtension`. You can choose to see all extensions or selected ones by member name. CodeProperty extensions are hidden by default.

```powershell
PS C:\> Get-PSTypeExtension system.int32
Expand All @@ -75,7 +74,8 @@ GetPercent ScriptMethod Param([int32]$Total,[int32]$Round=2) [math]::Round(($t
If you always want these extensions, you would have to put the commands into your PowerShell profile script. Or you can export the extensions to a JSON or XML file. You can either export all members or selected ones, which is helpful if you are extending a type that already has type extensions from PowerShell.

```powershell
PS C:\> Get-PSTypeExtension system.int32 | Export-PSTypeExtension -TypeName system.int32 -Path c:\work\int32-types.json
PS C:\> Get-PSTypeExtension system.int32 |
Export-PSTypeExtension -TypeName system.int32 -Path c:\work\int32-types.json
```

In your PowerShell profile scrip,t you can then re-import the type extension definitions.
Expand Down Expand Up @@ -110,10 +110,10 @@ Import type extension definitions from a JSON file or XML.

In addition to custom properties, PowerShell also has the idea of a _propertyset_. This allows you to reference a group of properties with a single name.

Let's say you have loaded the sample fileinfo type extensions from this module.
Let's say you have loaded the sample 'System.IO.FileInfo` type extensions from this module.

```powershell
PS C:\> Import-PSTypeExtension -Path $PSTypeSamples\fileinfo-extensions.json
Import-PSTypeExtension -Path $PSTypeSamples\fileinfo-extensions.json
```powershell
You could write a command like this:
Expand All @@ -125,7 +125,7 @@ dir c:\work -file | Select-Object Name,Size,LastWriteTime,Age
Or you could create a custom property set. These have to be defined in `ps1xml` files. The `New-PSPropertySet` simplifies this process.

```powershell
New-PSPropertySet -Typename System.IO.FileInfo -Name FileAge -Properties Name,Size,LastWriteTime,Age -FilePath d:\temp\Fileinfo.types.ps1xml
New-PSPropertySet -Typename System.IO.FileInfo -Name FileAge -Properties Name,Size,LastWriteTime,Age -FilePath d:\temp\Fileinfo.types.ps1xml
```

I've included the file in the Samples folder.
Expand All @@ -151,13 +151,13 @@ If your property set is using custom properties, you need to load them into your

## Create ps1xml Files

The export command makes it easy to construct a ps1xml file. All you need to do is provide the type name and the extensions you want to export, and it will create a properly formatted ps1xml file that you can import into a session with `Update-TypeData` or distribute with a module. No more clunky XML copying, pasting, and hoping for the best.
The `Export-PSTypeExtension` command will also export extensions to a properly formatted .ps1xml file. This can be useful when building type extension files for a module where you want to use the traditional ps1xml form. You can also import these types of files with `Update-TypeData` with the `-AppendPath` or -PrependPath parameters.

Well, there is one reason you still might need to do some copying and pasting. Technically, you can define all custom properties, including property sets, in a single .ps1xml file. However, I don't have a simple command to export everything for a single type to a single file. For now, you can create a `<typename>.types.ps1xml` file for your custom extensions. Then manually merge the `Members` section from your property set .ps1xml file. This is only necessary if you have custom extensions **and** one or more property sets defined for a given type.
When exporting to .ps1xml file, `Export-PSTypeExtension` has a dynamic parameter, `Append`. This allows you to combine multiple type extensions into a single file. If you intend to use a property set, create that file first. Then append your custom type extensions to that file.

## I Want to Try

You can find a number of type extension exports in the [Samples](./samples) folder. The location will be saved to a global variable, `$PSTypeSamples`. This makes it a bit easier to import.
You can find sample and demonstration type extension exports in the [Samples](./samples) folder. When you import the module, this location is saved to a global variable, `$PSTypeSamples`.

```powershell
PS C:\> dir $PSTypeSamples
Expand Down Expand Up @@ -193,7 +193,8 @@ VERBOSE: Creating scriptblock from value
VERBOSE: Performing the operation "Adding ScriptProperty SumGB" on target "Microsoft.PowerShell.Commands.GenericMeasureInfo".
VERBOSE: Ending: Import-PSTypeExtension
PS C:\> dir D:\VMDisks\ -file -Recurse | measure length -sum | select Count,SumGB
PS C:\> dir D:\VMDisks\ -file -recurse | measure length -sum |
select Count,SumGB
Count SumGB
----- -----
Expand All @@ -208,4 +209,4 @@ There is also an about topic you can read:
help about_PSTypeExtensionTools
```

Last Updated 2021-03-08 21:19:40Z
Last Updated 2021-03-09 16:42:02Z
2 changes: 1 addition & 1 deletion docs/Add-PSTypeExtension.md
Original file line number Diff line number Diff line change
Expand Up @@ -184,4 +184,4 @@ http://jdhitsolutions.com/blog/essential-powershell-resources/
## RELATED LINKS
[Get-PSTypeExtension]()
[Get-PSTypeExtension](Get-PSTypeExtension.md)
79 changes: 64 additions & 15 deletions docs/Export-PSTypeExtension.md
Original file line number Diff line number Diff line change
Expand Up @@ -16,25 +16,21 @@ Export type extensions to a file.
### Object (Default)

```yaml
Export-PSTypeExtension -Path <String> [-WhatIf] [-Confirm] [<CommonParameters>]
Export-PSTypeExtension -Path <String> [-InputObject <Object>] [-Passthru] [Append] [-WhatIf] [-Confirm] [<CommonParameters>]
```

### Name

```yaml
Export-PSTypeExtension [-TypeName] <String> -MemberName <String[]>
-Path <String> [-WhatIf] [-Confirm] [<CommonParameters>]
```

### object

```yaml
Export-PSTypeExtension -Path <String> [-InputObject <Object>] [-WhatIf] [-Confirm] [<CommonParameters>]
-Path <String> [-Passthru] [Append] [-WhatIf] [-Confirm] [<CommonParameters>]
```

## DESCRIPTION

This command can be used in 2 ways. First, you can export custom type information to a JSON or XML file to make it easier to recreate them in another PowerShell session. Or you can export the type extensions to a properly formatted ps1xml file which you can use with Update-TypeData. The command will create the appropriate file based on the extension in the specified path.
You can use this command in two ways. First, you can export custom type information to a JSON or XML file to import in another PowerShell session using Import-PSTypeExtension. Or you can export the type extensions to a properly formatted ps1xml file, which you can import using Update-TypeData. Export-PSTypeExtension will create the appropriate file based on the extension in the specified path.

If you are exporting to a .ps1xml file, you can use a dynamic parameter called Append. You might want to do this if you have created a property set using New-PSPropertySet, and want to include additional type extensions in the same file.

## EXAMPLES

Expand All @@ -57,10 +53,29 @@ Get all type extensions for System.String and export to an XML file.
### EXAMPLE 3

```powershell
PS C:\> Get-PSTypeExtension system.string -members "IsIpAddress","Size","Randomize" | Export-PSTypeExtension -path c:\work\mystring.type.ps1xml
PS C:\> Get-PSTypeExtension system.string -members "IsIpAddress","Size","Randomize" | Export-PSTypeExtension -path c:\work\mystring.type.ps1xml -passthru
Directory: C:\work
Mode LastWriteTime Length Name
---- ------------- ------ ----
-a---- 3/9/2021 11:15 AM 858 mystring.type.ps1xml
```

Export the selected members to a properly formatted ps1xml file.
Export the selected members to a ps1xml file and pass the file object to the pipeline.

### EXAMPLE 4

```powershell
PS C:\work> Get-PSTypeExtension system.string |
Export-PSTypeExtension -Path .\all.ps1xml
PS C:\work> Get-PSTypeExtension system.io.fileinfo |
Export-PSTypeExtension -Path .\all.ps1xml -append
PS C:\work> Get-PSTypeExtension system.serviceprocess.servicecontroller -Members State | Export-PSTypeExtension -Path .\all.ps1xml -append
```

Export multiple type extensions to the same .ps1xml file. You can use Update-TypeData to load this file in another PowerShell session. Although, you might see errors if there is an existing type extension with the same name.

## PARAMETERS

Expand Down Expand Up @@ -150,7 +165,7 @@ This is typically the output of Get-PSSTypeExtension.
```yaml
Type: Object
Parameter Sets: object
Parameter Sets: Object
Aliases:

Required: False
Expand All @@ -160,9 +175,41 @@ Accept pipeline input: True (ByValue)
Accept wildcard characters: False
```
### -Passthru
Display the new file object.
```yaml
Type: SwitchParameter
Parameter Sets: (All)
Aliases:

Required: False
Position: Named
Default value: None
Accept pipeline input: False
Accept wildcard characters: False
```
### -Append
If you are exporting to a .ps1xml file, you can append to it. You might want to do this if you have created a property set using New-PSPropertySet and want to export your type extensions to the same file. Or you might want to export all of your type extensions to a single .ps1xml file so that anyone can import it using Update-TypeData. See examples.
```yaml
Type: SwitchParameter
Parameter Sets: (All)
Aliases:

Required: False
Position: Named
Default value: None
Accept pipeline input: False
Accept wildcard characters: False
```
### CommonParameters
This cmdlet supports the common parameters: -Debug, -ErrorAction, -ErrorVariable, -InformationAction, -InformationVariable, -OutVariable, -OutBuffer, -PipelineVariable, -Verbose, -WarningAction, and -WarningVariable. For more information, see about_CommonParameters (http://go.microsoft.com/fwlink/?LinkID=113216).
This cmdlet supports the common parameters: -Debug, -ErrorAction, -ErrorVariable, -InformationAction, -InformationVariable, -OutVariable, -OutBuffer, -PipelineVariable, -Verbose, -WarningAction, and -WarningVariable. For more information, see [about_CommonParameters](http://go.microsoft.com/fwlink/?LinkID=113216).
## INPUTS
Expand All @@ -172,13 +219,15 @@ This cmdlet supports the common parameters: -Debug, -ErrorAction, -ErrorVariable
### None
### Sytstem.IO.File
## NOTES
Learn more about PowerShell:
http://jdhitsolutions.com/blog/essential-powershell-resources/
## RELATED LINKS
[Import-PSTypeExtension]()
[Import-PSTypeExtension](Import-PSTypeExtension.md)
[Get-PSTypeExtension]()
[Get-PSTypeExtension](Get-PSTypeExtension.md)
4 changes: 2 additions & 2 deletions docs/Get-PSType.md
Original file line number Diff line number Diff line change
Expand Up @@ -104,8 +104,8 @@ http://jdhitsolutions.com/blog/essential-powershell-resources/
## RELATED LINKS
[Get-PSTypeExtension]()
[Get-PSTypeExtension](Get-PSTypeExtension.md)
[Add-PSTypeExtension]()
[Add-PSTypeExtension](Add-PSTypeExtension.md)
[Get-TypeData]()
6 changes: 3 additions & 3 deletions docs/Import-PSTypeExtension.md
Original file line number Diff line number Diff line change
Expand Up @@ -108,8 +108,8 @@ Learn more about PowerShell:
## RELATED LINKS
[Export-PSTypeExtension]()
[Export-PSTypeExtension](Export-PSTypeExtension.md)
[Update-TypeData]()
[Get-PSTypeExtension](Get-PSTypeExtension.md)
[Get-PSTypeExtension]()
[Update-TypeData]()
4 changes: 3 additions & 1 deletion docs/about_PSTypeExtensionTools.md
Original file line number Diff line number Diff line change
Expand Up @@ -98,7 +98,9 @@ Import-PSTypeExtension $PSTypeSamples\measure-extensions.json

### Creating ps1xml Files

The `Export-PSTypeExtension` command will also export extensions to a properly formatted .ps1xml file. This can be useful when building type extension files for a module where you want to use the traditional ps1xml form. You can also import these types of files with Update-TypeData with the -AppendPath or -PrependPath parameters.
The `Export-PSTypeExtension` command will also export extensions to a properly formatted .ps1xml file. This can be useful when building type extension files for a module where you want to use the traditional ps1xml form. You can also import these types of files with `Update-TypeData` with the -AppendPath or -PrependPath parameters.

When exporting to .ps1xml file, `Export-PSTypeExtension` has a dynamic parameter, `Append`. Tis allows you to combine multiple type extensions into a single file. If you intend to use a property set, create that file first. Then append your custom type extensions to that file.

## NOTE

Expand Down
Loading

0 comments on commit 9668750

Please sign in to comment.