Running commands in local and remote systems have different methods. We can use psexec
, commands own feature or Invoke-Command
cmdlet. In this tutorial we will look how to use Invoke-Command
in powershell. Invoke-Command
runs commands remotely and returns the output like success or error messages.
在本地和远程系统中运行命令有不同的方法。 我们可以使用psexec
,命令自己的功能或Invoke-Command
cmdlet。 在本教程中,我们将研究如何在Powershell中使用Invoke-Command
。 Invoke-Command
远程运行命令,并返回输出(如成功或错误消息)。
帮帮我 (Help)
Detailed help about the Invoke-Command
can be get like below.
有关Invoke-Command
详细帮助可以从下面获得。
PS> Run Powershell Script On Remote Computer

Also more detailed help can be get by adding -full
option like below. This will provide enormous output and provide help like parameters details, usage examples etc.
通过添加如下所示的-full
选项,也可以获得更详细的帮助。 这将提供巨大的输出,并提供诸如参数详细信息,用法示例等帮助。
PS> Get-Help Invoke-Command -full

启用PSRemoting(Enable PSRemoting)
In order to run Powershell command on the remote systems the remote systems should be configured to accept remote powershell commands via PSRemoting
. Following post shows how to enable PSRemoting in windows operating systems.
为了在远程系统上运行Powershell命令,应该将远程系统配置为通过PSRemoting
接受远程Powershell命令。 以下文章显示了如何在Windows操作系统中启用PSRemoting。
How To Enable Powershell Remoting PSRemoting or WinRM?
如何启用Powershell Remoting PSRemoting或WinRM?
在远程计算机上运行命令 (Run Command On Remote Computer)
One of the most used example of Invoke-Command
is running command on remote systems. We will specify following parameters to run command remotely.
最常用的Invoke-Command
示例之一是在远程系统上运行命令。 我们将指定以下参数以远程运行命令。
-ComputerName
specifies the host name or IP address of the remote system-ComputerName
指定远程系统的主机名或IP地址Credentials
specifies the remote system login user with domain or computer nameCredentials
使用域或计算机名称指定远程系统登录用户-ScriptBlock
specifies the command block that will be run on remote system in curly braces.-ScriptBlock
指定将在大括号中在远程系统上运行的命令块。
PS> Invoke-Command -ComputerName 192.168.122.66 -Credential Administrator -ScriptBlock {Get-Culture}

If we do not provide the password or any credentials Powershell will currently available local credentials to authentication remote system.
如果我们不提供密码或任何凭据,Powershell将当前将本地凭据用于身份验证远程系统。

As we can see from the given command output it is executed successfully on the remote system and the response is printed in the local system shell.
从给定的命令输出中可以看到,它已在远程系统上成功执行,并且响应被打印在本地系统外壳中。
在远程计算机上运行Powershell脚本 (Run Powershell Script On Remote Computer)
In previous example we provided a shell command to run on the remote system. Powershell also have the feature to run local powershell scripts on the remote system. We will provide the Powershell File to run content on the remote system.We have following Powershell script file named myscript.ps1
在前面的示例中,我们提供了一个shell命令以在远程系统上运行。 Powershell还具有在远程系统上运行本地Powershell脚本的功能。 我们将提供Powershell文件以在远程系统上运行内容。下面是名为myscript.ps1
Powershell脚本文件。
Get-Host
Get-Culture
We will use -FilePath
option to provide script file path like below.
我们将使用-FilePath
选项来提供脚本文件路径,如下所示。
PS> Invoke-Command -ComputerName 192.168.122.66 -Credential Administrator -FilePath c:\myscript.ps1

We can see from output that provided script file commands are executed in a row on the given remote system.
从输出中我们可以看到,提供的脚本文件命令在给定的远程系统上连续执行。
在多台远程计算机上运行命令 (Run Command On Multiple Remote Computers)
Now previous examples were useful but less effective because we have only provided single remote system. The power of the Invoke-Command
comes from running commands multiple remote systems. We will specify more than one remote system in this step. We will use same syntax with previous commands but add more remote system IP address or hostname to the -ComputerName
option by separating them with comma. We will provide remote system IP address as 192.168.122.66
and hostname localhost
. We can add more remote systems just separating comma again
现在,前面的示例很有用,但效果不佳,因为我们仅提供了一个远程系统。 Invoke-Command
来自在多个远程系统上运行命令。 在这一步中,我们将指定多个远程系统。 我们将对先前的命令使用相同的语法,但是通过用逗号分隔它们,将更多的远程系统IP地址或主机名添加到-ComputerName
选项。 我们将提供远程系统IP地址为192.168.122.66
和主机名localhost
。 我们可以添加更多的远程系统,只需再次分隔逗号即可
PS> Invoke-Command -ComputerName 192.168.122.66 , localhost -Credential Administrator -ScriptBlock {Get-Culture}

We can see the out that each remote system output is listed as a new line in the same table and columns. This type of output makes filtering and searching easier and more readable.
我们可以看到,每个远程系统输出在同一表和列中均作为新行列出。 这种类型的输出使过滤和搜索更加容易且更具可读性。
在多台远程计算机上运行Powershell脚本 (Run Powershell Script On Multiple Remote Computer)
We will merge previous examples and run given Powershell script file on the multiple remote systems. The command will be as you expect like below.
我们将合并前面的示例,并在多个远程系统上运行给定的Powershell脚本文件。 该命令将如您所愿,如下所示。
PS> Invoke-Command -ComputerName 192.168.122.66 , localhost -Credential Administrator -FilePath C:\myscript.ps1

从远程计算机上的本地变量运行命令(Run Command From Local Variable On Remote Computer)
Another useful resource for command to be run on the remote system is Powershell local variables. We can use Powershell local variable in the command block like a regular powershell command. We will use ScriptBlock
again but we will specify intended variable name by prefixing with $Using:
keyword. In this example we create a local variable name $dir
and set its value Get-Host
. Then we use this variable in ScriptBlock as stated previously.
用于在远程系统上运行的命令的另一个有用资源是Powershell局部变量。 我们可以像常规powershell命令一样在命令块中使用Powershell局部变量。 我们将再次使用ScriptBlock
,但将通过在$Using:
关键字前面指定预期的变量名称。 在此示例中,我们创建一个本地变量名称$dir
并设置其值Get-Host
。 然后,如前所述,我们在ScriptBlock中使用此变量。
PS> $dir="C:\"
We have setup our local variable which is the path we want to list files and folder.
我们已经设置了本地变量,这是我们要列出文件和文件夹的路径。
PS> Invoke-Command -ComputerName 192.168.122.66 -Credential Administrator -ScriptBlock {Dir $Using:dir}

As we can see remote systems C:\
path is listed as expected.
如我们所见,远程系统C:\
路径已按预期列出。
翻译自: https://www.poftut.com/run-commands-local-remote-system-powershell-invoke-command-cmdlet/