Skip to content

Commit 7e4c963

Browse files
sdwheelerJos Koelewijn
andauthored
Add more information about 'using' scope modifier (#5596)
* add more information about 'using' scope modifier * editorial feedback * feedback edits * more feedback edits * final feedback Co-authored-by: Jos Koelewijn <[email protected]>
1 parent 2a51a35 commit 7e4c963

File tree

8 files changed

+374
-33
lines changed

8 files changed

+374
-33
lines changed

reference/5.1/Microsoft.PowerShell.Core/About/about_Remote_Variables.md

Lines changed: 42 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
---
22
keywords: powershell,cmdlet
33
locale: en-us
4-
ms.date: 07/23/2019
4+
ms.date: 03/13/2020
55
online version: https://docs.microsoft.com/powershell/module/microsoft.powershell.core/about/about_remote_variables?view=powershell-5.1&WT.mc_id=ps-gethelp
66
schema: 2.0.0
77
title: about_Remote_Variables
@@ -105,14 +105,48 @@ $Splat = @{ Name = "Win*"; Include = "WinRM" }
105105
Invoke-Command -Session $s -ScriptBlock { Get-Service @Using:Splat }
106106
```
107107

108-
## Using local variables in PowerShell 2.0
108+
### Other situations where the 'Using' scope modifier is needed
109+
110+
For any script or command that executes out of session, you need the `Using`
111+
scope modifier to embed variable values from the calling session scope, so that
112+
out of session code can access them. The `Using` scope modifier is supported in
113+
the following contexts:
114+
115+
- Remotely executed commands, started with `Invoke-Command` using the
116+
**ComputerName** or **Session** parameter (remote session)
117+
- Background jobs, started with `Start-Job` (out-of-process session)
118+
- Thread jobs started via `Start-ThreadJob` (separate thread session)
119+
120+
Depending on the context, embedded variable values are either independent
121+
copies of the data in the caller's scope or references to it. In remote and
122+
out-of-process sessions, they are always independent copies. In thread
123+
sessions, they are passed by reference.
124+
125+
## Serialization of variable values
126+
127+
Remotely executed commands and background jobs run out-of-process.
128+
Out-of-process sessions use XML-based serialization and deserialization to make
129+
the values of variables available across the process boundaries. The
130+
serialization process converts objects to a **PSObject** that contains the
131+
original objects properties but not its methods.
132+
133+
For a limited set of types, deserialization rehydrates objects back to the
134+
original type. The rehydrated object is a copy of the original object instance.
135+
It has the type properties and methods. For simple types, such as
136+
**System.Version**, the copy is exact. For complex types, the copy is
137+
imperfect. For example, rehydrated certificate objects do not include the
138+
private key.
139+
140+
Instances of all other types are **PSObject** instances. The **PSTypeNames**
141+
property contains the original type name prefixed with **Deserialized**, for
142+
example, **Deserialized.System.Data.DataTable**
143+
144+
## Using local variables with **ArgumentList** parameter
109145

110146
You can use local variables in a remote command by defining parameters for the
111147
remote command and using the **ArgumentList** parameter of the `Invoke-Command`
112148
cmdlet to specify the local variable as the parameter value.
113149

114-
The following command format is valid on PowerShell 2.0 and later versions:
115-
116150
- Use the `param` keyword to define parameters for the remote command. The
117151
parameter names are placeholders that don't need to match the local
118152
variable's name.
@@ -144,8 +178,12 @@ Invoke-Command -ComputerName S1 -ScriptBlock {
144178

145179
[about_Splatting](about_Splatting.md)
146180

181+
[about_Variables](about_Variables.md)
182+
147183
[Enter-PSSession](../Enter-PSSession.md)
148184

149185
[Invoke-Command](../Invoke-Command.md)
150186

151187
[New-PSSession](../New-PSSession.md)
188+
189+
[Start-ThreadJob](/powershell/module/ThreadJob/Start-ThreadJob)

reference/5.1/Microsoft.PowerShell.Core/About/about_Scopes.md

Lines changed: 46 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
---
22
keywords: powershell,cmdlet
33
locale: en-us
4-
ms.date: 01/23/2020
4+
ms.date: 03/13/2020
55
online version: https://docs.microsoft.com/powershell/module/microsoft.powershell.core/about/about_scopes?view=powershell-5.1&WT.mc_id=ps-gethelp
66
schema: 2.0.0
77
title: about_scopes
@@ -191,10 +191,52 @@ Using is a special scope modifier that identifies a local variable in a remote
191191
command. Without a modifier, PowerShell expects variables in remote commands
192192
to be defined in the remote session.
193193

194-
The Using scope modifier is introduced in PowerShell 3.0.
194+
The `Using` scope modifier is introduced in PowerShell 3.0.
195+
196+
For any script or command that executes out of session, you need the `Using`
197+
scope modifier to embed variable values from the calling session scope, so that
198+
out of session code can access them. The `Using` scope modifier is supported in
199+
the following contexts:
200+
201+
- Remotely executed commands, started with `Invoke-Command` using the
202+
**ComputerName** or **Session** parameter (remote session)
203+
- Background jobs, started with `Start-Job` (out-of-process session)
204+
- Thread jobs, started via `Start-ThreadJob` or `ForEach-Object -Parallel`
205+
(separate thread session)
206+
207+
Depending on the context, embedded variable values are either independent
208+
copies of the data in the caller's scope or references to it. In remote and
209+
out-of-process sessions, they are always independent copies.
195210

196211
For more information, see [about_Remote_Variables](about_Remote_Variables.md).
197212

213+
In thread sessions, they are passed by reference. This means it is possible to
214+
modify call scope variables in a different thread. To safely modify variables
215+
requires thread synchronization.
216+
217+
For more information see:
218+
219+
- [Start-ThreadJob](../../ThreadJob/Start-ThreadJob.md)
220+
221+
#### Serialization of variable values
222+
223+
Remotely executed commands and background jobs run out-of-process.
224+
Out-of-process sessions use XML-based serialization and deserialization to make
225+
the values of variables available across the process boundaries. The
226+
serialization process converts objects to a **PSObject** that contains the
227+
original objects properties but not its methods.
228+
229+
For a limited set of types, deserialization rehydrates objects back to the
230+
original type. The rehydrated object is a copy of the original object instance.
231+
It has the type properties and methods. For simple types, such as
232+
**System.Version**, the copy is exact. For complex types, the copy is
233+
imperfect. For example, rehydrated certificate objects do not include the
234+
private key.
235+
236+
Instances of all other types are **PSObject** instances. The **PSTypeNames**
237+
property contains the original type name prefixed with **Deserialized**, for
238+
example, **Deserialized.System.Data.DataTable**
239+
198240
### The AllScope Option
199241

200242
Variables and aliases have an **Option** property that can take a value of
@@ -606,3 +648,5 @@ Invoke-Command $s {
606648
[about_Functions](about_Functions.md)
607649

608650
[about_Script_Blocks](about_Script_Blocks.md)
651+
652+
[Start-ThreadJob](/powershell/module/ThreadJob/Start-ThreadJob)

reference/6/Microsoft.PowerShell.Core/About/about_Remote_Variables.md

Lines changed: 52 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
---
22
keywords: powershell,cmdlet
33
locale: en-us
4-
ms.date: 07/23/2019
4+
ms.date: 03/13/2020
55
online version: https://docs.microsoft.com/powershell/module/microsoft.powershell.core/about/about_remote_variables?view=powershell-6&WT.mc_id=ps-gethelp
66
schema: 2.0.0
77
title: about_Remote_Variables
@@ -44,14 +44,6 @@ Invoke-Command -Session $s -ScriptBlock {$ps = "*PowerShell*"}
4444
Invoke-Command -Session $s -ScriptBlock {Get-WinEvent -LogName $ps}
4545
```
4646

47-
The `Using` scope modifier cannot be used to modify a local variable from **PSSession**.
48-
49-
```powershell
50-
$s = New-PSSession -ComputerName S1
51-
$ps = "*PowerShell*"
52-
Invoke-Command -Session $s -ScriptBlock {$Using:ps = 'Cannot assign new value'}
53-
```
54-
5547
## Using local variables
5648

5749
You can use local variables in remote commands, but the variable must be
@@ -77,6 +69,14 @@ Invoke-Command -ComputerName S1 -ScriptBlock {
7769
}
7870
```
7971

72+
The `Using` scope modifier can be used in a **PSSession**.
73+
74+
```powershell
75+
$s = New-PSSession -ComputerName S1
76+
$ps = "*PowerShell*"
77+
Invoke-Command -Session $s -ScriptBlock {Get-WinEvent -LogName $Using:ps}
78+
```
79+
8080
A variable reference such as `$using:var` expands to the value of variable `$var`
8181
from the caller's context. You do not get access to the caller's variable object.
8282
The `Using` scope modifier cannot be used to modify a local variable within the
@@ -85,7 +85,7 @@ The `Using` scope modifier cannot be used to modify a local variable within the
8585
```powershell
8686
$s = New-PSSession -ComputerName S1
8787
$ps = "*PowerShell*"
88-
Invoke-Command -Session $s -ScriptBlock {Get-WinEvent -LogName $Using:ps}
88+
Invoke-Command -Session $s -ScriptBlock {$Using:ps = 'Cannot assign new value'}
8989
```
9090

9191
For more information about `Using`, see [about_Scopes](./about_Scopes.md)
@@ -105,14 +105,49 @@ $Splat = @{ Name = "Win*"; Include = "WinRM" }
105105
Invoke-Command -Session $s -ScriptBlock { Get-Service @Using:Splat }
106106
```
107107

108-
## Using local variables in PowerShell 2.0
108+
### Other situations where the 'Using' scope modifier is needed
109+
110+
For any script or command that executes out of session, you need the `Using`
111+
scope modifier to embed variable values from the calling session scope, so that
112+
out of session code can access them. The `Using` scope modifier is supported in
113+
the following contexts:
114+
115+
- Remotely executed commands, started with `Invoke-Command` using the
116+
**ComputerName**, **HostName**, **SSHConnection** or **Session** parameters
117+
(remote session)
118+
- Background jobs, started with `Start-Job` (out-of-process session)
119+
- Thread jobs started via `Start-ThreadJob` (separate thread session)
120+
121+
Depending on the context, embedded variable values are either independent
122+
copies of the data in the caller's scope or references to it. In remote and
123+
out-of-process sessions, they are always independent copies. In thread
124+
sessions, they are passed by reference.
125+
126+
## Serialization of variable values
127+
128+
Remotely executed commands and background jobs run out-of-process.
129+
Out-of-process sessions use XML-based serialization and deserialization to make
130+
the values of variables available across the process boundaries. The
131+
serialization process converts objects to a **PSObject** that contains the
132+
original objects properties but not its methods.
133+
134+
For a limited set of types, deserialization rehydrates objects back to the
135+
original type. The rehydrated object is a copy of the original object instance.
136+
It has the type properties and methods. For simple types, such as
137+
**System.Version**, the copy is exact. For complex types, the copy is
138+
imperfect. For example, rehydrated certificate objects do not include the
139+
private key.
140+
141+
Instances of all other types are **PSObject** instances. The **PSTypeNames**
142+
property contains the original type name prefixed with **Deserialized**, for
143+
example, **Deserialized.System.Data.DataTable**
144+
145+
## Using local variables with **ArgumentList** parameter
109146

110147
You can use local variables in a remote command by defining parameters for the
111148
remote command and using the **ArgumentList** parameter of the `Invoke-Command`
112149
cmdlet to specify the local variable as the parameter value.
113150

114-
The following command format is valid on PowerShell 2.0 and later versions:
115-
116151
- Use the `param` keyword to define parameters for the remote command. The
117152
parameter names are placeholders that don't need to match the local
118153
variable's name.
@@ -144,8 +179,12 @@ Invoke-Command -ComputerName S1 -ScriptBlock {
144179

145180
[about_Splatting](about_Splatting.md)
146181

182+
[about_Variables](about_Variables.md)
183+
147184
[Enter-PSSession](../Enter-PSSession.md)
148185

149186
[Invoke-Command](../Invoke-Command.md)
150187

151188
[New-PSSession](../New-PSSession.md)
189+
190+
[Start-ThreadJob](../../ThreadJob/Start-ThreadJob.md)

reference/6/Microsoft.PowerShell.Core/About/about_Scopes.md

Lines changed: 46 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
---
22
keywords: powershell,cmdlet
33
locale: en-us
4-
ms.date: 01/23/2020
4+
ms.date: 03/13/2020
55
online version: https://docs.microsoft.com/powershell/module/microsoft.powershell.core/about/about_scopes?view=powershell-6&WT.mc_id=ps-gethelp
66
schema: 2.0.0
77
title: about_scopes
@@ -191,10 +191,52 @@ Using is a special scope modifier that identifies a local variable in a remote
191191
command. Without a modifier, PowerShell expects variables in remote commands
192192
to be defined in the remote session.
193193

194-
The Using scope modifier is introduced in PowerShell 3.0.
194+
The `Using` scope modifier is introduced in PowerShell 3.0.
195+
196+
For any script or command that executes out of session, you need the `Using`
197+
scope modifier to embed variable values from the calling session scope, so that
198+
out of session code can access them. The `Using` scope modifier is supported in
199+
the following contexts:
200+
201+
- Remotely executed commands, started with `Invoke-Command` using the
202+
**ComputerName**, **HostName**, **SSHConnection** or **Session** parameters
203+
(remote session)
204+
- Background jobs, started with `Start-Job` (out-of-process session)
205+
- Thread jobs started via `Start-ThreadJob` (separate thread session)
206+
207+
Depending on the context, embedded variable values are either independent
208+
copies of the data in the caller's scope or references to it. In remote and
209+
out-of-process sessions, they are always independent copies.
195210

196211
For more information, see [about_Remote_Variables](about_Remote_Variables.md).
197212

213+
In thread sessions, they are passed by reference. This means it is possible to
214+
modify call scope variables in a different thread. To safely modify variables
215+
requires thread synchronization.
216+
217+
For more information see:
218+
219+
- [Start-ThreadJob](../../ThreadJob/Start-ThreadJob.md)
220+
221+
#### Serialization of variable values
222+
223+
Remotely executed commands and background jobs run out-of-process.
224+
Out-of-process sessions use XML-based serialization and deserialization to make
225+
the values of variables available across the process boundaries. The
226+
serialization process converts objects to a **PSObject** that contains the
227+
original objects properties but not its methods.
228+
229+
For a limited set of types, deserialization rehydrates objects back to the
230+
original type. The rehydrated object is a copy of the original object instance.
231+
It has the type properties and methods. For simple types, such as
232+
**System.Version**, the copy is exact. For complex types, the copy is
233+
imperfect. For example, rehydrated certificate objects do not include the
234+
private key.
235+
236+
Instances of all other types are **PSObject** instances. The **PSTypeNames**
237+
property contains the original type name prefixed with **Deserialized**, for
238+
example, **Deserialized.System.Data.DataTable**
239+
198240
### The AllScope Option
199241

200242
Variables and aliases have an **Option** property that can take a value of
@@ -606,3 +648,5 @@ Invoke-Command $s {
606648
[about_Functions](about_Functions.md)
607649

608650
[about_Script_Blocks](about_Script_Blocks.md)
651+
652+
[Start-ThreadJob](/powershell/module/ThreadJob/Start-ThreadJob)

0 commit comments

Comments
 (0)