|
1 |
| -# Piping in PowerShell |
| 1 | +# Azure PowerShell Piping Scenarios |
| 2 | + |
| 3 | +There are two main scenarios that we wish to enable in cmdlets for Azure PowerShell: |
| 4 | +- piping by value using an `InputObject` parameter |
| 5 | +- piping by property name using a `ResourceId` parameter |
| 6 | + |
| 7 | +## Using the `InputObject` parameter |
| 8 | + |
| 9 | +### Short explanation |
| 10 | +For all resources, `InputObject` should be implemented for the Remove/Set/Update cmdlets (and any other cmdlet where an existing resource is being operated on). The implementation of this will be a new parameter set: |
| 11 | + |
| 12 | +```powershell |
| 13 | +Remove/Set/Update-AzureRm<ExampleResource> -InputObject <TypeOfExampleResource> <Other parameters that cannot be obtained from TypeOfExampleResource> |
| 14 | +``` |
| 15 | + |
| 16 | +For all child resources, `InputObject` should also be implemented for the Get/New cmdlets. The implementation of this will be a new parameter set: |
| 17 | + |
| 18 | +```powershell |
| 19 | +Get/New-AzureRm<ExampleResource> -ParentObject <TypeOfParentOfExampleResource> <Other parameters that cannot be obtained from TypeOfParentOfExampleResource> |
| 20 | +``` |
| 21 | + |
| 22 | +### Long explanation |
| 23 | +This scenario should be used when piping objects around within the same module. For example, if you have a set of `Get-AzureRmFoo`, `Remove-AzureRmFoo`, and `Set-AzureRmFoo` cmdlets, the `Remove-AzureRmFoo` and `Set-AzureRmFoo` cmdlets should have a parameter set that takes the `InputObject` parameter of type `PSFoo` so that a user can do the following: |
| 24 | + |
| 25 | +```powershell |
| 26 | +# --- Piping scenario --- |
| 27 | +# Setting and removing an individual object |
| 28 | +Get-AzureRmFoo -Name "FooName" -ResourceGroupName "RG" | Set-AzureRmFoo <additional parameters> |
| 29 | +Get-AzureRmFoo -Name "FooName" -ResourceGroupName "RG" | Remove-AzureRmFoo |
| 30 | +
|
| 31 | +# Setting and removing a collection of objects |
| 32 | +Get-AzureRmFoo | Set-AzureRmFoo <additional parameters> |
| 33 | +Get-AzureRmFoo | Remove-AzureRmFoo |
| 34 | +
|
| 35 | +
|
| 36 | +# --- Non-piping scenario --- |
| 37 | +# Setting and removing an individual object |
| 38 | +$foo = Get-AzureRmFoo -Name "FooName" -ResourceGroupName "RG" |
| 39 | +Set-AzureRmFoo -InputObject $foo <additional parameters> |
| 40 | +Remove-AzureRmFoo -InputObject $foo |
| 41 | +
|
| 42 | +# Setting and removing a collection of objects |
| 43 | +Get-AzureRmFoo | ForEach-Object { Set-AzureRmFoo -InputObject $_ <additional parameters> } |
| 44 | +Get-AzureRmFoo | ForEach-Object { Remove-AzureRmFoo -InputObject $_ } |
| 45 | +``` |
| 46 | + |
| 47 | +Another time that this scenario applies is when you have cmdlets for child resources that need information about the parent (top-level) resource. For example you can pipe in the whole parent object to the `New-AzureRmFooBar` and `Get-AzureRmFooBar` cmdlets to get the child resources, and then pipe the child resource object to the `Remove-AzureRmFooBar` and `Set-AzureRmFooBar` cmdlets. |
| 48 | + |
| 49 | +```powershell |
| 50 | +# --- Piping scenario --- |
| 51 | +# Getting all of child resources from all of the parent resources and removing them |
| 52 | +Get-AzureRmFoo | Get-AzureRmFooBar | Remove-AzureRmFooBar |
| 53 | +
|
| 54 | +# Getting all of the child resources from all of the parent resources in a resource group and removing them |
| 55 | +Get-AzureRmFoo -ResourceGroupName "RG" | Get-AzureRmFooBar | Remove-AzureRmFooBar |
| 56 | +
|
| 57 | +# Getting all of the child resources from a specific parent resource and removing them |
| 58 | +Get-AzureRmFoo -ResourceGroupName "RG" -Name "FooName" | Get-AzureRmFooBar | Remove-AzureRmFooBar |
| 59 | +
|
| 60 | +
|
| 61 | +# --- Non-piping scenario --- |
| 62 | +# Getting all of the child resources from a specific parent resource and removing them |
| 63 | +$foo = Get-AzureRmFoo -ResourceGroupName "RG" -Name "FooName" |
| 64 | +$fooBar = Get-AzureRmFooBar -InputObject $foo |
| 65 | +Remove-AzureRmFooBar -InputObject $fooBar |
| 66 | +``` |
| 67 | + |
| 68 | +## Using the `ResourceId` parameter |
| 69 | + |
| 70 | +### Short explanation |
| 71 | +For all resources, `ResourceId` should be implemented for the Remove/Set/Update cmdlets (and any other cmdlet where an existing resource is being operated on). The implementation of this will be a new parameter set: |
| 72 | + |
| 73 | +```powershell |
| 74 | +Remove/Set/Update-AzureRm<ExampleResource> -ResourceId <string (accepts ExampleResource ResourceId> <Other parameters that cannot be obtained from ExampleResource ResourceId> |
| 75 | +``` |
| 76 | + |
| 77 | +For all child resources, `ResourceId` should also be implemented for the Get/New cmdlets. The implementation of this will be a new parameter set: |
| 78 | + |
| 79 | +```powershell |
| 80 | +Get/New-AzureRm<ExampleResource> -ParentResourceId <string (accepts ResourceId of the parent of ExampleResource> <Other parameters that cannot be obtained from the ResourceId of the parent of ExampleResource> |
| 81 | +``` |
| 82 | + |
| 83 | +### Long explanation |
| 84 | + |
| 85 | +In this scenario, we are using the generic cmdlets found in the `AzureRM.Resources` module. These cmdlets, `Find-AzureRmResource` and `Get-AzureRmResource`, return a `PSCustomObject` that has a `ResourceId` property, which is the unique identifier for the given resource. Since this identifier can parsed to get the name and resource group name for a top-level resource, we can create a parameter set that has a `ResourceId` parameter that accepts its value from the pipeline by property name, allowing us to accept piping from these generic cmdlets. |
| 86 | + |
| 87 | +```powershell |
| 88 | +# --- Piping scenario --- |
| 89 | +# Remove all Foo objects in the current subscription |
| 90 | +Find-AzureRmResource -ResourceType Microsoft.Foo/foo | Remove-AzureRmFoo |
| 91 | +
|
| 92 | +# Remove all Foo objects in a given resource group |
| 93 | +Find-AzureRmResource -ResourceType Microsoft.Foo/foo -ResourceGroupEquals "RG" | Remove-AzureRmFoo |
| 94 | +
|
| 95 | +# Remove a specific Foo object |
| 96 | +Find-AzureRmResource -ResourceGroupEquals "RG" -ResourceNameEquals "FooName" | Remove-AzureRmFoo |
| 97 | +
|
| 98 | +
|
| 99 | +# -- Non-piping scenario --- |
| 100 | +# Removing all Foo objects in the current subscription |
| 101 | +Find-AzureRmResource -ResourceType Microsoft.Foo/foo | ForEach-Object { Remove-AzureRmFoo -ResourceId $_.ResourceId } |
| 102 | +
|
| 103 | +# Remove all Foo objects in a given resource group |
| 104 | +Find-AzureRmResource -ResourceType Microsoft.Foo/foo -ResourceGroupEquals "RG" | ForEach-Object { Remove-AzureRmFoo -ResourceId $_.ResourceId } |
| 105 | +
|
| 106 | +# Remove a specific Foo object |
| 107 | +Find-AzureRmResource -ResourceGroupEquals "RG" -ResourceNameEquals "FooName" | ForEach-Object { Remove-AzureRmFoo -ResourceId $_.ResourceId } |
| 108 | +``` |
| 109 | + |
| 110 | +To implement this scenario, please see the [`ResourceIdentifier`](https://github.com/Azure/azure-powershell/blob/preview/src/ResourceManager/Common/Commands.ResourceManager.Common/Utilities/Models/ResourceIdentifier.cs) class in the `Commands.ResourceManager.Common` project. This class will allow you to create a `ResourceIdentifier` object that accepts a `ResourceId` string in its constructor and has properties `ResourceName`, `ResourceGroupName`, and others. |
| 111 | + |
| 112 | +## Summary |
| 113 | +For all Remove/Set/Update cmdlets (and any other cmdlet where an existing resource is being operated on), you will have three parameter sets (and potentially a multiple of three if you have initially have multiple parameter sets): |
| 114 | +```powershell |
| 115 | +Remove/Set/Update-AzureRm<ExampleResource> <parameters that are required to identify ExampleResource> <Other parameters> |
| 116 | +Remove/Set/Update-AzureRm<ExampleResource> -InputObject <TypeOfExampleResource> <Other parameters that cannot be obtained from TypeOfExampleResource> |
| 117 | +Remove/Set/Update-AzureRm<ExampleResource> -ResourceId <string (accepts ExampleResource ResourceId> <Other parameters that cannot be obtained from ExampleResource ResourceId> |
| 118 | +``` |
| 119 | +For example, for child resource "Widget" with parent "Foo", there will be these three parameter sets for Remove: |
| 120 | +```powershell |
| 121 | +Remove-AzureRmWidget -ResourceGroupName <string> -FooName <string> -Name <string> |
| 122 | +Remove-AzureRmWidget -InputObject <Widget> |
| 123 | +Remove-AzureRmWidget -ResourceId <string> |
| 124 | +``` |
| 125 | +For all child resources, Get/New cmdlets will also have these three parameter sets: |
| 126 | +```powershell |
| 127 | +Get/New-AzureRm<ExampleResource> <parameters needed to identify/create ExampleResource> |
| 128 | +Get-AzureRm<ExampleResource> -InputObject <TypeOfParentOfExampleResource> <Other parameters that cannot be obtained from TypeOfParentOfExampleResource> |
| 129 | +Get/New-AzureRm<ExampleResource> -ResourceId <string (accepts ResourceId of the parent of ExampleResource> <Other parameters that cannot be obtained from the ResourceId of the parent of ExampleResource> |
| 130 | +``` |
| 131 | +For example, for child resource "Widget" with parent "Foo", there will be these three parameter sets for New: |
| 132 | +```powershell |
| 133 | +New-AzureRmWidget -ResourceGroupName <string> -FooName <String> -Name <string> |
| 134 | +New-AzureRmWidget -WidgetObject <Foo> -Name <string> |
| 135 | +New-AzureRmWidget -WidgetResourceId <string> -Name <string> |
| 136 | +``` |
| 137 | + |
| 138 | +# Piping in PowerShell (additional information) |
2 | 139 |
|
3 | 140 | ## Understanding Piping
|
4 | 141 |
|
@@ -107,88 +244,6 @@ public override void ExecuteCmdlet()
|
107 | 244 | }
|
108 | 245 | ```
|
109 | 246 |
|
110 |
| -## Azure PowerShell Piping Scenarios |
111 |
| - |
112 |
| -There are two main scenarios that we wish to enable in cmdlets for Azure PowerShell: |
113 |
| -- piping by value using an `InputObject` parameter |
114 |
| -- piping by property name using a `ResourceId` parameter |
115 |
| - |
116 |
| -### Using the `InputObject` parameter |
117 |
| - |
118 |
| -This scenario should be used when piping objects around within the same module. For example, if you have a set of `Get-AzureRmFoo`, `Remove-AzureRmFoo`, and `Set-AzureRmFoo` cmdlets, the `Remove-AzureRmFoo` and `Set-AzureRmFoo` cmdlets should have a parameter set that takes the `InputObject` parameter of type `PSFoo` so that a user can do the following: |
119 |
| - |
120 |
| -```powershell |
121 |
| -# --- Piping scenario --- |
122 |
| -# Setting and removing an individual object |
123 |
| -Get-AzureRmFoo -Name "FooName" -ResourceGroupName "RG" | Set-AzureRmFoo <additional parameters> |
124 |
| -Get-AzureRmFoo -Name "FooName" -ResourceGroupName "RG" | Remove-AzureRmFoo |
125 |
| -
|
126 |
| -# Setting and removing a collection of objects |
127 |
| -Get-AzureRmFoo | Set-AzureRmFoo <additional parameters> |
128 |
| -Get-AzureRmFoo | Remove-AzureRmFoo |
129 |
| -
|
130 |
| -
|
131 |
| -# --- Non-piping scenario --- |
132 |
| -# Setting and removing an individual object |
133 |
| -$foo = Get-AzureRmFoo -Name "FooName" -ResourceGroupName "RG" |
134 |
| -Set-AzureRmFoo -InputObject $foo <additional parameters> |
135 |
| -Remove-AzureRmFoo -InputObject $foo |
136 |
| -
|
137 |
| -# Setting and removing a collection of objects |
138 |
| -Get-AzureRmFoo | ForEach-Object { Set-AzureRmFoo -InputObject $_ <additional parameters> } |
139 |
| -Get-AzureRmFoo | ForEach-Object { Remove-AzureRmFoo -InputObject $_ } |
140 |
| -``` |
141 |
| - |
142 |
| -Another time that this scenario applies is when you have cmdlets for child resources that need information about the parent (top-level) resource. For example you can pipe in the whole parent object to the `New-AzureRmFooBar` and `Get-AzureRmFooBar` cmdlets to get the child resources, and then pipe the child resource object to the `Remove-AzureRmFooBar` and `Set-AzureRmFooBar` cmdlets. |
143 |
| - |
144 |
| -```powershell |
145 |
| -# --- Piping scenario --- |
146 |
| -# Getting all of child resources from all of the parent resources and removing them |
147 |
| -Get-AzureRmFoo | Get-AzureRmFooBar | Remove-AzureRmFooBar |
148 |
| -
|
149 |
| -# Getting all of the child resources from all of the parent resources in a resource group and removing them |
150 |
| -Get-AzureRmFoo -ResourceGroupName "RG" | Get-AzureRmFooBar | Remove-AzureRmFooBar |
151 |
| -
|
152 |
| -# Getting all of the child resources from a specific parent resource and removing them |
153 |
| -Get-AzureRmFoo -ResourceGroupName "RG" -Name "FooName" | Get-AzureRmFooBar | Remove-AzureRmFooBar |
154 |
| -
|
155 |
| -
|
156 |
| -# --- Non-piping scenario --- |
157 |
| -# Getting all of the child resources from a specific parent resource and removing them |
158 |
| -$foo = Get-AzureRmFoo -ResourceGroupName "RG" -Name "FooName" |
159 |
| -$fooBar = Get-AzureRmFooBar -InputObject $foo |
160 |
| -Remove-AzureRmFooBar -InputObject $fooBar |
161 |
| -``` |
162 |
| - |
163 |
| -### Using the `ResourceId` parameter |
164 |
| - |
165 |
| -In this scenario, we are using the generic cmdlets found in the `AzureRM.Resources` module. These cmdlets, `Find-AzureRmResource` and `Get-AzureRmResource`, return a `PSCustomObject` that has a `ResourceId` property, which is the unique identifier for the given resource. Since this identifier can parsed to get the name and resource group name for a top-level resource, we can create a parameter set that has a `ResourceId` parameter that accepts its value from the pipeline by property name, allowing us to accept piping from these generic cmdlets. |
166 |
| - |
167 |
| -```powershell |
168 |
| -# --- Piping scenario --- |
169 |
| -# Remove all Foo objects in the current subscription |
170 |
| -Find-AzureRmResource -ResourceType Microsoft.Foo/foo | Remove-AzureRmFoo |
171 |
| -
|
172 |
| -# Remove all Foo objects in a given resource group |
173 |
| -Find-AzureRmResource -ResourceType Microsoft.Foo/foo -ResourceGroupEquals "RG" | Remove-AzureRmFoo |
174 |
| -
|
175 |
| -# Remove a specific Foo object |
176 |
| -Find-AzureRmResource -ResourceGroupEquals "RG" -ResourceNameEquals "FooName" | Remove-AzureRmFoo |
177 |
| -
|
178 |
| -
|
179 |
| -# -- Non-piping scenario --- |
180 |
| -# Removing all Foo objects in the current subscription |
181 |
| -Find-AzureRmResource -ResourceType Microsoft.Foo/foo | ForEach-Object { Remove-AzureRmFoo -ResourceId $_.ResourceId } |
182 |
| -
|
183 |
| -# Remove all Foo objects in a given resource group |
184 |
| -Find-AzureRmResource -ResourceType Microsoft.Foo/foo -ResourceGroupEquals "RG" | ForEach-Object { Remove-AzureRmFoo -ResourceId $_.ResourceId } |
185 |
| -
|
186 |
| -# Remove a specific Foo object |
187 |
| -Find-AzureRmResource -ResourceGroupEquals "RG" -ResourceNameEquals "FooName" | ForEach-Object { Remove-AzureRmFoo -ResourceId $_.ResourceId } |
188 |
| -``` |
189 |
| - |
190 |
| -To implement this scenario, please see the [`ResourceIdentifier`](https://github.com/Azure/azure-powershell/blob/preview/src/ResourceManager/Common/Commands.ResourceManager.Common/Utilities/Models/ResourceIdentifier.cs) class in the `Commands.ResourceManager.Common` project. This class will allow you to create a `ResourceIdentifier` object that accepts a `ResourceId` string in its constructor and has properties `ResourceName`, `ResourceGroupName`, and others. |
191 |
| - |
192 | 247 | ## More Information
|
193 | 248 |
|
194 |
| -For more information on piping, see the article ["Understanding the Windows PowerShell Pipeline"](https://msdn.microsoft.com/en-us/powershell/scripting/getting-started/fundamental/understanding-the-windows-powershell-pipeline). |
| 249 | +For more information on piping, see the article ["Understanding the Windows PowerShell Pipeline"](https://msdn.microsoft.com/en-us/powershell/scripting/getting-started/fundamental/understanding-the-windows-powershell-pipeline). |
0 commit comments