標籤:powershell
分兩部分操作:
一。在遠程主機上執行相應的策略允許。
在遠程主機上開啟防火牆該組件Windows Remote Management (Http-In)允許為public
在遠程主機上開通5985連接埠。
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 或者進程號:$id=Get-Process -name explorer* | Select-Object id | ForEach-Object -Process{$_.id}
二。本地電腦上允許某些策略
powershell run as administrator
PS> Start-Process powershell -Verb runAs
PS> enable-psremoting -force
把遠程主機加入到本地powershell信任清單
PS> set-item wsman:\localhost\Client\TrustedHosts -value "remote-ip" -force
PS> set-item WSMan:\localhost\Shell\MaxMemoryPerShellMB -Value 0 -Force
在本機上允許執行powershell指令碼
PS> set-executionpolicy -executionpolicy unrestricted
指令碼內容:
$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在遠程windows server上登入並執行程式