วิธีเปิดใช้งาน PowerShell Remoting บน Windows Server 2022

PowerShell Remoting เป็นหนึ่งในเครื่องมือที่สำคัญที่สุดสำหรับผู้ดูแลระบบยุคใหม่ เพราะช่วยให้สามารถสั่งงาน Windows Server จากระยะไกลผ่าน PowerShell ได้โดยไม่ต้องใช้ Remote Desktop

ในองค์กรที่มี Server จำนวนมาก การใช้ PowerShell Remoting สามารถลดเวลาการทำงานจากหลายชั่วโมงเหลือเพียงไม่กี่นาที

บทความนี้จะอธิบายวิธีเปิดใช้งาน PowerShell Remoting บน Windows Server 2022 อย่างถูกต้อง

① PowerShell Remoting คืออะไร

PowerShell Remoting

คือความสามารถในการส่งคำสั่ง PowerShell ไปยังเครื่องอื่นผ่านเครือข่าย

ช่วยให้สามารถ

  • ตรวจสอบ Server

  • รีสตาร์ต Service

  • ติดตั้ง Software

  • เก็บข้อมูลระบบ

ได้จากเครื่องเดียว

② ทำไมต้องใช้ PowerShell Remoting

ข้อดี

  • บริหารหลาย Server พร้อมกัน

  • ไม่ต้อง Remote Desktop

  • ทำ Automation ได้

  • ลดเวลาการทำงาน

เหมาะสำหรับองค์กรทุกขนาด

③ PowerShell Remoting ใช้อะไรเบื้องหลัง

PowerShell Remoting

ทำงานบน

  • WinRM

  • WSMan

  • PowerShell

ดังนั้นต้องเปิด WinRM ก่อน

④ ตรวจสอบ WinRM

PowerShell

Get-Service WinRM

ควรเห็นสถานะ

Running

⑤ เปิด PowerShell Remoting

วิธีที่ง่ายที่สุด

Enable-PSRemoting -Force

คำสั่งนี้จะ

  • เปิด WinRM

  • เปิด Firewall

  • สร้าง Listener

ให้อัตโนมัติ

⑥ ตรวจสอบ Listener

winrm enumerate winrm/config/listener

ใช้ตรวจสอบว่า Listener ทำงานหรือไม่

⑦ ตรวจสอบ WinRM

Test-WsMan localhost

หากแสดงข้อมูลกลับมา

แสดงว่าพร้อมใช้งาน

⑧ ทดสอบจากเครื่องอื่น

Test-WsMan SRV01

หรือ

Test-WsMan 192.168.1.10

⑨ เข้าใช้งาน Remote Session

Enter-PSSession -ComputerName SRV01

เมื่อสำเร็จ

จะเข้าสู่ PowerShell ของเครื่องปลายทาง

⑩ ออกจาก Session

Exit-PSSession

⑪ รันคำสั่งระยะไกล

ตัวอย่าง

Invoke-Command -ComputerName SRV01 -ScriptBlock { Get-Service }

⑫ รันคำสั่งหลายเครื่องพร้อมกัน

Invoke-Command -ComputerName SRV01,SRV02,SRV03 -ScriptBlock { hostname }

เหมาะสำหรับ Data Center

⑬ ดูข้อมูลระบบระยะไกล

Invoke-Command -ComputerName SRV01 -ScriptBlock { Get-ComputerInfo }

⑭ รีสตาร์ต Service ระยะไกล

Invoke-Command -ComputerName SRV01 -ScriptBlock { Restart-Service Spooler }

⑮ รีบูต Server ระยะไกล

Restart-Computer -ComputerName SRV01

ช่วยลดการใช้ RDP อย่างมาก

⑯ ใช้งานกับ Credential

Enter-PSSession -ComputerName SRV01 -Credential DOMAIN\Admin

เหมาะสำหรับ Domain Environment

⑰ ใช้งานกับ Workgroup

กรณี Workgroup

อาจต้องเพิ่ม Trusted Hosts

Set-Item WSMan:\localhost\Client\TrustedHosts -Value *

⑱ ตรวจสอบ Trusted Hosts

Get-Item WSMan:\localhost\Client\TrustedHosts

⑲ Firewall ที่เกี่ยวข้อง

PowerShell Remoting ใช้

TCP 5985

และ

TCP 5986

Firewall ต้องอนุญาต

⑳ ปัญหาที่พบบ่อย

Access Denied

สาเหตุ

  • สิทธิ์ไม่เพียงพอ

WinRM Cannot Process Request

สาเหตุ

  • WinRM ไม่ทำงาน

  • Firewall Block

Test-WsMan ไม่ผ่าน

สาเหตุ

  • Network มีปัญหา

  • DNS ผิด

㉑ PowerShell Remoting กับ Server Core

Server Core

ออกแบบมาให้ใช้งานผ่าน

PowerShell Remoting

เป็นหลัก

จึงควรเปิดใช้งานเสมอ

㉒ PowerShell Remoting กับ Windows Admin Center

Windows Admin Center

ใช้ WinRM และ PowerShell Remoting

เป็นกลไกหลักในการสื่อสาร

㉓ Security Best Practice

ควร

  • ใช้ Domain Authentication

  • จำกัด Firewall

  • ใช้ HTTPS หากเป็นไปได้

  • จำกัดสิทธิ์ผู้ใช้งาน

㉔ Checklist หลังเปิดใช้งาน

ตรวจสอบ

  • WinRM Running

  • Test-WsMan ผ่าน

  • Firewall เปิด

  • Remote Session ใช้งานได้

  • Event Log ปกติ

㉕ สรุป

PowerShell Remoting เป็นเครื่องมือสำคัญสำหรับการบริหาร Windows Server 2022 แบบมืออาชีพ และเป็นพื้นฐานของ Automation ยุคใหม่

Checklist สำคัญ

  • เปิด WinRM

  • เปิด PowerShell Remoting

  • เปิด Firewall

  • ทดสอบ Remote Session

  • ใช้ Domain Authentication

comsiam แนะนำว่า หากมี Windows Server มากกว่า 3 เครื่อง ควรเริ่มใช้ PowerShell Remoting ทันที เพราะสามารถลดเวลาการบริหารจัดการระบบได้อย่างมหาศาล และเป็นทักษะที่ผู้ดูแลระบบระดับมืออาชีพทุกคนควรมี

Popular posts from this blog

IIS Security Best Practices บน Windows Server 2022 แนวทางความปลอดภัยที่ควรทำสำหรับทุกองค์กร

Windows Server 2022 ยังน่าใช้ในปีนี้หรือไม่? คำตอบสำหรับคนกำลังวางระบบใหม่

สเปกเครื่องสำหรับติดตั้ง Windows Server 2022 เลือกอย่างไรให้คุ้มและใช้งานได้ยาว