PowerShell remote operation is the basis of remote management, and it provides another reliable and efficient method for centralized management of distributed systems.
Generally speaking, PowerShell remote operations rely on remote processing infrastructure, except for the few commands that have remote processing capabilities, such as Get-Service, Get-Process, Get-WMIObject, Get-EventLog, and Get-WinEvent. (It is not difficult to see that these commands "read" information on certain aspects of the system without making any changes. They rely on the .Net Framework for remote operations.)
Configure remote infrastructure
Remote infrastructure is the basis of remote operations. Only by correctly setting up the remote infrastructure can remote commands be executed correctly. Fortunately, although the remote infrastructure is important, it is very simple to configure. In fact, as long as the following two points are set, you can perform remote operations:
1. Run as an administrator. To perform remote operations, you must start the PowerShell command line as an administrator, even if the current user is a system administrator.
2. Enable remote processing mode. After enabling remote processing mode, commands that rely on remote infrastructure can be executed correctly. To enable remote processing mode, please use the enable-psremoting command. Some systems have enabled remote processing mode by default to check whether the remote processing mode is started, and can execute the new-pssession command. If a new session is created successfully, it means that the remote mode is started and the remote infrastructure configuration is successful.
Perform remote operations
Configure the remote infrastructure and you can perform remote operations. Remote operations mainly rely on several Session commands and Invoke-Command commands. There are three common usage scenarios:
Scene 1: Remote interactive conversation
This scenario is generally used to manually perform remote operations, enter commands, and view results. The method is very simple. The command to enter an interactive session is Enter-PSSession. When exiting, type Exit-PSSession or exit. During remote interactive operations, the entered commands run on the remote computer, just like entering and executing these commands directly on the remote computer. The execution results of variables and commands defined during the period are no longer available after exiting the interactive session.
Scenario 2: One-time execution of script blocks and script files
This scenario is to establish a temporary session on the local computer and the remote computer. Send the contents of the script block or script file to the remote computer for execution and send the result back to the local computer. This method is very efficient in execution and is a method recommended by PowerShell to execute remote commands. This method is recommended unless you need to share data in a session.
Scenario 3: script blocks and script files are executed in the named session
This kind of scenario is the most complex and powerful. The session will keep all defined variables, functions and scripts, imported modules and snap-ins, which are conducive to sharing of data. How to use it is as follows:
1. Define the session: Please use the new-pssession command to define the session, such as $session1 = new-pssession –computer server1. (If necessary, use the Credential parameter.)
2. Remotely execute scripts (or script files) in a session: Please use the Invoke-Command command to execute remote scripts, such as Invoke-Command -Session $session1 -ScriptBlock {dir c:\} or Invoke-Command -Session $session1 -FilePath .\dirDriveC.ps1
3. Obtain the result: You can assign the execution result to a variable, such as $sub = Invoke-Command -Session $session1 -ScriptBlock {dir c:\} or $sub = Invoke-Command -Session $session1 -FilePath .\dirDriveC.ps1
The subsequent commands can be continued with reference to Step 2 or 3, and all executed commands are as if they were executed in the same context.
Conclusion
The Shell or scripting language in the Internet age must have strong network processing capabilities, and PowerShell is such a language. Microsoft's products generally attach great importance to security, so PowerShell has many restrictions on network processing. For example, PowerShell cannot display interfaces on remote machines, and even programs with interfaces can only run in the background. We cannot have both bear's paw and fish, but fortunately we can always find some solutions. For example, the above problem can be achieved with the help of Microsoft's PsExec tool. For details, see Windows Sysinternals.