-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathpipeline.ps1.txt
executable file
·197 lines (110 loc) · 5.59 KB
/
pipeline.ps1.txt
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
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
<#
Helpful notes from the presentation
"Understanding the Pipeline - Get your one-liners to work!"
"It will always work -- if you know how"
Jason Helmick
2009
###############################################################
# First Problem - ByValue
Get-Service | Stop-Service
Sending | Receiving
1. What kind of object is sent across the pipeline
To Determine: Get-Service | gm
Answer: ServiceController
2. Does the receiving cmdlet have any parameters that accept
pipeline input BYVALUE
To Determine: Get-Help Stop-Service -Full
Answer: Yes
-Name <String>
-InputObject <ServiceController>
Solution: Because THe passed object is same type as -InputObject
(ServiceController) The two cmdlets work together.
####################################################################
# Second Problem - ByPropertyName
Get-Service | Stop-Process
Sending | Receiving
1. What kind of object is sent across the pipeline?
To Determine: Get-Service | gm
Answer: ServiceController
2. Does the receiving cmdlet have any parameters that accept
pipeline input BYVALUE?
To Determine: Get-Help Stop-Process -Full
Answer: Yes
-InputObject <Process>
*Note: Because the Passed object has a different type
(ServiceController) than -InputObject (process) There is
no ByValue connection
______________________________________________________
3. What parameters of Stop-Process accept pipeline input ByPropertyName?
To Determine: Get-Help Stop-Process -Full
Answer: Yes
-Name (Process name)
-ID (Process ID)
4. Is there a property of the sending cmdlet that matches the parameter
name of the receiving cmdlet?
To Determine: Get-Service | gm
Answer: Yes
Name
Solution: Because THe passed object has a property with the same name (Name)
as the parameter (-Name) on the receiving cmdlet - this works.
####################################################################
# Third Problem - ByPropertyName
Get-ADComputer -Filter * | Get-Service -name bits
Sending | Receiving
1. What kind of object is sent across the pipeline?
To Determine: Get-ADComputer -Filter * | gm
Answer: ADComputer
2. Does the receiving cmdlet have any parameters that accept
pipeline input BYVALUE?
To Determine: Get-Help Get-Service -Full
Answer: Yes
-InputObject <ServiceController>
Note: Because the Passed object has a different type
(ADComputer) than -InputObject (ServiceController) There is
no ByValue connection
______________________________________________________
3. What parameters of Get-Service accept pipeline input ByPropertyName?
To Determine: Get-Help Get-Service -Full
Answer: Yes
-Name (Service name)
-ComputerName (ComputerName)
4. Is there a property of the sending cmdlet that matches the parameter
name of the receiving cmdlet?
To Determine: Get-ADComputer -Filter * | gm
Answer: Yes - But its the wrong one we want - it matches to -Name.
Name (Computer names)
Note: Because the Passed object has a different property name (Name)
than the desired parameter on the receiving cmdlet (-ComputerName)
this doesn't work as we expect.
Solution: To match the property (Name) to the parameter (-ComputerNAme) create
your own custom property in-flight using the Select-Object cmdlet.
Get-Adcomputer -filter * | Select-Object -Property @{n='ComputerName';e={$_.name}} |
Get-Service -name bits
####################################################################
# Fourth Problem - The parenthetical
Get-Adcomputer -filter * | Get-WmiObject -Class win32_bios
Sending | Receiving
1. What kind of object is sent accross the pipeline?
To Determine: Get-ADComputer -Filter * | gm
Answer: ADComputer
2. Does the receiving cmldet have any parameters that accept
pipeline input BYVALUE or ByPropertyName
To Determine: Get-Help Get-WmiObject -Full
Answer: NO!
Note: This cmdlet does not except ANY pipeline input, so
a new tactic to solving the problem must be used.
Solution:The cmdlet Get-WmiObject supports -ComputerName with an argument of
object type <String[]>. You need to fill this argument with the data, but it must
be of type <String>.
Example:
1. Get-Adcomputer -filter * | Select-Object -Property name | gm
# Not type string - won't work
2. Get-Adcomputer -filter * | Select-Object -ExpandProperty name | gm
# -ExpandProperty - This makes the type <string> - This will work.
Get-WmiObject -Class win32_bios -ComputerName (Get-Adcomputer -filter * | Select-Object -ExpandProperty name)
###################################################################################
Resources:
For more help on working with the pipeline, see the book
"Learn Windows PowerShell in a Month of Lunches" - chapter 9
Or go to http://www.PowerShell.Org and post a question in the forums.
#>