-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path03_CustomObjects.ps1
More file actions
91 lines (71 loc) · 2.78 KB
/
03_CustomObjects.ps1
File metadata and controls
91 lines (71 loc) · 2.78 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
# You can create custom objects with properties and methods that you define
# you notice the difference to an hashtable when you want to use format-table or export-csv for example
# Creating a PSCustomObject
# ----------------------------------------------------------------------------------------------------
$myBeer = [PSCustomObject]@{
Name = 'Thun Bier'
Type = 'Pale Ale'
Manufacturer = 'Brauerei Thun AG'
}
$myBeer
$myFirstCustomObject = [PSCustomObject]@{OSBuild = 'x'; OSVersion = 'y'}
# legacy approach to create custom objects with the New-Object Command:
$myFirstCustomObject = New-Object -TypeName PSCustomObject
# Converting a hashtable to a PSCustomObject
# ----------------------------------------------------------------------------------------------------
# first we create a hashtable
$myBeerHashtable = @{
Name = 'Thun Bier'
Type = 'Amber Ale'
Manufacturer = 'Brauerei Thun AG'
}
$myBeerHashtable.GetType()
$myBeerHashtable
# now we convert the hashtable to a pscustomobject
$myBeer2 = [pscustomobject]$myBeerHashtable
$myBeer2.gettype()
$myBeer2
# Adding properties
# ----------------------------------------------------------------------------------------------------
$myBeer2 | Add-Member -MemberType NoteProperty -Name 'Price' -Value 3.9
# show the added property with the value:
$myBeer2.Price
$myBeer2
# Reading / accessing properties
# ----------------------------------------------------------------------------------------------------
# show the added property with the value:
$myBeer2.Price
$myBeer2.Manufacturer
# you can also use a string:
$myBeer2.'Manufacturer'
# or you can use a variable fo the property name:
$property = 'Manufacturer'
$myBeer2.$property
Get-Member -InputObject $myBeer2
# Removing properties
# ----------------------------------------------------------------------------------------------------
$myBeer2.psobject.properties.remove('Price')
$myBeer2
# Adding object methods
# ----------------------------------------------------------------------------------------------------
# to add a method directly during creation of the PSCustomObject:
$myBeer = [PSCustomObject]@{
Name = 'Thun Bier'
Type = 'Pale Ale'
Manufacturer = 'Brauerei Thun AG'
'Manufacturing date' = ([ScriptBlock]{
Get-Date
}).InvokeReturnAsIs()
}
$myBeer
# It is also possible to create the ScriptBlock outside of the PSCustomObject like in this example:
$ScriptBlockExample = ([scriptblock]{
Get-Date
}).InvokeReturnAsIs()
$myBeer2 = [PSCustomObject]@{
Name = 'Thun Bier'
Type = 'IPA'
Manufacturer = 'Brauerei Thun AG'
'Manufacturing date' = $ScriptBlockExample
}
$myBeer2