PowerShell logs on and executes programs on remote Windows Server

Source: Internet
Author: User

Two-part operation:

One. Execute the appropriate policy permission on the remote host.

Turn on the firewall on the remote host Windows remote Management (http-in) allows for public

Open the 5985 port on the remote host.

PowerShell Run as Administrator

ps> start-process Powershell-verb RunAs

Ps> Enable-psremoting-force


ps> Set-executionpolicy-executionpolicy Remotesigned-scope Currentuser-force

ps> set-item wsman:\localhost\client\trustedhosts-value "Remote-ip"-force

ps> Set-item Wsman:\localhost\shell\maxmemorypershellmb-value 0-force


PowerShell or process number: $id =get-process-name explorer* | Select-object ID | Foreach-object-process{$_.id}




Two. Some policies are allowed on the local computer

PowerShell Run as Administrator

ps> start-process Powershell-verb RunAs

Ps> Enable-psremoting-force



To add a remote host to the local PowerShell trust list

ps> set-item wsman:\localhost\client\trustedhosts-value "Remote-ip"-force

ps> Set-item Wsman:\localhost\shell\maxmemorypershellmb-value 0-force


Allow PowerShell scripts to be executed on this machine

Ps> Set-executionpolicy-executionpolicy Unrestricted




Script content:

$remoteUsername = "username"

$remotePassword = "Password"

$remoteHostname = "Remote-ip"


$process = "Pro-name"

$securePassword = Convertto-securestring-asplaintext-force $remotePassword

$cred = New-object System.Management.Automation.PSCredential $remoteUsername, $securePassword

Invoke-command-computername $remoteHostname-credential $cred-scriptblock {

Write-host "Hello, World (from $env: COMPUTERNAME)"

if ($process) {

Stop-process-name $process

Start-sleep-seconds 2

Start-process "C:\path\to\program.exe"

Write-host "program restart finished."

} else {

Start-process "C:\path\to\program.exe"

Write-host "program start finished."}

}



PowerShell logs on and executes programs on remote Windows Server

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.