Running PowerShell commands on a remote computer

Source: Internet
Author: User

Running PowerShell commands on a remote computer



Check if PowerShell remoting is enabled


Run PowerShell as an administrator on Windows Server R2, and then enter

Enter-pssession-computername localhost


Return as follows:

650) this.width=650; "title=" clip_image002 "style=" border-top:0px;border-right:0px;background-image:none; border-bottom:0px;padding-top:0px;padding-left:0px;border-left:0px;margin:0px;padding-right:0px; "Alt=" Clip_ image002 "src=" http://s3.51cto.com/wyfs02/M00/8B/D3/wKioL1haNE7DXZubAABwMYUUXho574.jpg "height=" 126 "border=" 0 "/ >

Indicates that the PowerShell remoting feature is not enabled by default for this version.


On Windows Server R2, return the following:

650) this.width=650; "title=" clip_image004 "style=" border-top:0px;border-right:0px;background-image:none; border-bottom:0px;padding-top:0px;padding-left:0px;border-left:0px;margin:0px;padding-right:0px; "Alt=" Clip_ image004 "src=" http://s3.51cto.com/wyfs02/M02/8B/D7/wKiom1haNE_BaiXtAAA6vI5vuSw699.jpg "height=" "border=" 0 "/ >


On Windows Server 2016, return the following:

650) this.width=650; "title=" clip_image005 "style=" border-top:0px;border-right:0px;background-image:none; border-bottom:0px;padding-top:0px;padding-left:0px;border-left:0px;margin:0px;padding-right:0px; "Alt=" Clip_ image005 "src=" Http://s3.51cto.com/wyfs02/M02/8B/D3/wKioL1haNE-wswxrAAAeaLgd-YE214.png "height=" 98 "border=" 0 "/ >

Description on 2012 and 2016, the PowerShell remoting feature is enabled by default.



Enable PowerShell Remoting


On Windows Server R2, run PowerShell as an administrator, and then enter the following script:

Enable-psremoting-force


This command initiates the WinRM service, is set to start automatically on your system, and creates a firewall policy that allows incoming connections.


Verify that the startup was successful:

650) this.width=650; "title=" clip_image007 "style=" border-top:0px;border-right:0px;background-image:none; border-bottom:0px;padding-top:0px;padding-left:0px;border-left:0px;margin:0px;padding-right:0px; "Alt=" Clip_ image007 "src=" http://s3.51cto.com/wyfs02/M00/8B/D7/wKiom1haNFCD4jTxAACHbuHldSc033.jpg "height=" 165 "border=" 0 "/ >



Configuring a trusted Host


If your host is not in a domain environment, but in a workgroup environment, you can configure a trusted list that can access the host remotely.

Set-item wsman:\localhost\client\trustedhosts *

You can also replace * with a trusted LAN host 192.168.11.*


Then, restart the WinRM service to make it effective:

Restart-service WinRM



Test connection


From the client accessing the remote host, test your configuration with Test-wsman. This command tests whether the WinRM service is running on the remote host. If completed successfully, you will know that WinRM is enabled and can communicate with each other.

Test-wsman szsql05


If the command completes successfully, you will see the WinRM service information for the remote computer. If the command fails, you will see an error message.

650) this.width=650; "title=" clip_image009 "style=" border-top:0px;border-right:0px;background-image:none; border-bottom:0px;padding-top:0px;padding-left:0px;border-left:0px;margin:0px;padding-right:0px; "Alt=" Clip_ image009 "src=" http://s3.51cto.com/wyfs02/M02/8B/D3/wKioL1haNFHTl8RdAAB2O7yIRV4001.jpg "height=" 176 "border=" 0 "/ >



Execute remote command


Use Invoke-command to run commands on the remote system. The syntax is as follows:

Invoke-command-computername computer-scriptblock {Command}-credential USERNAME

Computer for the name of the computer;

command is the order you want to run;

Username is the user name of the command you want to run on the remote computer. You will be prompted to enter the password for that user name.


For example, to view the contents of the C packing directory on the remote computer szsql05 with user Dx\xucy, you can enter the following command:

Invoke-command-computername szsql05-scriptblock {Get-childitem c \}-credential Dx\xucy

650) this.width=650; "title=" clip_image011 "style=" border-top:0px;border-right:0px;background-image:none; border-bottom:0px;padding-top:0px;padding-left:0px;border-left:0px;margin:0px;padding-right:0px; "Alt=" Clip_ image011 "src=" http://s3.51cto.com/wyfs02/M00/8B/D3/wKioL1haNFKAjZtcAABIx65F5kg194.jpg "height=" 191 "border=" 0 "/ >


After entering the domain account password:

650) this.width=650; "title=" clip_image013 "style=" border-top:0px;border-right:0px;background-image:none; border-bottom:0px;padding-top:0px;padding-left:0px;border-left:0px;margin:0px;padding-right:0px; "Alt=" Clip_ image013 "src=" http://s3.51cto.com/wyfs02/M00/8B/D3/wKioL1haNFPhBPKaAABvt5__ebA107.jpg "height=" "border=" 0 "/ >



Start a remote session


Start a remote PowerShell session using Enter-pssession, where you can run multiple commands and not just a single command:

Enter-pssession-computername computer-credential USER

650) this.width=650; "title=" clip_image015 "style=" border-top:0px;border-right:0px;background-image:none; border-bottom:0px;padding-top:0px;padding-left:0px;border-left:0px;padding-right:0px, "alt=" clip_image015 "src=" Http://s3.51cto.com/wyfs02/M02/8B/D7/wKiom1haNFSiAi3RAAEFxkSDlQo148.jpg "height=" 591 "border=" 0 "/>



This article is from the SQL Server deep Dive blog, so be sure to keep this source http://ultrasql.blog.51cto.com/9591438/1884773

Running PowerShell commands on a remote computer

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.