Skip to content

Commit d0caf6b

Browse files
davidsmatlaksdwheeler
authored andcommitted
issue 4913 (#4953)
1 parent bd86464 commit d0caf6b

File tree

3 files changed

+213
-27
lines changed

3 files changed

+213
-27
lines changed

reference/5.1/Microsoft.PowerShell.Core/Invoke-Command.md

Lines changed: 69 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ external help file: System.Management.Automation.dll-Help.xml
33
keywords: powershell,cmdlet
44
locale: en-us
55
Module Name: Microsoft.PowerShell.Core
6-
ms.date: 08/20/2019
6+
ms.date: 10/17/2019
77
online version: https://docs.microsoft.com/powershell/module/microsoft.powershell.core/invoke-command?view=powershell-5.1&WT.mc_id=ps-gethelp
88
schema: 2.0.0
99
title: Invoke-Command
@@ -393,7 +393,69 @@ process. The output of the first command includes the **PsComputerName** propert
393393
the name of the computer on which the command ran. The output of the second command, which uses
394394
**HideComputerName**, doesn't include the **PsComputerName** column.
395395

396-
### Example 11: Run a script on all the computers listed in a text file
396+
### Example 11: Use the Param keyword in a script block
397+
398+
The `Param` keyword and the **ArgumentList** parameter are used to pass variable values to named
399+
parameters in a script block. This example displays file names that begin with the letter `a` and
400+
have the `.pdf` extension.
401+
402+
For more information about the `Param` keyword, see
403+
[about_Language_Keywords](./about/about_language_keywords.md#param).
404+
405+
```powershell
406+
$parameters = @{
407+
ComputerName = "Server01"
408+
ScriptBlock = { Param ($param1,$param2) Get-ChildItem -Name $param1 -Include $param2 }
409+
ArgumentList = "a*", "*.pdf"
410+
}
411+
Invoke-Command @parameters
412+
```
413+
414+
```Output
415+
aa.pdf
416+
ab.pdf
417+
ac.pdf
418+
az.pdf
419+
```
420+
421+
`Invoke-Command` uses the **ScriptBlock** parameter that defines two variables, `$param1` and
422+
`$param2`. `Get-ChildItem` uses the named parameters, **Name** and **Include** with the variable
423+
names. The **ArgumentList** passes the values to the variables.
424+
425+
### Example 12: Use the $args automatic variable in a script block
426+
427+
The `$args` automatic variable and the **ArgumentList** parameter are used to pass array values to
428+
parameter positions in a script block. This example displays a server's directory contents of `.txt`
429+
files. The `Get-ChildItem` **Path** parameter is position 0 and the **Filter** parameter is position
430+
1.
431+
432+
For more information about the `$args` variable, see
433+
[about_Automatic_Variables](./about/about_automatic_variables.md#args)
434+
435+
```powershell
436+
$parameters = @{
437+
ComputerName = "Server01"
438+
ScriptBlock = { Get-ChildItem $args[0] $args[1] }
439+
ArgumentList = "C:\Test", "*.txt*"
440+
}
441+
Invoke-Command @parameters
442+
```
443+
444+
```Output
445+
Directory: C:\Test
446+
447+
Mode LastWriteTime Length Name
448+
---- ------------- ------ ----
449+
-a--- 6/12/2019 15:15 128 alog.txt
450+
-a--- 7/27/2019 15:16 256 blog.txt
451+
-a--- 9/28/2019 17:10 64 zlog.txt
452+
```
453+
454+
`Invoke-Command` uses a **ScriptBlock** parameter and `Get-ChildItem` specifies the `$args[0]` and
455+
`$args[1]` array values. The **ArgumentList** passes the `$args` array values to the `Get-ChildItem`
456+
parameter positions for **Path** and **Filter**.
457+
458+
### Example 13: Run a script on all the computers listed in a text file
397459

398460
This example uses the `Invoke-Command` cmdlet to run the `Sample.ps1` script on all the computers
399461
listed in the `Servers.txt` file. The command uses the **FilePath** parameter to specify the script
@@ -408,7 +470,7 @@ When you submit the command, the content of the `Sample.ps1` file is copied into
408470
the script block is run on each of the remote computers. This procedure is equivalent to using the
409471
**ScriptBlock** parameter to submit the contents of the script.
410472

411-
### Example 12: Run a command on a remote computer by using a URI
473+
### Example 14: Run a command on a remote computer by using a URI
412474

413475
This example shows how to run a command on a remote computer that's identified by a Uniform Resource
414476
Identifier (URI). This particular example runs a `Set-Mailbox` command on a remote Exchange server.
@@ -435,7 +497,7 @@ the Exchange server endpoint. The **Credential** parameter specifies the credent
435497
`$LiveCred` variable. The **AuthenticationMechanism** parameter specifies the use of basic
436498
authentication. The **ScriptBlock** parameter specifies a script block that contains the command.
437499

438-
### Example 13: Use a session option
500+
### Example 15: Use a session option
439501

440502
This example shows how to create and use a **SessionOption** parameter.
441503

@@ -454,7 +516,7 @@ HTTPS connection. The **SessionOption** object is saved in the `$so` variable.
454516
The `Invoke-Command` cmdlet runs a `Get-HotFix` command remotely. The **SessionOption** parameter is
455517
given the `$so` variable.
456518

457-
### Example 14: Manage URI redirection in a remote command
519+
### Example 16: Manage URI redirection in a remote command
458520

459521
This example shows how to use the **AllowRedirection** and **SessionOption** parameters to manage
460522
URI redirection in a remote command.
@@ -483,7 +545,7 @@ As a result, if the remote computer specified by **ConnectionURI** returns a red
483545
PowerShell redirects the connection, but if the new destination returns another redirection message,
484546
the redirection count value of 1 is exceeded, and `Invoke-Command` returns a non-terminating error.
485547

486-
### Example 15: Access a network share in a remote session
548+
### Example 17: Access a network share in a remote session
487549

488550
This example shows how to access a network share from a remote session. Three computers are used to
489551
demonstrate the example. Server01 is the local computer, Server02 is the remote computer, and Net03
@@ -526,7 +588,7 @@ The `$parameters` variable contains the parameter values to connect to the netwo
526588
from the `\\Net03\Scripts` network share. The command uses the **Authentication** parameter with a
527589
value of **CredSSP** and the **Credential** parameter with a value of **Domain01\Admin01**.
528590

529-
### Example 16: Start scripts on many remote computers
591+
### Example 18: Start scripts on many remote computers
530592

531593
This example runs a script on more than a hundred computers. To minimize the impact on the local
532594
computer, it connects to each computer, starts the script, and then disconnects from each computer.

reference/6/Microsoft.PowerShell.Core/Invoke-Command.md

Lines changed: 72 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ external help file: System.Management.Automation.dll-Help.xml
33
keywords: powershell,cmdlet
44
locale: en-us
55
Module Name: Microsoft.PowerShell.Core
6-
ms.date: 08/20/2019
6+
ms.date: 10/17/2019
77
online version: https://docs.microsoft.com/powershell/module/microsoft.powershell.core/invoke-command?view=powershell-6&WT.mc_id=ps-gethelp
88
schema: 2.0.0
99
title: Invoke-Command
@@ -434,7 +434,69 @@ process. The output of the first command includes the **PsComputerName** propert
434434
the name of the computer on which the command ran. The output of the second command, which uses
435435
**HideComputerName**, doesn't include the **PsComputerName** column.
436436

437-
### Example 11: Run a script on all the computers listed in a text file
437+
### Example 11: Use the Param keyword in a script block
438+
439+
The `Param` keyword and the **ArgumentList** parameter are used to pass variable values to named
440+
parameters in a script block. This example displays file names that begin with the letter `a` and
441+
have the `.pdf` extension.
442+
443+
For more information about the `Param` keyword, see
444+
[about_Language_Keywords](./about/about_language_keywords.md#param).
445+
446+
```powershell
447+
$parameters = @{
448+
ComputerName = "Server01"
449+
ScriptBlock = { Param ($param1,$param2) Get-ChildItem -Name $param1 -Include $param2 }
450+
ArgumentList = "a*", "*.pdf"
451+
}
452+
Invoke-Command @parameters
453+
```
454+
455+
```Output
456+
aa.pdf
457+
ab.pdf
458+
ac.pdf
459+
az.pdf
460+
```
461+
462+
`Invoke-Command` uses the **ScriptBlock** parameter that defines two variables, `$param1` and
463+
`$param2`. `Get-ChildItem` uses the named parameters, **Name** and **Include** with the variable
464+
names. The **ArgumentList** passes the values to the variables.
465+
466+
### Example 12: Use the $args automatic variable in a script block
467+
468+
The `$args` automatic variable and the **ArgumentList** parameter are used to pass array values to
469+
parameter positions in a script block. This example displays a server's directory contents of `.txt`
470+
files. The `Get-ChildItem` **Path** parameter is position 0 and the **Filter** parameter is position
471+
1.
472+
473+
For more information about the `$args` variable, see
474+
[about_Automatic_Variables](./about/about_automatic_variables.md#args)
475+
476+
```powershell
477+
$parameters = @{
478+
ComputerName = "Server01"
479+
ScriptBlock = { Get-ChildItem $args[0] $args[1] }
480+
ArgumentList = "C:\Test", "*.txt*"
481+
}
482+
Invoke-Command @parameters
483+
```
484+
485+
```Output
486+
Directory: C:\Test
487+
488+
Mode LastWriteTime Length Name
489+
---- ------------- ------ ----
490+
-a--- 6/12/2019 15:15 128 alog.txt
491+
-a--- 7/27/2019 15:16 256 blog.txt
492+
-a--- 9/28/2019 17:10 64 zlog.txt
493+
```
494+
495+
`Invoke-Command` uses a **ScriptBlock** parameter and `Get-ChildItem` specifies the `$args[0]` and
496+
`$args[1]` array values. The **ArgumentList** passes the `$args` array values to the `Get-ChildItem`
497+
parameter positions for **Path** and **Filter**.
498+
499+
### Example 13: Run a script on all the computers listed in a text file
438500

439501
This example uses the `Invoke-Command` cmdlet to run the `Sample.ps1` script on all the computers
440502
listed in the `Servers.txt` file. The command uses the **FilePath** parameter to specify the script
@@ -449,7 +511,7 @@ When you submit the command, the content of the `Sample.ps1` file is copied into
449511
the script block is run on each of the remote computers. This procedure is equivalent to using the
450512
**ScriptBlock** parameter to submit the contents of the script.
451513

452-
### Example 12: Run a command on a remote computer by using a URI
514+
### Example 14: Run a command on a remote computer by using a URI
453515

454516
This example shows how to run a command on a remote computer that's identified by a Uniform Resource
455517
Identifier (URI). This particular example runs a `Set-Mailbox` command on a remote Exchange server.
@@ -476,7 +538,7 @@ the Exchange server endpoint. The **Credential** parameter specifies the credent
476538
`$LiveCred` variable. The **AuthenticationMechanism** parameter specifies the use of basic
477539
authentication. The **ScriptBlock** parameter specifies a script block that contains the command.
478540

479-
### Example 13: Use a session option
541+
### Example 15: Use a session option
480542

481543
This example shows how to create and use a **SessionOption** parameter.
482544

@@ -495,7 +557,7 @@ HTTPS connection. The **SessionOption** object is saved in the `$so` variable.
495557
The `Invoke-Command` cmdlet runs a `Get-HotFix` command remotely. The **SessionOption** parameter is
496558
given the `$so` variable.
497559

498-
### Example 14: Manage URI redirection in a remote command
560+
### Example 16: Manage URI redirection in a remote command
499561

500562
This example shows how to use the **AllowRedirection** and **SessionOption** parameters to manage
501563
URI redirection in a remote command.
@@ -524,7 +586,7 @@ As a result, if the remote computer specified by **ConnectionURI** returns a red
524586
PowerShell redirects the connection, but if the new destination returns another redirection message,
525587
the redirection count value of 1 is exceeded, and `Invoke-Command` returns a non-terminating error.
526588

527-
### Example 15: Access a network share in a remote session
589+
### Example 17: Access a network share in a remote session
528590

529591
This example shows how to access a network share from a remote session. Three computers are used to
530592
demonstrate the example. Server01 is the local computer, Server02 is the remote computer, and Net03
@@ -567,7 +629,7 @@ The `$parameters` variable contains the parameter values to connect to the netwo
567629
from the `\\Net03\Scripts` network share. The command uses the **Authentication** parameter with a
568630
value of **CredSSP** and the **Credential** parameter with a value of **Domain01\Admin01**.
569631

570-
### Example 16: Start scripts on many remote computers
632+
### Example 18: Start scripts on many remote computers
571633

572634
This example runs a script on more than a hundred computers. To minimize the impact on the local
573635
computer, it connects to each computer, starts the script, and then disconnects from each computer.
@@ -594,7 +656,7 @@ and the **IdleTimeout** value is set to **43200000** milliseconds (12 hours).
594656
To get the results of commands and scripts that run in disconnected sessions, use the
595657
`Receive-PSSession` cmdlet.
596658

597-
### Example 17: Run a command on a remote computer using SSH
659+
### Example 19: Run a command on a remote computer using SSH
598660

599661
This example shows how to run a command on a remote computer using Secure Shell (SSH). If SSH is
600662
configured on the remote computer to prompt for passwords, then you'll get a password prompt.
@@ -604,7 +666,7 @@ Otherwise, you'll have to use SSH key-based user authentication.
604666
Invoke-Command -HostName UserA@LinuxServer01 -ScriptBlock { Get-MailBox * }
605667
```
606668

607-
### Example 18: Run a command on a remote computer using SSH and specify a user authentication key
669+
### Example 20: Run a command on a remote computer using SSH and specify a user authentication key
608670

609671
This example shows how to run a command on a remote computer using SSH and specifying a key file for
610672
user authentication. You won't be prompted for a password unless the key authentication fails and
@@ -614,7 +676,7 @@ the remote computer is configured to allow basic password authentication.
614676
Invoke-Command -HostName UserA@LinuxServer01 -ScriptBlock { Get-MailBox * } -KeyFilePath /UserA/UserAKey_rsa
615677
```
616678

617-
### Example 19: Run a script file on multiple remote computers using SSH as a job
679+
### Example 21: Run a script file on multiple remote computers using SSH as a job
618680

619681
This example shows how to run a script file on multiple remote computers using SSH and the
620682
**SSHConnection** parameter set. The **SSHConnection** parameter takes an array of hash tables that

0 commit comments

Comments
 (0)