@@ -3,7 +3,7 @@ external help file: System.Management.Automation.dll-Help.xml
3
3
keywords : powershell,cmdlet
4
4
locale : en-us
5
5
Module Name : Microsoft.PowerShell.Core
6
- ms.date : 08/20 /2019
6
+ ms.date : 10/17 /2019
7
7
online version : https://docs.microsoft.com/powershell/module/microsoft.powershell.core/invoke-command?view=powershell-6&WT.mc_id=ps-gethelp
8
8
schema : 2.0.0
9
9
title : Invoke-Command
@@ -434,7 +434,69 @@ process. The output of the first command includes the **PsComputerName** propert
434
434
the name of the computer on which the command ran. The output of the second command, which uses
435
435
** HideComputerName** , doesn't include the ** PsComputerName** column.
436
436
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
438
500
439
501
This example uses the ` Invoke-Command ` cmdlet to run the ` Sample.ps1 ` script on all the computers
440
502
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
449
511
the script block is run on each of the remote computers. This procedure is equivalent to using the
450
512
** ScriptBlock** parameter to submit the contents of the script.
451
513
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
453
515
454
516
This example shows how to run a command on a remote computer that's identified by a Uniform Resource
455
517
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
476
538
` $LiveCred ` variable. The ** AuthenticationMechanism** parameter specifies the use of basic
477
539
authentication. The ** ScriptBlock** parameter specifies a script block that contains the command.
478
540
479
- ### Example 13 : Use a session option
541
+ ### Example 15 : Use a session option
480
542
481
543
This example shows how to create and use a ** SessionOption** parameter.
482
544
@@ -495,7 +557,7 @@ HTTPS connection. The **SessionOption** object is saved in the `$so` variable.
495
557
The ` Invoke-Command ` cmdlet runs a ` Get-HotFix ` command remotely. The ** SessionOption** parameter is
496
558
given the ` $so ` variable.
497
559
498
- ### Example 14 : Manage URI redirection in a remote command
560
+ ### Example 16 : Manage URI redirection in a remote command
499
561
500
562
This example shows how to use the ** AllowRedirection** and ** SessionOption** parameters to manage
501
563
URI redirection in a remote command.
@@ -524,7 +586,7 @@ As a result, if the remote computer specified by **ConnectionURI** returns a red
524
586
PowerShell redirects the connection, but if the new destination returns another redirection message,
525
587
the redirection count value of 1 is exceeded, and ` Invoke-Command ` returns a non-terminating error.
526
588
527
- ### Example 15 : Access a network share in a remote session
589
+ ### Example 17 : Access a network share in a remote session
528
590
529
591
This example shows how to access a network share from a remote session. Three computers are used to
530
592
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
567
629
from the ` \\Net03\Scripts ` network share. The command uses the ** Authentication** parameter with a
568
630
value of ** CredSSP** and the ** Credential** parameter with a value of ** Domain01\Admin01** .
569
631
570
- ### Example 16 : Start scripts on many remote computers
632
+ ### Example 18 : Start scripts on many remote computers
571
633
572
634
This example runs a script on more than a hundred computers. To minimize the impact on the local
573
635
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).
594
656
To get the results of commands and scripts that run in disconnected sessions, use the
595
657
` Receive-PSSession ` cmdlet.
596
658
597
- ### Example 17 : Run a command on a remote computer using SSH
659
+ ### Example 19 : Run a command on a remote computer using SSH
598
660
599
661
This example shows how to run a command on a remote computer using Secure Shell (SSH). If SSH is
600
662
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.
604
666
Invoke-Command -HostName UserA@LinuxServer01 -ScriptBlock { Get-MailBox * }
605
667
```
606
668
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
608
670
609
671
This example shows how to run a command on a remote computer using SSH and specifying a key file for
610
672
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.
614
676
Invoke-Command -HostName UserA@LinuxServer01 -ScriptBlock { Get-MailBox * } -KeyFilePath /UserA/UserAKey_rsa
615
677
```
616
678
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
618
680
619
681
This example shows how to run a script file on multiple remote computers using SSH and the
620
682
** SSHConnection** parameter set. The ** SSHConnection** parameter takes an array of hash tables that
0 commit comments