Skip to content

Commit 77e9ab1

Browse files
a few helper functions
1 parent 568da93 commit 77e9ab1

File tree

2 files changed

+235
-0
lines changed

2 files changed

+235
-0
lines changed

Add-ObjectDetail.ps1

Lines changed: 170 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,170 @@
1+
function Add-ObjectDetail
2+
{
3+
<#
4+
.SYNOPSIS
5+
Decorate an object with
6+
- A TypeName
7+
- New properties
8+
- Default parameters
9+
10+
.DESCRIPTION
11+
Helper function to decorate an object with
12+
- A TypeName
13+
- New properties
14+
- Default parameters
15+
16+
.PARAMETER InputObject
17+
Object to decorate. Accepts pipeline input.
18+
19+
.PARAMETER TypeName
20+
Typename to insert.
21+
22+
This will show up when you use Get-Member against the resulting object.
23+
24+
.PARAMETER PropertyToAdd
25+
Add these noteproperties.
26+
27+
Format is a hashtable with Key (Property Name) = Value (Property Value).
28+
29+
Example to add a One and Date property:
30+
31+
-PropertyToAdd @{
32+
One = 1
33+
Date = (Get-Date)
34+
}
35+
36+
.PARAMETER DefaultProperties
37+
Change the default properties that show up
38+
39+
.PARAMETER Passthru
40+
Whether to pass the resulting object on. Defaults to true
41+
42+
.EXAMPLE
43+
#
44+
# Create an object to work with
45+
$Object = [PSCustomObject]@{
46+
First = 'Cookie'
47+
Last = 'Monster'
48+
Account = 'CMonster'
49+
}
50+
51+
#Add a type name and a random property
52+
Add-ObjectDetail -InputObject $Object -TypeName 'ApplicationX.Account' -PropertyToAdd @{ AnotherProperty = 5 }
53+
54+
# First Last Account AnotherProperty
55+
# ----- ---- ------- ---------------
56+
# Cookie Monster CMonster 5
57+
58+
#Verify that get-member shows us the right type
59+
$Object | Get-Member
60+
61+
# TypeName: ApplicationX.Account ...
62+
63+
.EXAMPLE
64+
#
65+
# Create an object to work with
66+
$Object = [PSCustomObject]@{
67+
First = 'Cookie'
68+
Last = 'Monster'
69+
Account = 'CMonster'
70+
}
71+
72+
#Add a random property, set a default property set so we only see two props by default
73+
Add-ObjectDetail -InputObject $Object -PropertyToAdd @{ AnotherProperty = 5 } -DefaultProperties Account, AnotherProperty
74+
75+
# Account AnotherProperty
76+
# ------- ---------------
77+
# CMonster 5
78+
79+
#Verify that the other properties are around
80+
$Object | Select -Property *
81+
82+
# First Last Account AnotherProperty
83+
# ----- ---- ------- ---------------
84+
# Cookie Monster CMonster 5
85+
86+
.NOTES
87+
This breaks the 'do one thing' rule from certain perspectives...
88+
The goal is to decorate an object all in one shot
89+
90+
This abstraction simplifies decorating an object, with a slight trade-off in performance. For example:
91+
92+
10,000 objects, add a property and typename:
93+
Add-ObjectDetail: ~4.6 seconds
94+
Add-Member + PSObject.TypeNames.Insert: ~3 seconds
95+
96+
Initial code borrowed from Shay Levy:
97+
http://blogs.microsoft.co.il/scriptfanatic/2012/04/13/custom-objects-default-display-in-powershell-30/
98+
99+
.LINK
100+
http://ramblingcookiemonster.github.io/Decorating-Objects/
101+
102+
.FUNCTIONALITY
103+
PowerShell Language
104+
#>
105+
[CmdletBinding()]
106+
param(
107+
[Parameter( Mandatory = $true,
108+
Position=0,
109+
ValueFromPipeline=$true )]
110+
[ValidateNotNullOrEmpty()]
111+
[psobject[]]$InputObject,
112+
113+
[Parameter( Mandatory = $false,
114+
Position=1)]
115+
[string]$TypeName,
116+
117+
[Parameter( Mandatory = $false,
118+
Position=2)]
119+
[System.Collections.Hashtable]$PropertyToAdd,
120+
121+
[Parameter( Mandatory = $false,
122+
Position=3)]
123+
[ValidateNotNullOrEmpty()]
124+
[Alias('dp')]
125+
[System.String[]]$DefaultProperties,
126+
127+
[boolean]$Passthru = $True
128+
)
129+
130+
Begin
131+
{
132+
if($PSBoundParameters.ContainsKey('DefaultProperties'))
133+
{
134+
# define a subset of properties
135+
$ddps = New-Object System.Management.Automation.PSPropertySet DefaultDisplayPropertySet,$DefaultProperties
136+
$PSStandardMembers = [System.Management.Automation.PSMemberInfo[]]$ddps
137+
}
138+
}
139+
Process
140+
{
141+
foreach($Object in $InputObject)
142+
{
143+
switch ($PSBoundParameters.Keys)
144+
{
145+
'PropertyToAdd'
146+
{
147+
foreach($Key in $PropertyToAdd.Keys)
148+
{
149+
#Add some noteproperties. Slightly faster than Add-Member.
150+
$Object.PSObject.Properties.Add( ( New-Object System.Management.Automation.PSNoteProperty($Key, $PropertyToAdd[$Key]) ) )
151+
}
152+
}
153+
'TypeName'
154+
{
155+
#Add specified type
156+
[void]$Object.PSObject.TypeNames.Insert(0,$TypeName)
157+
}
158+
'DefaultProperties'
159+
{
160+
# Attach default display property set
161+
Add-Member -InputObject $Object -MemberType MemberSet -Name PSStandardMembers -Value $PSStandardMembers
162+
}
163+
}
164+
if($Passthru)
165+
{
166+
$Object
167+
}
168+
}
169+
}
170+
}

Join-Parts.ps1

Lines changed: 65 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,65 @@
1+
function Join-Parts
2+
{
3+
<#
4+
.SYNOPSIS
5+
Join strings with a specified separator.
6+
7+
.DESCRIPTION
8+
Join strings with a specified separator.
9+
10+
This strips out null values and any duplicate separator characters.
11+
12+
See examples for clarification.
13+
14+
.PARAMETER Separator
15+
Separator to join with
16+
17+
.PARAMETER Parts
18+
Strings to join
19+
20+
.EXAMPLE
21+
Join-Parts -Separator "/" this //should $Null /work/ /well
22+
23+
# Output: this/should/work/well
24+
25+
.EXAMPLE
26+
Join-Parts -Parts http://this.com, should, /work/, /wel
27+
28+
# Output: http://this.com/should/work/wel
29+
30+
.EXAMPLE
31+
Join-Parts -Separator "?" this ?should work ???well
32+
33+
# Output: this?should?work?well
34+
35+
.EXAMPLE
36+
37+
$CouldBeOneOrMore = @( "JustOne" )
38+
Join-Parts -Separator ? -Parts CouldBeOneOrMore
39+
40+
# Output JustOne
41+
42+
# If you have an arbitrary count of parts coming in,
43+
# Unnecessary separators will not be added
44+
45+
.NOTES
46+
Credit to Rob C. and Michael S. from this post:
47+
http://stackoverflow.com/questions/9593535/best-way-to-join-parts-with-a-separator-in-powershell
48+
49+
#>
50+
[cmdletbinding()]
51+
param
52+
(
53+
[string]$Separator = "/",
54+
55+
[parameter(ValueFromRemainingArguments=$true)]
56+
[string[]]$Parts = $null
57+
58+
)
59+
60+
( $Parts |
61+
Where { $_ } |
62+
Foreach { ( [string]$_ ).trim($Separator) } |
63+
Where { $_ }
64+
) -join $Separator
65+
}

0 commit comments

Comments
 (0)