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