Skip to content

Commit b093ac6

Browse files
v1.8.0
1 parent 554ccad commit b093ac6

10 files changed

+138
-42
lines changed

Changelog.md

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,14 @@
11
# Change Log for PSTypeExtensionTools
22

3+
## v1.8.0
4+
5+
+ Fixed bugs in `Get-PSTypeExtension` and `Export-PSTypeExtension` where I needed to let the user force recognizing a type name. ([Issue #20](https://github.com/jdhitsolutions/PSTypeExtensionTools/issues/20))
6+
+ Help updates.
7+
+ Updated `README.md`.
8+
39
## v1.7.1
410

5-
+ Fixed bug in `New-PSPropertySet` that failed to correct typename case. ([Issue #19](https://github.com/jdhitsolutions/PSTypeExtensionTools/issues/19))
11+
+ Fixed bug in `New-PSPropertySet` that failed to correct typename case. ([Issue #19](https://github.com/jdhitsolutions/PSTypeExtensionTools/issues/19))
612
+ Updated online help link for `New-PSPropertySet`.
713
+ Added sample file `process.types.ps1xml`.
814
+ Updated `README.md`.

PSTypeExtensionTools.psd1

0 Bytes
Binary file not shown.

README.md

Lines changed: 22 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@ Install-Module PSTypeExtensionTools
1717
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:
1818

1919
```powershell
20-
123 | Get-PSType | Add-PSTypeExtension -MemberType ScriptProperty -MemberName SquareRoot -Value { [math]::Sqrt($this)}
20+
123 | Get-PSType | Add-PSTypeExtension -MemberType ScriptProperty -MemberName SquareRoot -Value {[math]::Sqrt($this)}
2121
```
2222

2323
Use `$this` to reference the object instead of `$_`. Now you can get the new property.
@@ -55,6 +55,26 @@ PS C:\> $x.GetPercent(110,4)
5555
34.5455
5656
```
5757

58+
### Go GUI
59+
60+
As an alternative to the command-line, you can use the native `Show-Command` cmdlet to display a graphical interface.
61+
62+
```powershell
63+
Show-Command Add-PSTypeExtension
64+
```
65+
66+
![Add via GUI](images/show-add.png)
67+
68+
Clicking Run will insert this code at your prompt.
69+
70+
```powershell
71+
Add-PSTypeExtension -MemberName ToTitleCase -MemberType ScriptMethod -TypeName System.String -Value { (Get-Culture).TextInfo.ToTitleCase($this.tolower())}
72+
```
73+
74+
If you like this extension, you can export it and re-import it later.
75+
76+
## Get Type Extensions
77+
5878
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.
5979

6080
```powershell
@@ -300,4 +320,4 @@ There is an about help topic you can read:
300320
help about_PSTypeExtensionTools
301321
```
302322

303-
Last Updated 2021-03-09 21:03:55Z
323+
Last Updated 2021-03-10 21:49:24Z

docs/Add-PSTypeExtension.md

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -50,6 +50,14 @@ PS C:\ Add-PSTypeExtension -TypeName system.io.fileinfo -MemberType AliasPropert
5050

5151
Create an alias called Size using the Length property on file objects. This expression will also create it for the deserialized version of the type so that you can use it with remoting results.
5252

53+
### EXAMPLE 4
54+
55+
```powershell
56+
PS C:\> Show-Command Add-PSTypeExtension
57+
```
58+
59+
Here is an easy way to get a GUI to create a new type extension.
60+
5361
## PARAMETERS
5462

5563
### -MemberName

docs/Export-PSTypeExtension.md

Lines changed: 4 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -16,14 +16,13 @@ Export type extensions to a file.
1616
### Object (Default)
1717

1818
```yaml
19-
Export-PSTypeExtension -Path <String> [-InputObject <Object>] [-Passthru] [Append] [-WhatIf] [-Confirm] [<CommonParameters>]
19+
Export-PSTypeExtension -Path <String> [-InputObject <Object>] [-Force] [-Passthru] [-WhatIf] [-Confirm] [<CommonParameters>]
2020
```
2121

2222
### Name
2323

2424
```yaml
25-
Export-PSTypeExtension [-TypeName] <String> -MemberName <String[]>
26-
-Path <String> [-Passthru] [Append] [-WhatIf] [-Confirm] [<CommonParameters>]
25+
Export-PSTypeExtension [-TypeName] <String> -MemberName <String[]> -Path <String> [-Force] [-Passthru] [-WhatIf] [-Confirm] [<CommonParameters>]
2726
```
2827

2928
## DESCRIPTION
@@ -191,9 +190,9 @@ Accept pipeline input: False
191190
Accept wildcard characters: False
192191
```
193192
194-
### -Append
193+
### -Force
195194
196-
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.
195+
Force the command to accept the name as a type.
197196
198197
```yaml
199198
Type: SwitchParameter
@@ -208,7 +207,6 @@ Accept wildcard characters: False
208207
```
209208
210209
### CommonParameters
211-
212210
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).
213211
214212
## INPUTS

docs/Get-PSTypeExtension.md

Lines changed: 18 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -14,12 +14,12 @@ Get selected type extensions.
1414
## SYNTAX
1515

1616
```yaml
17-
Get-PSTypeExtension [-TypeName] <String> [-Members <String[]>] [-CodeProperty] [<CommonParameters>]
17+
Get-PSTypeExtension [-TypeName] <String> [-Members <String[]>] [-CodeProperty] [-Force] [<CommonParameters>]
1818
```
1919

2020
## DESCRIPTION
2121

22-
Use this command to list defined type extensions. You can either select individual ones or all of them. Do not specify any members to retrieve all of them. This command is very similar to Get-TypeData, except that it makes it easier to see the extension value.
22+
Use this command to list defined type extensions. You can either select individual ones or all of them. Do not specify any members to retrieve all of them. This command is very similar to Get-TypeData, except that it makes it easier to see the extension value. If you are specifying a custom type name, you may need to use -Force for the command to accept it as written.
2323

2424
By default, CodeProperty members are not displayed because they can't be exported.
2525

@@ -162,6 +162,22 @@ Accept pipeline input: False
162162
Accept wildcard characters: False
163163
```
164164
165+
### -Force
166+
167+
Force the command to accept the name as a type.
168+
169+
```yaml
170+
Type: SwitchParameter
171+
Parameter Sets: (All)
172+
Aliases:
173+
174+
Required: False
175+
Position: Named
176+
Default value: None
177+
Accept pipeline input: False
178+
Accept wildcard characters: False
179+
```
180+
165181
### CommonParameters
166182
167183
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).

en-us/PSTypeExtensionTools-help.xml

Lines changed: 37 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -240,6 +240,13 @@ True</dev:code>
240240
<maml:para>Create an alias called Size using the Length property on file objects. This expression will also create it for the deserialized version of the type so that you can use it with remoting results.</maml:para>
241241
</dev:remarks>
242242
</command:example>
243+
<command:example>
244+
<maml:title>-------------------------- EXAMPLE 4 --------------------------</maml:title>
245+
<dev:code>PS C:\&gt; Show-Command Add-PSTypeExtension</dev:code>
246+
<dev:remarks>
247+
<maml:para>Here is an easy way to get a GUI to create a new type extension.</maml:para>
248+
</dev:remarks>
249+
</command:example>
243250
</command:examples>
244251
<command:relatedLinks>
245252
<maml:navigationLink>
@@ -338,9 +345,9 @@ True</dev:code>
338345
<dev:defaultValue>False</dev:defaultValue>
339346
</command:parameter>
340347
<command:parameter required="false" variableLength="true" globbing="false" pipelineInput="False" position="named" aliases="none">
341-
<maml:name>Append</maml:name>
348+
<maml:name>Force</maml:name>
342349
<maml:description>
343-
<maml:para>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.</maml:para>
350+
<maml:para>Force the command to accept the name as a type.</maml:para>
344351
</maml:description>
345352
<dev:type>
346353
<maml:name>SwitchParameter</maml:name>
@@ -409,9 +416,9 @@ True</dev:code>
409416
<dev:defaultValue>False</dev:defaultValue>
410417
</command:parameter>
411418
<command:parameter required="false" variableLength="true" globbing="false" pipelineInput="False" position="named" aliases="none">
412-
<maml:name>Append</maml:name>
419+
<maml:name>Force</maml:name>
413420
<maml:description>
414-
<maml:para>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.</maml:para>
421+
<maml:para>Force the command to accept the name as a type.</maml:para>
415422
</maml:description>
416423
<dev:type>
417424
<maml:name>SwitchParameter</maml:name>
@@ -507,9 +514,9 @@ True</dev:code>
507514
<dev:defaultValue>False</dev:defaultValue>
508515
</command:parameter>
509516
<command:parameter required="false" variableLength="true" globbing="false" pipelineInput="False" position="named" aliases="none">
510-
<maml:name>Append</maml:name>
517+
<maml:name>Force</maml:name>
511518
<maml:description>
512-
<maml:para>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.</maml:para>
519+
<maml:para>Force the command to accept the name as a type.</maml:para>
513520
</maml:description>
514521
<command:parameterValue required="false" variableLength="false">SwitchParameter</command:parameterValue>
515522
<dev:type>
@@ -748,7 +755,7 @@ Win10Ent 10/16/2020 9:30:46 AM 25.00:49:58.1206825</dev:code>
748755
</maml:description>
749756
</command:details>
750757
<maml:description>
751-
<maml:para>Use this command to list defined type extensions. You can either select individual ones or all of them. Do not specify any members to retrieve all of them. This command is very similar to Get-TypeData, except that it makes it easier to see the extension value.</maml:para>
758+
<maml:para>Use this command to list defined type extensions. You can either select individual ones or all of them. Do not specify any members to retrieve all of them. This command is very similar to Get-TypeData, except that it makes it easier to see the extension value. If you are specifying a custom type name, you may need to use -Force for the command to accept it as written.</maml:para>
752759
<maml:para>By default, CodeProperty members are not displayed because they can't be exported.</maml:para>
753760
</maml:description>
754761
<command:syntax>
@@ -789,6 +796,17 @@ Win10Ent 10/16/2020 9:30:46 AM 25.00:49:58.1206825</dev:code>
789796
</dev:type>
790797
<dev:defaultValue>False</dev:defaultValue>
791798
</command:parameter>
799+
<command:parameter required="false" variableLength="true" globbing="false" pipelineInput="False" position="named" aliases="none">
800+
<maml:name>Force</maml:name>
801+
<maml:description>
802+
<maml:para>Force the command to accept the name as a type.</maml:para>
803+
</maml:description>
804+
<dev:type>
805+
<maml:name>SwitchParameter</maml:name>
806+
<maml:uri />
807+
</dev:type>
808+
<dev:defaultValue>False</dev:defaultValue>
809+
</command:parameter>
792810
</command:syntaxItem>
793811
</command:syntax>
794812
<command:parameters>
@@ -828,6 +846,18 @@ Win10Ent 10/16/2020 9:30:46 AM 25.00:49:58.1206825</dev:code>
828846
</dev:type>
829847
<dev:defaultValue>False</dev:defaultValue>
830848
</command:parameter>
849+
<command:parameter required="false" variableLength="true" globbing="false" pipelineInput="False" position="named" aliases="none">
850+
<maml:name>Force</maml:name>
851+
<maml:description>
852+
<maml:para>Force the command to accept the name as a type.</maml:para>
853+
</maml:description>
854+
<command:parameterValue required="false" variableLength="false">SwitchParameter</command:parameterValue>
855+
<dev:type>
856+
<maml:name>SwitchParameter</maml:name>
857+
<maml:uri />
858+
</dev:type>
859+
<dev:defaultValue>False</dev:defaultValue>
860+
</command:parameter>
831861
</command:parameters>
832862
<command:inputTypes>
833863
<command:inputType>

functions/private.ps1

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,6 @@ Function _convertTypeName {
66
($Typename -as [type]).fullname
77
}
88
Catch {
9-
$Typename
9+
$typename
1010
}
11-
1211
}

functions/public.ps1

Lines changed: 41 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -11,43 +11,48 @@ Function Get-PSTypeExtension {
1111
ValueFromPipeline
1212
)]
1313
[ValidateNotNullorEmpty()]
14-
[ValidateScript( {
15-
#check if typename can be found with Get-TypeData
16-
if ((Get-TypeData).typename -contains "$_") {
17-
$True
18-
}
19-
elseif ($_ -as [type]) {
20-
#test if string resolves as a typename
21-
$True
22-
}
23-
else {
24-
Throw "$_ does not appear to be a valid type."
25-
}
26-
})]
2714
[string]$TypeName,
2815
[Parameter(
2916
HelpMessage = "Enter a comma separated list of member names",
3017
ParameterSetName = "members"
3118
)]
3219
[string[]]$Members,
3320
[Parameter(HelpMessage = "Show CodeProperty custom properties")]
34-
[switch]$CodeProperty
21+
[switch]$CodeProperty,
22+
[parameter(HelpMessage="Force the command to accept the name as a type.")]
23+
[switch]$Force
3524
)
3625

3726
Begin {
3827
Write-Verbose "Starting: $($MyInvocation.Mycommand)"
3928
$typedata = @()
40-
$TypeName=_convertTypeName $TypeName
29+
4130
} #begin
4231
Process {
32+
if ($force) {
33+
Write-Verbose "Using typename AS-IS."
34+
}
35+
else {
36+
$TypeName=_convertTypeName $TypeName
37+
}
38+
4339
Write-Verbose "Analyzing $typename"
44-
$typedata += Get-TypeData -TypeName $typename
40+
if ($TypeName) {
41+
Write-Verbose "Getting type data"
42+
$typedata += Get-TypeData -TypeName $typename
43+
}
44+
else {
45+
Write-Warning "Failed to get a typename"
46+
#bail out
47+
$typedata = $False
48+
return
49+
}
4550

4651
} #process
4752
End {
48-
$typedata = $typedata | Select-Object -Unique
4953

5054
if ($typedata) {
55+
$typedata = $typedata | Select-Object -Unique
5156
$out = [System.Collections.Generic.List[object]]::new()
5257
if (-Not $Members) {
5358
Write-Verbose "Getting all member names"
@@ -214,6 +219,9 @@ Function Export-PSTypeExtension {
214219
[Parameter(ParameterSetName = "Object", ValueFromPipeline)]
215220
[object]$InputObject,
216221

222+
[parameter(HelpMessage="Force the command to accept the name as a type.")]
223+
[switch]$Force,
224+
217225
[switch]$Passthru
218226
)
219227
DynamicParam {
@@ -267,17 +275,28 @@ Function Export-PSTypeExtension {
267275

268276
if ($TypeName) {
269277
#convert typename to a properly cased name
270-
Write-Verbose "Converting $typename to properly cased type name."
271-
$TypeName=_convertTypeName $TypeName
272-
}
278+
if ($force) {
279+
Write-Verbose "Using typename $typename AS-IS."
280+
}
281+
else {
282+
Write-Verbose "Converting $typename to properly cased type name."
283+
$TypeName=_convertTypeName $TypeName
284+
}
285+
}
273286
}
274287
Process {
275288
#test if parent path exists
276289
If ($validPath) {
277290
if ($Inputobject) {
278291
Write-Verbose "Processing input type: $($InputObject.TypeName)"
279-
#convert the type name just in case
280-
$typeName = _convertTypeName $InputObject.TypeName
292+
if ($force) {
293+
Write-Verbose "Using typename AS-IS."
294+
$typename = $InputObject.typename
295+
}
296+
else {
297+
Write-Verbose "Converting $typename to properly cased type name."
298+
$TypeName=_convertTypeName $TypeName
299+
}
281300
$data.Add($InputObject)
282301
}
283302
else {

images/show-add.png

33.2 KB
Loading

0 commit comments

Comments
 (0)