powershell在遠程windows server上登入並執行程式

來源:互聯網
上載者:User

標籤: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上登入並執行程式

相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

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.