Skip to content

Commit

Permalink
see changelog for v1.4.0
Browse files Browse the repository at this point in the history
  • Loading branch information
jdhitsolutions committed Feb 18, 2019
1 parent aa38709 commit ffa3c6c
Show file tree
Hide file tree
Showing 8 changed files with 151 additions and 61 deletions.
7 changes: 7 additions & 0 deletions Changelog.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,12 @@
# Change Log for PSTypeExtensionTools

## v1.4.0

+ Modified manifest to require PowerShell 5.1
+ Modified manifest to support both Desktop and Core PSEditions
+ Updates to some of the sample files
+ Updated `README.md`

## v1.3.0

+ file cleanup for the PowerShell Gallery
Expand Down
2 changes: 1 addition & 1 deletion License.txt
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
MIT License

Copyright (c) 2017-2018 JDH Information Technology Solutions, Inc.
Copyright (c) 2017-2019 JDH Information Technology Solutions, Inc.


Permission is hereby granted, free of charge, to any person obtaining a copy
Expand Down
Binary file modified PSTypeExtensionTools.psd1
Binary file not shown.
53 changes: 26 additions & 27 deletions PSTypeExtensionTools.psm1
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@


Function Get-PSTypeExtension {
[cmdletbinding()]
Param(
Expand All @@ -23,35 +22,35 @@ Function Get-PSTypeExtension {
[Parameter(HelpMessage = "Enter a comma separated list of member names", ParameterSetName = "members")]
[string[]]$Members
)

Begin {
Write-Verbose "Starting: $($MyInvocation.Mycommand)"
$typedata = @()
} #begin
Process {
Write-Verbose "Analyzing $typename"
$typedata += Get-TypeData -TypeName $typename


} #process
End {
End {
$typedata = $typedata | Select-Object -Unique
if ($typedata) {

if (-Not $Members) {
Write-Verbose "Getting all member names"
$Members = $typedata.members.keys

}
foreach ($name in $Members) {
Try {
Write-Verbose "Analyzing member $name"
$member = $typedata.members[$name]
$datatype = $member.gettype().name

Write-Verbose "Processing type $datatype"
Switch ($datatype) {
"AliasPropertyData" {
"AliasPropertyData" {
$def = [pscustomobject]@{
MemberType = "AliasProperty"
MemberName = $member.name
Expand All @@ -67,14 +66,14 @@ Function Get-PSTypeExtension {
}
$def = [pscustomobject]@{
MemberType = "ScriptProperty"
MemberName = $member.name
MemberName = $member.name
Value = $code
}
} #scriptproperty
"ScriptMethodData" {
$def = [pscustomobject]@{
MemberType = "ScriptMethod"
MemberName = $member.name
MemberName = $member.name
Value = $member.script.ToString().trim()
}
} #scriptmethod
Expand Down Expand Up @@ -107,7 +106,7 @@ Function Get-PSTypeExtension {
}
}
}

$def | Add-Member -MemberType NoteProperty -Name TypeName -Value $typedata.typename
#insert a typename
$def.psobject.typenames.insert(0, 'PSTypeExtension')
Expand All @@ -119,9 +118,9 @@ Function Get-PSTypeExtension {
Write-Warning "Could not find an extension member called $name"
write-Debug $_.exception.message
}

} #foreach

}
else {
Write-Warning "Failed to find any type extensions for [$Typename]."
Expand Down Expand Up @@ -165,7 +164,7 @@ Function Export-PSTypeExtension {
[Parameter(Mandatory, HelpMessage = "The name of the export file. The extension must be .json,.xml or .ps1xml")]
[ValidatePattern("\.(xml|json|ps1xml)$")]
[string]$Path,
[Parameter(ParameterSetName = "object", ValueFromPipeline)]
[Parameter(ParameterSetName = "object", ValueFromPipeline)]
[object]$InputObject
)
Begin {
Expand All @@ -182,7 +181,7 @@ Function Export-PSTypeExtension {
foreach ($member in $membername) {
$data += Get-PSTypeExtension -TypeName $Typename -Members $Member
}
}
}
}
End {
if ($Path -match "\.ps1xml$") {
Expand All @@ -195,24 +194,24 @@ Function Export-PSTypeExtension {
$dec = $Doc.CreateXmlDeclaration("1.0", "UTF-8", $null)
#append to document
$doc.AppendChild($dec) | Out-Null

#create a comment and append it in one line
$text = @"
Custom type extensions generated by $($env:username)
$(Get-Date)
"@
$doc.AppendChild($doc.CreateComment($text)) | Out-Null

#create root Node
$root = $doc.CreateNode("element", "Types", $null)
$main = $doc.CreateNode("element", "Type", $null)
$name = $doc.CreateElement("Name")
$name.innerText = $data[0].TypeName
$main.AppendChild($name) | out-null
$member = $doc.CreateNode("element", "Members", $null)
foreach ($extension in $data) {
foreach ($extension in $data) {
Write-Verbose "Exporting $($extension.membername)"
$membertype = $doc.createNode("element", $extension.memberType, $null)
$membernameEL = $doc.CreateElement("Name")
Expand All @@ -225,7 +224,7 @@ $(Get-Date)
$memberdef = $doc.createelement("Script")
}
"ScriptProperty" {
$memberdef = $doc.createelement("GetScriptBlock")
$memberdef = $doc.createelement("GetScriptBlock")
}
"AliasProperty" {
$memberdef = $doc.createelement("ReferencedMemberName")
Expand All @@ -240,7 +239,7 @@ $(Get-Date)
$memberdef.InnerText = $extension.value
$membertype.AppendChild($memberdef)| out-null
$member.AppendChild($membertype) | out-null

} #foreach
$main.AppendChild($member) | out-null
$root.AppendChild($main) | Out-Null
Expand All @@ -262,7 +261,7 @@ $(Get-Date)
Write-Verbose "Exporting data to $path"
Write-Verbose "Ending: $($MyInvocation.Mycommand)"
}

} #end Export-PSTypeExtension

Function Import-PSTypeExtension {
Expand All @@ -283,7 +282,7 @@ Function Import-PSTypeExtension {
Write-Verbose "Importing file $(Convert-path $Path)"
if ($path -match "\.xml$") {
#xml format seems to add an extra entry
$import = Import-clixml -Path $path | Where-Object MemberType
$import = Import-clixml -Path $path | Where-Object MemberType
}
else {
$import = Get-Content -path $path | ConvertFrom-Json
Expand Down Expand Up @@ -311,15 +310,15 @@ Function Import-PSTypeExtension {
End {
Write-Verbose "Ending: $($myInvocation.mycommand)"
}

} #end Import-PSTypeExtension

Function Add-PSTypeExtension {
[cmdletbinding(SupportsShouldProcess)]
[Alias('Set-PSTypeExtension')]

Param(
[Parameter(Position = 0, Mandatory,
[Parameter(Position = 0, Mandatory,
ValueFromPipeline,
HelpMessage = "Enter the name of a type like system.io.fileinfo")]
[string]$TypeName,
Expand Down Expand Up @@ -349,7 +348,7 @@ Function Add-PSTypeExtension {
End {
Write-Verbose "[$((Get-Date).TimeofDay) END ] Ending $($myinvocation.mycommand)"

} #end
} #end

} #close Add-MyTypeExtension

Expand Down
62 changes: 52 additions & 10 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,7 @@ PS C:\> $x.GetPercent(110,4)
To see what has been defined you can use `Get-PSTypeExtension`. You can choose to see all extensions or selected ones by member name.

```powershell
PS C:\> Get-PSTypeExtension system.int32
PS C:\> Get-PSTypeExtension system.int32
TypeName: System.Int32
Expand Down Expand Up @@ -86,32 +86,74 @@ Import-PSTypeExtension -Path C:\work\int32-types.json

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.

## I Want to Try

You can find a number of type extension exports in the [Samples](./samples) folder.

## PSTypeExtensionTools Cmdlets

### [Add-PSTypeExtension](./docs/Add-PSTypeExtension.md)
### [Add-PSTypeExtension](/docs/Add-PSTypeExtension.md)

Add a new type extension such as an Alias or ScriptProperty.

### [Export-PSTypeExtension](./docs/Export-PSTypeExtension.md)
### [Export-PSTypeExtension](/docs/Export-PSTypeExtension.md)

Export type extensions to a json, xml or ps1xml file.

### [Get-PSType](./docs/Get-PSType.md)
### [Get-PSType](/docs/Get-PSType.md)

Get the type name of an object.

### [Get-PSTypeExtension](./docs/Get-PSTypeExtension.md)
### [Get-PSTypeExtension](/docs/Get-PSTypeExtension.md)

Get type extensions for a given type.

### [Import-PSTypeExtension](./docs/Import-PSTypeExtension.md)
### [Import-PSTypeExtension](/docs/Import-PSTypeExtension.md)

Import type extension definitions from a json file or xml.

## 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.

```powershell
PS C:\> dir $PSTypeSamples
Directory: C:\scripts\PSTypeExtensionTools\samples
Mode LastWriteTime Length Name
---- ------------- ------ ----
-a---- 12/15/2017 2:25 PM 766 cimlogicaldisk-extensions.json
-a---- 9/28/2018 9:48 AM 265 datetime-extensions.json
-a---- 12/15/2017 5:09 PM 232 eventlog-type.json
-a---- 2/18/2019 1:18 PM 1266 fileinfo-extensions.json
-a---- 11/13/2017 8:37 AM 901 int32-types.json
-a---- 11/1/2017 6:18 PM 653 measure-extensions.json
-a---- 11/13/2017 8:49 AM 890 process-types.xml
-a---- 12/15/2017 6:09 PM 628 README.md
-a---- 12/15/2017 2:09 PM 1246 stringtypes.json
-a---- 11/9/2017 12:08 PM 3024 vm-extensions.json
PS C:\> Import-PSTypeExtension $PSTypeSamples\measure-extensions.json -Verbose
VERBOSE: Starting: Import-PSTypeExtension
VERBOSE: Importing file C:\scripts\PSTypeExtensionTools\samples\measure-extensions.json
VERBOSE: Processing ScriptProperty : SumKB
VERBOSE: Creating scriptblock from value
VERBOSE: Performing the operation "Adding ScriptProperty SumKB" on target "Microsoft.PowerShell.Commands.GenericMeasureInfo".
VERBOSE: Processing ScriptProperty : SumMB
VERBOSE: Creating scriptblock from value
VERBOSE: Performing the operation "Adding ScriptProperty SumMB" on target "Microsoft.PowerShell.Commands.GenericMeasureInfo".
VERBOSE: Processing ScriptProperty : SumGB
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
Count SumGB
----- -----
4 50.2031
```

This project was first described at http://jdhitsolutions.com/blog/powershell/5777/a-powershell-module-for-your-type-extensions

There is also an about topic you can read:
Expand All @@ -120,4 +162,4 @@ There is also an about topic you can read:
help about_pstypeextensiontools
```

*last updated 23 October 2018*
*last updated 18 February 2019*
1 change: 0 additions & 1 deletion docs/PSTypeExtensionTools.md
Original file line number Diff line number Diff line change
Expand Up @@ -33,4 +33,3 @@ Get type extensions for a given type.
### [Import-PSTypeExtension](Import-PSTypeExtension.md)

Import type extension definitions from a json or XML file.

55 changes: 33 additions & 22 deletions samples/fileinfo-extensions.json
Original file line number Diff line number Diff line change
@@ -1,32 +1,43 @@
[
[{
"MemberType": "AliasProperty",
"MemberName": "Size",
"Value": "Length",
"TypeName": "System.IO.FileInfo"
},
{
"MemberType": "AliasProperty",
"MemberName": "Size",
"Value": "Length",
"TypeName": "System.IO.FileInfo"
"MemberType": "ScriptProperty",
"MemberName": "SizeKB",
"Value": "[math]::Round($this.length/1KB,2)",
"TypeName": "System.IO.FileInfo"
},
{
"MemberType": "ScriptProperty",
"MemberName": "SizeKB",
"Value": "[math]::Round($this.length/1KB,2)",
"TypeName": "System.IO.FileInfo"
"MemberType": "ScriptProperty",
"MemberName": "SizeMB",
"Value": "[math]::Round($this.length/1MB,2)",
"TypeName": "System.IO.FileInfo"
},
{
"MemberType": "ScriptProperty",
"MemberName": "SizeMB",
"Value": "[math]::Round($this.length/1MB,2)",
"TypeName": "System.IO.FileInfo"
"MemberType": "ScriptProperty",
"MemberName": "SizeGB",
"Value": "[math]::Round($this.length/1GB,2)",
"TypeName": "System.IO.FileInfo"
},
{
"MemberType": "ScriptProperty",
"MemberName": "Age",
"Value": "'{0:dd\\.hh\\:mm\\:ss}' -f ((Get-Date) - $this.LastWriteTime)",
"TypeName": "System.IO.FileInfo"
},
{
"MemberType": "AliasProperty",
"MemberName": "Modified",
"Value": "LastWriteTime",
"TypeName": "System.IO.FileInfo"
"MemberType": "AliasProperty",
"MemberName": "Modified",
"Value": "LastWriteTime",
"TypeName": "System.IO.FileInfo"
},
{
"MemberType": "AliasProperty",
"MemberName": "Created",
"Value": "CreationTime",
"TypeName": "System.IO.FileInfo"
"MemberType": "AliasProperty",
"MemberName": "Created",
"Value": "CreationTime",
"TypeName": "System.IO.FileInfo"
}
]
]
Loading

0 comments on commit ffa3c6c

Please sign in to comment.