PowerShell คือ โปรแกรม CLI ทางฝั่งของ Microsoft โดยออกแบบมาเพื่อรองรับการทำงาน System Admin ซึ่งหากจะเปรียบเทียบทางฝั่ง Linux จะคล้ายกับ SSH ซึ่งตัวโปรแกรม PowerShell จะมีอยู่ใน Windows 7, 8, 8.1 และ 10 และ Windows Servers

ในที่นี้จะมาแนะนำการใช้งาน Remote Computer ด้วยการใช้ PowerShell Command โดยมีขั้นตอนดังต่อไปนี้

Enable PowerShell Remote

ขั้นตอนนี้จะทำที่ฝั่งที่เป็น Target ทำการ Search หา “PowerShell” จากปุ่ม Start เมื่อเจอแล้วให้ Run as administrator


จากนั้นให้ใส่ Command ดังนี้

Enable-PSRemoting -Force

โดย Command นี้จะเป็นการสั่งให้ WinRM Service ทำงานอัตโนมัติเมื่อเริ่มการทำงานของ System และกำหนดให้การ Remote เข้ามายังเครื่อง Target ผ่าน Firewall เครื่องด้วย


Workgroup Setup

ขั้นตอนนี้จะเป็นการกำหนดเครื่องที่สามารถเข้ามา Remote เครื่องนี้ได้ โดยใช้ Command ดังนี้

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

ใน “*” สามารถแทนค่าโดย Computer Name หรือ IP Address ได้ ในที่นี้ใช้ “*” แทนว่า ได้ทุกเครื่อง

หลังจากกด Run Command เสร็จ จะมีคำถามขึ้นมาถามหากไม่เคย Add เครื่องที่ไม่เคยใส่ข้อมูลมาก่อน จากนั้นให้กด “y” เพื่อ Add


เมื่อ Add จนเสร็จเรียบร้อยแล้ว ให้ทำการ Restart Service ดังต่อไปนี้

Restart-Service WinRM


Testing the Connection

ทำการตรวจสอบ Connection จากเครื่อง Host โดยใช้ Command ดังนี้

Test-WsMan //Computer or IP Destianation//


Executing a Remote Command

ก่อนจะทำการ PowerShell Remote นั้น จะต้องทำการร้องขอว่าจะเข้าใช้งานโดย User อะไร เข้าใช้งาน ที่ Path ไหน โดยมี Command ดังนี้

Invoke-Command -ComputerName //COMPUTER Destination -ScriptBlock { COMMAND } -credential //USERNAME on Destination Host//


Starting Remote Session

หลังจากทำการ Invoke ไปแล้ว สามารถเริ่มการ Remote PowerShell Session โดยใช้ Command นี้

Enter-PSSession –ComputerName //Computer or IP Destianation// -credential //USERNAME on Destination Host//



Reference: http://www.howtogeek.com/117192/how-to-run-powershell-commands-on-remote-computers/

How to Remote Computer by PowerShell Command