power shell远程_PowerShell远程管理02——Powershell远程管理的几种方式

本文详细介绍了PowerShell进行远程管理的五种方式,包括使用交互式会话、远程执行命令、运行脚本、建立持久连接以及使用其他支持远程执行的命令。通过示例展示了如何使用Enter-PSSession、Invoke-Command等命令进行远程管理,适用于系统管理员进行批量管理和维护工作。
摘要由CSDN通过智能技术生成

上一节,我们简单介绍了,PowerShell远程管理所依赖的三个服务。这一节我们来学习下PowerShell远程管理的几种方式。

上一节:PowerShell远程管理01——Powershell远程管理依赖的服务及配置

Powershell应该有五种远程管理的方式

?分别是:

使用交互式会话

使用远程执行命令 (Invoke-command -ScriptBlock {})

使用远程运行脚本(Invoke-command -FilePath )

建立持久连接 (New-PSSession)

其他支持远程执行的命令( [-ComputerName ])

1、使用交互式会话

?使用“Enter-PSSession RemoteComputer”启动一个交互式会话,然后可以在会话中执行Powershell命令,如同在此服务本地执行Powershell一样。

# 基本结构如下

Enter-PSSession -Credential $Credential

Exit-PSSession

实际操作记录如下:

PS C:> Enter-PSSession -ComputerName "sz-test1119.test.local"

[sz-test1119.test.local]: PS C:UsersxxxDocuments>

[sz-test1119.test.local]: PS C:UsersxxxDocuments> cd c:[sz-test1119.test.local]: PS C:>

[sz-test1119.test.local]: PS C:>

[sz-test1119.test.local]: PS C:> Get-Service "winrm"

Status Name DisplayName

------ ---- -----------

Running winrm Windows Remote Management (WS-Manag...

[sz-test1119.test.local]: PS C:> Exit-PSSession

2、使用远程执行命令 (Invoke-command -ScriptBlock {})

?借助于“Invoke-command”的“-ComputerName”参数和“-ScriptBlock”参数直接在本地写Powershell命令块在远程服务器执行.

# 基本命令结构

Invoke-Command -ComputerName Server01, Server02 -ScriptBlock {Get-UICulture} -Credential $Credential

PS C:> Invoke-Command -Credential $cred -ComputerName "sz-test1122.test.local","sz-test1119.test.local" -ScriptBlock {get-host}

PSComputerName : sz-test1119.test.local

RunspaceId : daa5238c-3593-4268-89e7-a01bab5bc3e4

Name : ServerRemoteHost

Version : 1.0.0.0

InstanceId : 0490d6f8-4f92-42f7-a065-e734dc73b6a7

UI : System.Management.Automation.Internal.Host.InternalHostUserInterface

CurrentCulture : zh-CN

CurrentUICulture : zh-CN

PrivateData :

DebuggerEnabled : True

IsRunspacePushed : False

Runspace : System.Management.Automation.Runspaces.LocalRunspace

PSComputerName : sz-test1122.test.local

RunspaceId : 2fd4b586-14f9-45b9-8feb-adae5d9af47f

Name : ServerRemoteHost

Version : 1.0.0.0

InstanceId : e5947824-21c2-4ddd-8204-3c4bb3a6855a

UI : System.Management.Automation.Internal.Host.InternalHostUserInterface

CurrentCulture : zh-CN

CurrentUICulture : zh-CN

PrivateData :

DebuggerEnabled : True

IsRunspacePushed : False

Runspace : System.Management.Automation.Runspaces.LocalRunspace

3、使用远程运行脚本(Invoke-command -FilePath )

借助于“Invoke-command”的“-ComputerName”参数和“-FilePath”参数调用本地的脚本在远程服务器执行.

基本结构

Invoke-Command -ComputerName Server01, Server02 -FilePath c:ScriptsDiskCollect.ps1 Credential $Credential

注:可能需要修改远程主机的脚本执行策略,可以借助“2”中的方法查询及修改脚本执行策略。

# 查询远程主机的脚本执行策略

Invoke-Command -Credential $cred -ComputerName "sz-test1122.test.local","sz-test1119.test.local" -ScriptBlock { Get-ExecutionPolicy }

# 修改脚本执行策略

Invoke-Command -Credential $cred -ComputerName "sz-test1122.test.local","sz-test1119.test.local" -ScriptBlock { Set-ExecutionPolicy RemoteSigned }

命令执行结果

PS C:> Invoke-Command -Credential $cred -ComputerName "sz-test1122.test.local","sz-test1119.test.local" -FilePath {C:UsersestDesktopget-host.ps1}

PSComputerName : sz-test1119.test.local

RunspaceId : daa5238c-3593-4268-89e7-a01bab5bc3e4

Name : ServerRemoteHost

Version : 1.0.0.0

InstanceId : 0490d6f8-4f92-42f7-a065-e734dc73b6a7

UI : System.Management.Automation.Internal.Host.InternalHostUserInterface

CurrentCulture : zh-CN

CurrentUICulture : zh-CN

PrivateData :

DebuggerEnabled : True

IsRunspacePushed : False

Runspace : System.Management.Automation.Runspaces.LocalRunspace

PSComputerName : sz-test1122.test.local

RunspaceId : 2fd4b586-14f9-45b9-8feb-adae5d9af47f

Name : ServerRemoteHost

Version : 1.0.0.0

InstanceId : e5947824-21c2-4ddd-8204-3c4bb3a6855a

UI : System.Management.Automation.Internal.Host.InternalHostUserInterface

CurrentCulture : zh-CN

CurrentUICulture : zh-CN

PrivateData :

DebuggerEnabled : True

IsRunspacePushed : False

Runspace : System.Management.Automation.Runspaces.LocalRunspace

4、建立持久连接 (New-PSSession)

?可以使用“New-PSSession”建立一个PS的会话,然后通过“Invoke-Command”的“Session”参数去引用。

基本结构

$s = New-PSSession -ComputerName Server01, Server02

Invoke-Command -Session $s {$h = Get-HotFix}

Invoke-Command -Session $s {$h | where {$_.InstalledBy -ne "NTAUTHORITYSYSTEM"}}

命令执行结果

PS C:> $session = New-PSSession -ComputerName "sz-test1122.test.local","sz-test1119.test.local"

PS C:> Invoke-Command -Session $session {Get-Service "WinRM"}

Status Name DisplayName PSComputerName

------ ---- ----------- --------------

Running WinRM Windows Remote Management (WS-Manag... sz-test1122.test.local

Running WinRM Windows Remote Management (WS-Manag... sz-test1119.test.local

PS C:> Invoke-Command -Session $session {Get-process "winlogon"}

Handles NPM(K) PM(K) WS(K) CPU(s) Id SI ProcessName PSComputerName

------- ------ ----- ----- ------ -- -- ----------- --------------

212 11 2360 2556 0.13 596 1 winlogon sz-test1119.test.local

260 11 1980 3280 0.05 2580 2 winlogon sz-test1119.test.local

157 9 2540 8832 1.05 604 1 winlogon sz-test1122.test.local

187 9 2400 8172 0.67 5380 4 winlogon sz-test1122.test.local

5、其他支持远程执行的命令( [-ComputerName ])

?随Powershell一起安装的cmdlet,有些cmdlet本身有“-ComputerName ”或者其他的参数,可以直接远程执行命令。

如不依赖“WinRM”服务的“Get-Service”、“Get-Process”,“Set-Service”,以及依赖“WinRM”服务的“Invoke-Command”等。

# 基本命令结构

[-ComputerName ] [-Parameter1] [-Parameter2]…

例子1:查看远程主机上的“WinRM”服务状态

PS C:> Get-Service -ComputerName "sz-test1119.test.local" -Name "WinRM"

Status Name DisplayName

------ ---- -----------

Stopped WinRM Windows Remote Management (WS-Manag...

例子2:查看远程主机上的“winlogon”进程状态

PS C:> Get-Process -ComputerName ‘sz-test1119.test.local‘ -Name "winlogon"

Handles NPM(K) PM(K) WS(K) CPU(s) Id SI ProcessName

------- ------ ----- ----- ------ -- -- -----------

215 11 2352 2580 600 0 winlogon

264 11 2184 2944 3552 0 winlogon

例子3:查看远程主机上的磁盘分区信息

PS C:> Get-CimInstance CIM_DiskPartition -ComputerName "sz-test1119.test.local"

Name NumberOfBlocks BootPartition PrimaryPartition Size Index PSComputerName

---- -------------- ------------- ---------------- ---- ----- --------------

磁盘 #0,分区 #0 1083392 False False 554696704 0 sz-test1119....

磁盘 #0,分区 #1 202752 True True 103809024 1 sz-test1119....

磁盘 #0,分区 #2 81354667 False True 41653589504 2 sz-test1119....

磁盘 #0,分区 #3 1206272 False False 617611264 3 sz-test1119....

例子4:查看远程主机上的启动分区信息

PS C:> Get-CimInstance CIM_DiskPartition -ComputerName "sz-test1119.test.local" | ? {$_.BootPartition -eq "True"}

Name NumberOfBlocks BootPartition PrimaryPartition Size Index PSComputerName

---- -------------- ------------- ---------------- ---- ----- --------------

磁盘 #0,分区 #1 202752 True True 103809024 1 sz-test1119....

PowerShell 是 Windows 操作系统中的一个很强大的命令行工具,它提供了许多方便的操作和管理功能。具有大量的内置功能,这使得 PowerShell 被广泛用于自动化系统管理、配置网络和管理 IT 资源等任务。 在 Windows 操作系统中,用户通常需要以管理员身份运行 PowerShell 以实现系统管理员的权限,这将赋予用户操作系统内部的更高级别权限。这种管理员权限对于一些系统级别的任务来说是必不可少的。 以管理员身份运行 PowerShell 有三种方法: 1. 在 Windows 操作系统中搜索 PowerShell,右键单击 PowerShell 应用程序图标,然后选择“以管理员身份运行”。 2. 打开“开始”菜单,搜索“PowerShell”,右键单击 PowerShell 应用程序图标,然后选择“以管理员身份运行”。 3. 使用 Windows 键 + X 快捷键,在弹出的快捷菜单中选择 Windows PowerShell(管理员)。 不管哪种方法,要想以管理员身份运行 PowerShell ,需要用户的账户拥有管理员权限或已运行的用户是系统管理员。 在 PowerShell 中以管理员身份使用系统命令时,能够达到很好的效果。这样可以对系统进行更深入的操作、创建和管理用户、安装外部程序并跨 Windows 系统进行管理。在系统维护、升级和修复方面,这种管理员权限显得尤为重要。 总之,以管理员身份运行 PowerShell 可以更轻松地完成系统维护、管理和安全控制任务,能够对 Windows 操作系统实现更高级别的管理。因此,系统管理员必须学如何以管理员身份运行 PowerShell
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值