当前位置:网站首页 > 更多 > 玩电脑 > 正文

[玩转系统] PowerShell 调用命令 | Invoke-Command 的前 18 个参数

作者:精品下载站 日期:2024-12-14 04:55:43 浏览:15 分类:玩电脑

PowerShell 调用命令 | Invoke-Command 的前 18 个参数


[玩转系统] PowerShell 调用命令 | Invoke-Command 的前 18 个参数

PowerShell 调用命令简介

Invoke-Command 用于在本地或远程计算机上运行任何命令并返回其输出。它允许我们编写脚本或命令块并调用该脚本或命令块。它将返回命令执行的结果。该命令可以在本地或远程运行。让我们通过一些基本的例子来理解它。非常简单,如果我们有一些命令 {command1 command 2 Commands 3},并且我们想要执行它们并退出它们的执行,那么我们可以使用 PowerShell Invoke-Command。

PowerShell Invoke-Command 的语法

执行以下命令,您将获得其所有语法和参数的输出。我们展示了 PowerShell Invoke-Command 的所有可用语法。每种语法都将用于特定情况。

语法:

Get-Help Invoke-Command -full

以下是运行上述命令后得到的语法。

1.将参数传递给远程计算机

Invoke-Command
[-ScriptBlock] <scriptblock>
[-NoNewScope]
[-InputObject <psobject>]
[-ArgumentList <list of arguments in object form>]
[<CommonParameters>]

2.定义作业名称

Invoke-Command
[[-Session] <PSSession[]>]
[-FilePath] <string>
[-ThrottleLimit <int>]
[-AsJob]
[-HideComputerName]
[-JobName <understandable user defined names>]
[-RemoteDebug] [-InputObject <psobject>]
[-ArgumentList <list of arguments in object form>]
[<CommonParameters>]

3. 在远程调试模式下运行命令

Invoke-Command
[[-Session] <PSSession[]>]
[-ScriptBlock] <scriptblock>
[-ThrottleLimit <int>]
[-AsJob]
[-HideComputerName]
[-JobName <string>]
[-RemoteDebug]
[-InputObject <psobject>]
[-ArgumentList <list of arguments in object form>]
[<CommonParameters>]
Invoke-Command
[[-ComputerName] <string[]>]
[-FilePath] <string>
[-Credential <pscredential>]
[-Port <int>] [-UseSSL]
[-ConfigurationName <string>]
[-ApplicationName <string>]
[-ThrottleLimit <int>]
[-AsJob]
[-InDisconnectedSession]
[-SessionName <string[]>]
[-HideComputerName]
[-JobName <string>]
[-SessionOption <PSSessionOption>]
[-Authentication {Default | Basic | Negotiate | NegotiateWithImplicitCredential | Credssp | Digest | Kerberos}]
[-EnableNetworkAccess]
[-RemoteDebug]
[-InputObject <psobject>]
[-ArgumentList <list of arguments in object form>]
[<CommonParameters>]
Invoke-Command
[[-ComputerName] <string[]>]
[-ScriptBlock] <scriptblock>
[-Credential <pscredential>]
[-Port <port where remote computer is listening>]
[-UseSSL]
[-ConfigurationName <string>]
[-ApplicationName <string>]
[-ThrottleLimit <int>]
[-AsJob]
[-InDisconnectedSession]
[-SessionName <string[]>]
[-HideComputerName]
[-JobName <string>]
[-SessionOption <PSSessionOption>]
[-Authentication {Default | Basic | Negotiate | NegotiateWithImplicitCredential | Credssp | Digest | Kerberos}]
[-EnableNetworkAccess]
[-RemoteDebug]
[-InputObject <psobject>]
[-ArgumentList <list of arguments in object form>]
[-CertificateThumbprint <string>]
[<CommonParameters>]
Invoke-Command
[[-ConnectionUri] <uri[]>]
[-ScriptBlock] <scriptblock>
[-Credential <pscredential>]
[-ConfigurationName <string>]
[-ThrottleLimit <int>]
[-AsJob]
[-InDisconnectedSession]
[-HideComputerName]
[-JobName <string>]
[-AllowRedirection]
[-SessionOption <PSSessionOption>]
[-Authentication {Default | Basic | Negotiate | NegotiateWithImplicitCredential | Credssp | Digest | Kerberos}]
[-EnableNetworkAccess]
[-RemoteDebug]
[-InputObject <psobject>]
[-ArgumentList <list of arguments in object form>]
[-CertificateThumbprint <string>]
[<CommonParameters>]
Invoke-Command
[[-ConnectionUri] <uri[]>]
[-FilePath] <string>
[-Credential <pscredential>]
[-ConfigurationName <string>]
[-ThrottleLimit <int>]
[-AsJob]
[-InDisconnectedSession]
[-HideComputerName]
[-JobName <string>]
[-AllowRedirection]
[-SessionOption <PSSessionOption>]
[-Authentication {Default | Basic | Negotiate | NegotiateWithImplicitCredential | Credssp | Digest | Kerberos}]
[-EnableNetworkAccess]
[-RemoteDebug]
[-InputObject <psobject>]
[-ArgumentList <Object[]>]
[<CommonParameters>]
Invoke-Command
[-VMId] <guid[]>
[-ScriptBlock] <scriptblock>
-Credential <pscredential>
[-ConfigurationName <string>]
[-ThrottleLimit <int>]
[-AsJob]
[-HideComputerName]
[-RemoteDebug]
[-InputObject <psobject>]
[-ArgumentList <list of arguments in object form>]
[<CommonParameters>]
Invoke-Command
[-ScriptBlock] <scriptblock>
-Credential <pscredential>
-VMName <string[]>
[-ConfigurationName <string>]
[-ThrottleLimit <int>]
[-AsJob]
[-HideComputerName]
[-RemoteDebug]
[-InputObject <psobject>]
[-ArgumentList <list of arguments in object form>]
[<CommonParameters>]
Invoke-Command
[-VMId] <guid[]>
[-FilePath] <string>
-Credential <pscredential>
[-ConfigurationName <string>]
[-ThrottleLimit <int>]
[-AsJob]
[-HideComputerName]
[-RemoteDebug]
[-InputObject <psobject>]
[-ArgumentList <list of arguments in object form>]
[<CommonParameters>]
Invoke-Command
[-FilePath] <path of the file>
-Credential <pscredential>
-VMName <string[]>
[-ConfigurationName <string>]
[-ThrottleLimit <int>]
[-AsJob]
[-HideComputerName]
[-RemoteDebug]
[-InputObject <psobject>]
[-ArgumentList <list of arguments in object form>]
[<CommonParameters>]
Invoke-Command
-ScriptBlock <scriptblock>
-HostName <string[]>
[-Port <port number where remote computer is listening>]
[-AsJob]
[-HideComputerName]
[-UserName <string>]
[-KeyFilePath <string>]
[-SSHTransport {true}]
[-RemoteDebug]
[-InputObject <psobject>]
[-ArgumentList <list of arguments in object form>]
[-Subsystem <string>]
[<CommonParameters>]
Invoke-Command
[-ScriptBlock] <scriptblock>
-ContainerId <string[]>
[-ConfigurationName <string>]
[-ThrottleLimit <int>]
[-AsJob]
[-HideComputerName]
[-JobName <string>]
[-RunAsAdministrator]
[-RemoteDebug]
[-InputObject <psobject>]
[-ArgumentList <list of arguments in object form>]
[<CommonParameters>]
Invoke-Command
[-FilePath] <path of the file>
-ContainerId <string[]>
[-ConfigurationName <string>]
[-ThrottleLimit <int>]
[-AsJob]
[-HideComputerName]
[-JobName <string>]
[-RunAsAdministrator]
[-RemoteDebug]
[-InputObject <psobject>]
[-ArgumentList <list of arguments in object form>]
[<CommonParameters>]
Invoke-Command
-ScriptBlock <scriptblock>
-SSHConnection <hashtable[]>
[-AsJob]
[-HideComputerName]
[-RemoteDebug]
[-InputObject <psobject>]
[-ArgumentList <list of arguments in object form>]
[<CommonParameters>]
Invoke-Command
-FilePath <path of the file>
-HostName <string[]>
[-AsJob]
[-HideComputerName]
[-UserName <string>]
[-KeyFilePath <string>]
[-SSHTransport {true}]
[-RemoteDebug]
[-InputObject <psobject>]
[-ArgumentList <list of arguments in object form>]
[<CommonParameters>]
Invoke-Command
-FilePath <path of the file>
-SSHConnection <hashtable[]>
[-AsJob]
[-HideComputerName]
[-RemoteDebug]
[-InputObject <psobject>]
[-ArgumentList <list of arguments in object form>]
[<CommonParameters>]

PowerShell Invoke-Command 的前 18 个参数

PowerShell invoke-command 的前 18 个参数解释如下。

1.允许重定向:有时,当您尝试访问远程计算机时,远程计算机可能会返回另一个 URI 以进行备用重定向。请记住,如果我们想要重定向到远程返回 URL,则 PowerShell 会自动重定向,而不是应该使用此参数。

2. ApplicationName:在这里我们可以定义 ConnectionURI 的应用程序名称。仅当我们不使用 ConnectionURI 时才应使用此参数。

3. ArgumentList:这些是我们可以传递给远程命令的变量,因为远程命令变量将被这些变量替换。传递以逗号分隔的参数。

4. AsJob:假设您正在远程计算机上运行一些命令,并且该命令或作业花费了太多时间,在这种情况下,AsJob 命令将返回一些对象,提供正在运行的作业的详细信息。很简单,它允许我们运行一些后台,以防上一个作业需要花费大量时间来执行。

5.身份验证:这定义了访问远程计算机的身份验证机制。但它仅适用于 Windows、Vista 和 Windows Server 2008。

6.证书指纹:它允许我们使用一些数字公钥证书来与远程计算机建立连接。

7. ComputerName:此命令定义了我们要运行命令的计算机。如果我们不定义-ComputerNameit 将在本地计算机上运行命令

8. ConnectionUri:定义统一资源标识符 (URI),其中包含会话的端点。

以下是其格式:

://:/

默认值如下:

http://localhost:8081/Ranjan

如果您没有端点,您可以使用 UseSSL 和 Port 来定义连接 URI 值。

9. ContainerId:定义 ID 数组。

10.凭据:显示有权执行某些操作的所有用户帐户。如果我们没有定义任何内容,那么它将仅提供当前用户。

11. EnableNetworkAccess:假设我们想要从一台计算机(一个会话)复制一些 XML 文件并将其粘贴到本地计算机,那么我们可以使用此命令。它向环回会话添加一个安全令牌,该会话从另一个会话获取数据。

12.文件路径:如果我们想访问远程计算机上的文件或脚本,则可以使用此命令。我们只需指定它的路径即可。

13.主机名:指定基于安全外壳 (SSH) 的连接的计算机名称数组。这与 ComputerName 参数类似,只不过与远程计算机的连接是使用 SSH 而不是 Windows WinRM 建立的。

14. JobName:它允许我们编写用户友好的作业名称。它的简单语法是 Job。

15. NoNewScope:它定义给定的 cmdlet 将在当前作用域中运行,如果我们不指定此参数,那么它将在自己的作用域中运行。

16.端口:定义远程计算机运行的端口。远程端口是用户远程计算机。因此,要连接任何计算机,我们需要获取它正在侦听的远程计算机端口。

17. RemoteDebug:它允许我们在远程计算机上放置一些调试器,以执行我们要执行的命令以进行检查或调试。

18.用户名:定义我们正在运行命令的远程帐户的用户名,例如,如果正在为远程计算机 XYX 运行命令,则必须有一个名为“XYZuser”的用户。

实施 PowerShell Invoke-Command 的示例

以下是实现 PowerShell 调用命令的不同示例。

示例#1

Invoke-Command -ScriptBlock {Get-Process}

输出:

[玩转系统] PowerShell 调用命令 | Invoke-Command 的前 18 个参数

我们可以在远程计算机上运行相同的命令,例如

Invoke-Command -ComputerName “name of host or remote computer”-ScriptBlock {$p = Get-Process PowerShell}

示例#2

下面是在本地计算机上获取计算机版本的示例。

invoke-command -scriptblock {(get-host).version}

输出:

[玩转系统] PowerShell 调用命令 | Invoke-Command 的前 18 个参数

对于远程计算机,

$version = Invoke-Command -ComputerName (Get-Content Machines.txt) -ScriptBlock {(Get-Host).Version}

示例#3

在下面的示例中,我们将 Get-Host 放在大括号内,并且在执行此命令时,我们将获取作为完整主机详细信息的输出。

Invoke-Command -ScriptBlock {Get-Host}

输出:

[玩转系统] PowerShell 调用命令 | Invoke-Command 的前 18 个参数

示例#4

在下面的示例中,我们将 Get-Culture 放在大括号内,执行此命令时我们将获得输出。

Invoke-Command -ScriptBlock {Get-Culture}

输出:

[玩转系统] PowerShell 调用命令 | Invoke-Command 的前 18 个参数

如果您想对您可以使用的任何远程计算机运行相同的命令,

Invoke-Command -ComputerName “remote computer name” -Credential domain\username -ScriptBlock {Get-Culture}

此命令也适用于 Linux 和 macOS。因此,如果你们中有人使用 Linux 或 macOS,您可以按照相关文档进行操作。

结论

如果我们想要在本地(同一台计算机)或远程计算机上运行某些已定义的命令(命令块),我们可以使用 Invoke-Command。

您需要 登录账户 后才能发表评论

取消回复欢迎 发表评论:

关灯