标签:简单介绍 exec stop hotfix private blog 基本 远程服务 test
上一节,我们简单介绍了,PowerShell远程管理所依赖的三个服务。这一节我们来学习下PowerShell远程管理的几种方式。
上一节:PowerShell远程管理01——Powershell远程管理依赖的服务及配置
Powershell应该有五种远程管理的方式
?分别是:
?使用“Enter-PSSession RemoteComputer”启动一个交互式会话,然后可以在会话中执行Powershell命令,如同在此服务本地执行Powershell一样。
# 基本结构如下
Enter-PSSession <Server> -Credential $Credential
<PowerShellCommand>
Exit-PSSession
实际操作记录如下:
PS C:\> Enter-PSSession -ComputerName "sz-test1119.test.local"
[sz-test1119.test.local]: PS C:\Users\xxx\Documents>
[sz-test1119.test.local]: PS C:\Users\xxx\Documents> 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
?借助于“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
借助于“Invoke-command”的“-ComputerName”参数和“-FilePath”参数调用本地的脚本在远程服务器执行.
基本结构
Invoke-Command -ComputerName Server01, Server02 -FilePath c:\Scripts\DiskCollect.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:\Users\test\Desktop\get-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
?可以使用“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 "NTAUTHORITY\SYSTEM"}}
命令执行结果
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
?随Powershell一起安装的cmdlet,有些cmdlet本身有“-ComputerName ”或者其他的参数,可以直接远程执行命令。
如不依赖“WinRM”服务的“Get-Service”、“Get-Process”,“Set-Service”,以及依赖“WinRM”服务的“Invoke-Command”等。
# 基本命令结构
<PSCommand> [-ComputerName <RemoteComputer>] [-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学习笔记
PowerShell远程管理02——Powershell远程管理的几种方式
标签:简单介绍 exec stop hotfix private blog 基本 远程服务 test
原文地址:https://blog.51cto.com/3chou/2562588