[玩转系统] PowerShell 调用命令 | Invoke-Command 的前 18 个参数
作者:精品下载站 日期:2024-12-14 04:55:43 浏览:15 分类:玩电脑
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}
输出:
我们可以在远程计算机上运行相同的命令,例如
Invoke-Command -ComputerName “name of host or remote computer”-ScriptBlock {$p = Get-Process PowerShell}
示例#2
下面是在本地计算机上获取计算机版本的示例。
invoke-command -scriptblock {(get-host).version}
输出:
对于远程计算机,
$version = Invoke-Command -ComputerName (Get-Content Machines.txt) -ScriptBlock {(Get-Host).Version}
示例#3
在下面的示例中,我们将 Get-Host 放在大括号内,并且在执行此命令时,我们将获取作为完整主机详细信息的输出。
Invoke-Command -ScriptBlock {Get-Host}
输出:
示例#4
在下面的示例中,我们将 Get-Culture 放在大括号内,执行此命令时我们将获得输出。
Invoke-Command -ScriptBlock {Get-Culture}
输出:
如果您想对您可以使用的任何远程计算机运行相同的命令,
Invoke-Command -ComputerName “remote computer name” -Credential domain\username -ScriptBlock {Get-Culture}
此命令也适用于 Linux 和 macOS。因此,如果你们中有人使用 Linux 或 macOS,您可以按照相关文档进行操作。
结论
如果我们想要在本地(同一台计算机)或远程计算机上运行某些已定义的命令(命令块),我们可以使用 Invoke-Command。
猜你还喜欢
- 03-30 [玩转系统] 如何用批处理实现关机,注销,重启和锁定计算机
- 02-14 [系统故障] Win10下报错:该文件没有与之关联的应用来执行该操作
- 01-07 [系统问题] Win10--解决锁屏后会断网的问题
- 01-02 [系统技巧] Windows系统如何关闭防火墙保姆式教程,超详细
- 12-15 [玩转系统] 如何在 Windows 10 和 11 上允许多个 RDP 会话
- 12-15 [玩转系统] 查找 Exchange/Microsoft 365 中不活动(未使用)的通讯组列表
- 12-15 [玩转系统] 如何在 Windows 上安装远程服务器管理工具 (RSAT)
- 12-15 [玩转系统] 如何在 Windows 上重置组策略设置
- 12-15 [玩转系统] 如何获取计算机上的本地管理员列表?
- 12-15 [玩转系统] 在 Visual Studio Code 中连接到 MS SQL Server 数据库
- 12-15 [玩转系统] 如何降级 Windows Server 版本或许可证
- 12-15 [玩转系统] 如何允许非管理员用户在 Windows 中启动/停止服务
取消回复欢迎 你 发表评论:
- 精品推荐!
-
- 最新文章
- 热门文章
- 热评文章
[影视] 黑道中人 Alto Knights(2025)剧情 犯罪 历史 电影
[古装剧] [七侠五义][全75集][WEB-MP4/76G][国语无字][1080P][焦恩俊经典]
[实用软件] 虚拟手机号 电话 验证码 注册
[电视剧] 安眠书店/你 第五季 You Season 5 (2025) 【全10集】
[电视剧] 棋士(2025) 4K 1080P【全22集】悬疑 犯罪 王宝强 陈明昊
[软件合集] 25年6月5日 精选软件22个
[软件合集] 25年6月4日 精选软件36个
[短剧] 2025年06月04日 精选+付费短剧推荐33部
[短剧] 2025年06月03日 精选+付费短剧推荐25部
[软件合集] 25年6月3日 精选软件44个
[剧集] [央视][笑傲江湖][2001][DVD-RMVB][高清][40集全]李亚鹏、许晴、苗乙乙
[电视剧] 欢乐颂.5部全 (2016-2024)
[电视剧] [突围] [45集全] [WEB-MP4/每集1.5GB] [国语/内嵌中文字幕] [4K-2160P] [无水印]
[影视] 【稀有资源】香港老片 艺坛照妖镜之96应召名册 (1996)
[剧集] 神经风云(2023)(完结).4K
[剧集] [BT] [TVB] [黑夜彩虹(2003)] [全21集] [粤语中字] [TV-RMVB]
[实用软件] 虚拟手机号 电话 验证码 注册
[资源] B站充电视频合集,包含多位重量级up主,全是大佬真金白银买来的~【99GB】
[影视] 内地绝版高清录像带 [mpg]
[书籍] 古今奇书禁书三教九流资料大合集 猎奇必备珍藏资源PDF版 1.14G
[电视剧] [突围] [45集全] [WEB-MP4/每集1.5GB] [国语/内嵌中文字幕] [4K-2160P] [无水印]
[剧集] [央视][笑傲江湖][2001][DVD-RMVB][高清][40集全]李亚鹏、许晴、苗乙乙
[电影] 美国队长4 4K原盘REMUX 杜比视界 内封简繁英双语字幕 49G
[电影] 死神来了(1-6)大合集!
[软件合集] 25年05月13日 精选软件16个
[精品软件] 25年05月15日 精选软件18个
[绝版资源] 南与北 第1-2季 合集 North and South (1985) /美国/豆瓣: 8.8[1080P][中文字幕]
[软件] 25年05月14日 精选软件57个
[短剧] 2025年05月14日 精选+付费短剧推荐39部
[短剧] 2025年05月15日 精选+付费短剧推荐36部
- 最新评论
-
- 热门tag