stay linux We can use SSH Perform various remote operations , The same in windows On the platform, we can PowerShell Gain similar ability . This article will introduce the
PowerShell Basic information for performing remote operations . Want to know SSH Students of remote operation , You can refer to 《SSH Remote task execution
<http://www.cnblogs.com/sparkdev/p/6842805.html>》.

Basics

MS Defined a name WS-Management Agreement of , This protocol provides an open standard for remote exchange management data of computer equipment . stay Windows On the platform ,MS adopt
Windows Remote management service (Windows Remote Management service, abbreviation WinRM) Achieved WS-Management
agreement . That's what we can do PowerShell The basis for performing remote operations , because PowerShell Is through WinRM Services for remote operations .

inspect WinRM service

We can check it with the following command WinRM Status of the service :
Get-Service WinRM


Server version Windows System default is start WinRM Serving , The normal system does not start the service by default . Next we'll show you how to start the service in the right posture .

Configure the system to accept remote commands

be similar to SSH Clients of / Server architecture , Only need to be configured on the host receiving the remote command WinRM Just the service . In fact, let's WinRM Service listens for a port , Follow SSH
daemon It's the same !
Start with administrator privileges PowerShell, Execute the following command :
Enable-PSRemoting –Force
Then check WinRM Service should be running :



perhaps :



Firewall rules have also changed :



Enable-PSRemoting Not only did the command start WinRM service , And set up the firewall rules for us .

If your computer is already joined to a domain , Then the above configuration is OK . Trust settings are also required for computers that are not joined to the domain , Then restart WinRM service :
Set-Item wsman:\localhost\client\trustedhosts * Restart-Service WinRM
Test remote connection

PowerShell A command is also provided to test whether the remote host has turned on the remote operation service :
Test-WsMan xxx.xxx.xxx.xxx


The command in the above figure successfully returned , Indicates that the service that allows remote access has been configured on the remote host . If an error message is returned , The remote host does not open the remote access service .

Create a remote connection Session

This is similar to SSH Remote connection for . use SSH The client establishes a connection to the server (session), Then perform various operations . This is SSH
Basic usage ,PowerShell There's a similar support that allows us to do the same thing .
Enter-PSSession -ComputerName my-svr -Credential nickli


Enter the password to establish the connection :



Next, you can perform tasks on the remote host !

Remote execution of a single command

Can be set up with remote host session It's a good place to start . But most of the time, we just need to execute a single command , Even in a script , What shall I do? ?
We can go through Invoke-Command command , And specify -ScriptBlock Parameters to complete this task :
Invoke-Command -ComputerName cd-lsr-svr -ScriptBlock { Get-Service WinRM }
-credential nickli
Execute the above command , And enter the user's password :



Although the order was executed , But the flaws are obvious : Password required , Cannot be written to script for automation .
Use the following method to write the password to the command ( So it can be automated ):
$Username = 'xxxx' $Password = 'yyyy' $pass = ConvertTo-SecureString
-AsPlainText$Password -Force $Cred = New-Object
System.Management.Automation.PSCredential -ArgumentList$Username,$pass Invoke
-Command -ComputerName cd-lsr-svr -ScriptBlock { Get-Service WinRM } -credential
$Cred
Remote execution of multiple commands

Sometimes we need to execute multiple related commands in succession , For example, the result of the previous command will be used in the latter command . At this time, the above method will not work . Need to pass session To handle the connection between commands :
Invoke-Command -ComputerName myserver -ScriptBlock {$p = Get-Process
PowerShell} Invoke-Command -ComputerName myserver -ScriptBlock {$p
.VirtualMemorySize}$s = New-PSSession -ComputerName myserver Invoke-Command
-Session$s -ScriptBlock {$p = Get-Process PowerShell} Invoke-Command -Session $s
-ScriptBlock {$p.VirtualMemorySize}


When we execute the first two commands directly , Because of the $p No value , So we can't get results . The last two lines of command go through the same session
Executed , So they can share variables , And finally get the result .

Remote script execution

This paper introduces the operation of remote command execution , It's much easier to introduce remote script execution . In fact, this is the core of automation :
Invoke-Command -ComputerName cm-12r2 -FilePath .\task.ps1
I created a script file locally task.ps1,task.ps1 The script in creates a test.txt file , And PowerShell
Version information of is added to this file . Run the above command :



Then on the remote machine, see if there is any file creation :



Everything looks good , It's just that our scripts will be more complex in practice !

Perform the same operation on multiple machines

Doing the same operation on multiple hosts at the same time will multiply the efficiency , This can be done remotely :



The commands in the above figure are executed on two remote hosts at the same time , In fact, it's for ComputerName Attribute specifies multiple targets !
come again : The real power is to execute script files :



this time , We executed script files on two remote hosts at the same time task.ps1.
If there are many target hosts , You can also write them to a file , Then reference it in the following way :
Invoke-Command -ComputerName (Get-Content Machines.txt)
Remote copy file

finally ,PowerShell stay 5.x Remote copy of files is now supported in version of ! Let's see first demo:
$mySession = new-PSSession -ComputerName xxxxxx Copy-Item -Path .\task.ps1
-Destination C:\task.ps1 -ToSession$mySession
The above command sets the task.ps1 File copy to remote host xxxxxx Of C Under the root directory . be careful Copy-Item The command uses a
Session Object as parameter , The name of this parameter is ToSession. We can guess , The direction of file transfer is determined by this parameter : use ToSession
Local files will be delivered to the remote host ; use FromSession The file on the remote host will be passed to the local machine .
Let's see how to copy the entire directory :
$mySession = new-PSSession -ComputerName xxxxxx Copy-Item -Path .\PowerShell
-Destination C:\PowerShell -ToSession$mySession -Recurse


Note that the directory copy operation should add Recurse parameter .
The above two operations are to copy a file and a directory to a remote host , Next, we will copy the files or folders on the remote host to the local computer :
$mySession = new-PSSession -ComputerName xxxxxx Copy-Item -Path C:\task.ps1
-Destination F:\temp\task.ps1 -FromSession$mySession Copy-Item -Path
C:\PowerShell -Destination F:\temp -FromSession$mySession -Recurse
Note that the parameters used here are FromSession, So the Destination Local path specified . Execute the above command , See if the content has been copied to the local
F:\temp Under the catalogue !

summary

This paper briefly introduces the use of PowerShell The basis of remote operation and some commonly used methods . on the whole , Common operations and SSH It's basically the same . The command to copy files remotely is
PowerShell Only the latest version supports , Be sure to determine the environment PowerShell Version of .