Getting started with PowerShell (6): Remote Operations

Source: Internet
Author: User

Remote PowerShell operation is the basis of remote management. It provides a reliable and efficient method for centralized management of distributed systems.

Generally, remote PowerShell operations depend on the basic structure of remote processing, except for a few commands with remote processing capabilities, such as Get-Service, Get-Process, Get-WMIObject, Get-EventLog, and Get-WinEvent. It is not hard to see that these commands "read" some information of the system without making any changes. They depend on. Net Framework for remote operations .)

Topic recommendation: PowerShell getting started knowledge

Configure remote infrastructure

Remote infrastructure is the basis for remote operations. Only by Correctly Setting remote infrastructure can remote commands be correctly executed. Fortunately, although the remote infrastructure is important, the configuration is very simple. In fact, you only need to set the following two points to perform remote operations:

1. Run as 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 the remote processing mode. After the remote processing mode is enabled, commands that depend on the remote infrastructure can be correctly executed. Use the enable-psremoting command to enable the remote processing mode. Some systems have enabled the remote processing mode by default. Check whether the remote processing mode is enabled. You can run the new-pssession command. If a new session is successfully created, it indicates that the remote mode is started and the remote infrastructure is configured successfully.

Perform remote operations

After configuring the remote infrastructure, you can perform remote operations. Remote Operations mainly rely on several Session commands) and the Invoke-Command. There are three common scenarios:

Scenario 1: remote interactive sessions

This scenario is generally used for manual remote operations, input commands, and view results. The method is simple. Enter-PSSession is the command to Enter the interactive session. When you Exit, Enter exit-PSSession or Exit. During a remote interactive operation, the input commands run on the remote computer, just as they are directly input and executed on the remote computer. The execution results of the variables and commands defined in the period are no longer available after the interactive session is exited.

Scenario 2: one-time execution of script blocks and script files

In this scenario, a temporary session is established between a local computer and a remote computer. Send the content of the script block or script file to a remote computer for execution and send the result back to the local computer. This method is highly efficient and is recommended by PowerShell to execute remote commands. Unless you need to share data in a session, we recommend that you use this method.

Scenario 3: script blocks and script files are executed in the naming session.

This scenario is the most complex and powerful. Sessions maintain all Defined variables, functions, and scripts. Imported modules and management units facilitate data sharing. The usage is as follows:

1. Define a session: use the new-pssession command to define a session, for example, $ session1 = new-pssession-computer server1. Use the Credential parameter if necessary .)

2. remotely execute scripts or script files in sessions): Use the Invoke-Command to execute remote scripts, such as Invoke-Command-Session $ session1-ScriptBlock {dir c: \} or Invoke-Command-Session $ session1-FilePath. \ dirDriveC. ps1

3. Get the result: You can assign the execution result to a variable, for example, $ sub = Invoke-Command-Session $ session1-ScriptBlock {dir c: \} or $ sub = Invoke-Command-Session $ session1-FilePath. \ dirDriveC. ps1

The subsequent commands can be continued by referring to steps 2 or 3. All the executed commands are as if they were executed in the same context.

Conclusion

In the Internet era, Shell or script languages must have strong network processing capabilities. PowerShell is such a language. Microsoft products generally take security seriously, so PowerShell has many restrictions on network processing. For example, PowerShell cannot display the interface on a remote machine. Even a program with an interface can only run in the background. Bear's paw and fish cannot have both sides. Fortunately, we can always find some solutions. For example, the above problem can be solved using Microsoft's javasxec tool. For details, see Windows Sysinternals.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.