Windows Server Remote Execution command (POWERSHELL+WINRM)

Source: Internet
Author: User
Tags argumentlist fully qualified domain name

Windows Remote Management (WinRM) is a Microsoft implementation of the Ws-management protocol. The protocol is a firewall-friendly standard protocol based on Simple Object Access Protocol (SOAP) that enables the interoperability of hardware and operating systems from different vendors. The Ws-management protocol is developed by the hardware and software manufacturer community and is used as a public standard to remotely Exchange Management data with any computer device that implements the protocol.

Use PowerShell to remotely administer the server to enable WinRM on the remote server.

First, the Knowledge Reserve:

1. WinRM related Operations

To turn on the WinRM service:

Enable-psremoting–force

Prevent the local computer from receiving remote commands (the WinRM service is not stopped):

Disable-psremoting–force

To view the WinRM service listening information:

WinRM Enumerate Winrm/config/listener

WinRM2.0 the default port 5985 (HTTP port) or 5986 (https port).

To delete WinRM http listening:

WinRM delete Winrm/config/listener? Address=*+transport=http

Re-establish HTTP listening:

WinRM create Winrm/config/listener? Address=*+transport=http

The WinRM service changes the listening port:

Set-item-force Wsman:\localhost\listener\listener*\port 5985

To view the configuration of WinRM:

WinRM get Winrm/config

To view the port listening status:

Netstat-nao | Findstr "5985"

650) this.width=650; "Src=" https://s1.51cto.com/wyfs02/M02/96/19/wKioL1kdBBzCxE20AAGJt42HbX0069.png-wh_500x0-wm_ 3-wmp_4-s_2958921877.png "title=" 1.png "alt=" Wkiol1kdbbzcxe20aagjt42hbx0069.png-wh_50 "/>

2. PowerShell Script Save Credential method

Using the get-credential command to interactively enter credentials (user name + password), you can first save the credentials to a variable, such as:

$cred = get-credential

650) this.width=650; "Src=" https://s5.51cto.com/wyfs02/M01/96/19/wKioL1kdBCjSkN7xAABGBTheQDA354.png-wh_500x0-wm_ 3-wmp_4-s_439647423.png "title=" 2.png "alt=" Wkiol1kdbcjskn7xaabgbtheqda354.png-wh_50 "/>

The object type name of the $cred is: System.Management.Automation.PSCredential, where both password and username are properties, so you can create a new object of that type. It is important to note here that the password type is securestring,username type string, so you need to convert the plaintext password to a secure string, and you can use the convertto-securestring command.

Example of a non-interactive Save credential Code:

$account = "Administrator" $password = ' 123456 ' $secpwd = convertto-securestring $password-asplaintext-force $cred = New-object system.management.automation.pscredential-argumentlist $account, $secpwd

3, Invoke-command

Invoke-command runs commands on both local and remote computers and returns all output from the command, including errors. With a Invoke-command command, you can run commands on multiple computers. Some of the parameters are:

-port <int>

Specifies the network port that is used for this command on the remote computer. Specifies the new port in case the WinRM default listener port is changed.

-computername <string[]>

Specifies the computer on which this command is run. The default value is the local computer.

When you use the ComputerName parameter, Windows PowerShell creates a temporary connection that is used only to run the specified command and then shuts down. If you need a persistent connection, use the Session parameter.

Type the NETBIOS name, IP address, or fully qualified domain name of one or more computers in a comma-separated list. To specify a local computer, type the computer name, "localhost", or a period (.).

To use an IP address in the value of the ComputerName parameter, the command must include the credential parameter. In addition, you must configure an HTTPS transport for the computer, or you must include the IP address of the remote computer in the WinRM trustedhosts list on the local computer.

-session <PSSession[]>

Run this command in the specified Windows PowerShell session (PSSession).

You can establish a persistent connection to a remote computer by creating Pssession,windows PowerShell.

-credential <PSCredential>

Specifies the user account that has permission to perform this operation. The default value is the current user.

-command/-scriptblock <scriptblock>

Specifies the command to run. Enclose the command in curly braces ({}) to form a script block.

-filepath <string>

Runs the specified local script on one or more remote computers.

-asjob

Run the command as a background job on the remote computer. Use this parameter to run a command that takes a long time to complete.

When you use AsJob, this command returns the object that represents the job, and then displays a command prompt. To manage jobs, use the job cmdlet. To get job results, use Receive-job.

-throttlelimit <int>

Specifies the maximum number of concurrent connections that can be established to run this command. If you omit this parameter or enter a value of 0, the default value of 32 is used.

4, PSSession

Pssession is a Windows PowerShell session. Use pssession when you need a continuous connection to the remote computer. Pssession Related commands:

Enter-pssession

Initiates an interactive session with the remote computer.

New-pssession

Creates a pssession and returns an object that represents the Pssession. You can save the object in a variable.

Get-pssession

Gets the pssession created in the current session. Get-pssession returns an object of the same type as the object returned by New-pssession.

Remove-pssession

Delete the PSSession and release the resource it is using.

5. Add the computer name to the Trustedhosts list

To add all computers to the trusted hosts list, use the following command:

Set-item Wsman:localhost\client\trustedhosts-force-value *

You can also use a wildcard character (*) to add all computers in a specific domain to the trusted hosts list.

For example, the following command adds all the computers in the Fabrikam domain to the list of trusted hosts.

Set-item wsman:localhost\client\trustedhosts *.fabrikam.com

To add the IP address of a specific computer to the list of trusted hosts, use the following command format (IP support wildcard *):

Set-item wsman:\localhost\client\trustedhosts-value "<ip address>[,<ip Address>]"

To view the list of trusted hosts, use the following command:

Get-item wsman:\localhost\client\trustedhosts

650) this.width=650; "Src=" https://s4.51cto.com/wyfs02/M00/96/19/wKioL1kdBDbBLJikAABZOEO_R5Q583.png-wh_500x0-wm_ 3-wmp_4-s_3331756484.png "title=" 3.png "alt=" Wkiol1kdbdbbljikaabzoeo_r5q583.png-wh_50 "/>


Second, remote operation of common scenarios

Scenario One: Remote Interactive session

This scenario is typically used to manually perform remote operations, enter commands, and view the results. The method is simple. The command to enter the interactive session is Enter-pssession, and you can type Exit-pssession or exit when you exit. During a remote interactive operation, the commands you enter are run on the remote computer as if they were entered and executed 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 Example: # User input credentials (user name + password) $cred =get-credential# establish a remote interactive session enter-pssession-computername 192.168.21.1-credential $cred


650) this.width=650; "Src=" https://s5.51cto.com/wyfs02/M00/96/19/wKiom1kdBF-zRbLbAAApoTzX7cE469.png-wh_500x0-wm_ 3-wmp_4-s_654629191.png "title=" 4.png "alt=" Wkiom1kdbf-zrblbaaapotzx7ce469.png-wh_50 "/>


Scenario Two: One-time execution of script blocks, script files

In this scenario, a temporary session is established on the local computer and the remote computer. Sends the contents of the script block or script file to the remote computer for execution and sends the results back to the local computer. This method of execution is highly efficient and is the recommended way for PowerShell to execute remote commands. This method is recommended unless you need to share data in a session.

Scenario Two example, # User input credentials (user name + password) $cred =get-credential# remote execute command invoke-command-computername 192.168.21.1-credential $cred- command {dir c:/}invoke-command-computername 192.168.21.1-credential $cred-scriptblock {dir c:\}# remote Execute script echo "dir c: \" > Dirdrivec.ps1invoke-command-computername 192.168.21.1-credential $cred-filepath. \dirdrivec.ps1

650) this.width=650; "Src=" https://s5.51cto.com/wyfs02/M02/96/19/wKioL1kdBEOgG1zGAAAlVesM1jw732.png-wh_500x0-wm_ 3-wmp_4-s_400029066.png "title=" 5.png "alt=" Wkiol1kdbeogg1zgaaalvesm1jw732.png-wh_50 "/>

Scenario Two example (multiple remote hosts), # User input credentials (user name + password) $cred =get-credential# batch execution of commands to multiple remote hosts Invoke-command-computername 192.168.21.1,192.168.21.4,192.168.21.7-credential $cred-scriptblock {dir c:\}# set the number of concurrent connections to 1 Invoke-command- ComputerName 192.168.21.1,192.168.21.4,192.168.21.7-credential $cred-throttlelimit 1-scriptblock {dir c \}


Scenario Three: script block, script file in a naming session Line

1. Define the session: Use the New-pssession command to define the session, such as $session1 = New-pssession-computername Server1. (if necessary, use the credential parameter.) )

2. Remotely execute script (or script file) in session: Use the Invoke-command command to execute a remote script, such as Invoke-command-session $session 1-scriptblock {dir: \} or Invoke-command-session $session 1-filepath. \dirdrivec.ps1

3. Get results: You can assign execution results to variables, such as $sub = Invoke-command-session $session 1-scriptblock {dir: \} or $sub = Invoke-command-session $sess Ion1-filepath. \dirdrivec.ps1

Subsequent commands can be executed with reference to step 2 or 3, and all executed commands appear to be executed in the same context.

Scenario three examples, # user input credentials (user name + password) $cred =get-credential# create pssession and assign to variable $session1 = new-pssession-computername 192.168.21.1- Credential $cred # Remote EXECUTE command (persistent) invoke-command-session $session 1-scriptblock {$a = "Hello World"}invoke-command-session $ Session1-scriptblock {$a}

650) this.width=650; "Src=" https://s3.51cto.com/wyfs02/M00/96/19/wKioL1kdBGzz--vqAAB_7WeKz-4673.png-wh_500x0-wm_ 3-wmp_4-s_261082102.png "title=" 6.png "alt=" Wkiol1kdbgzz--vqaab_7wekz-4673.png-wh_50 "/>

 scene three examples (multiple remote hosts), #  user input credentials (user name + password) $cred =get-credential#  Create pssession for multiple remote hosts and assign a value to the variable $session_many = new-pssession -computername  192.168.21.1,192.168.21.4,192.168.21.7 -credential  $cred #  Batch Execution of commands to multiple remote hosts invoke-command - session  $session _many -throttlelimit 1 -scriptblock {dir c:\} 
Scenario three examples (script mode), $account = "Administrator" $password = ' 123456 ' # convert password to SECURESTRING$SECPWD = convertto-securestring $ password-asplaintext-force# New Pscredential Object $cred = New-object System.Management.Automation.PSCredential- ArgumentList $account, $SECPWD # Remote host list assignment to variable [string[]] $computername = "192.168.21.1", "192.168.21.4", "192.168.21.7" # Create Pssession$session_many = New-pssession-computername $computername-credential $cred # Bulk Remote execute command invoke-command- Session $session _many-throttlelimit 1-scriptblock {dir c \}


Resources:

Https://technet.microsoft.com/zh-cn/library/dd347578.aspx

Https://technet.microsoft.com/zh-cn/library/hh847839.aspx

Https://technet.microsoft.com/zh-cn/library/dd347642.aspx

Http://www.cnblogs.com/ceachy/archive/2013/02/20/PowerShell_Remoting.html


This article is from the "Start Linux blog" blog, make sure to keep this source http://qicheng0211.blog.51cto.com/3958621/1926913

Windows Server Remote Execution command (POWERSHELL+WINRM)

Related Article

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.