Computer >> บทช่วยสอนคอมพิวเตอร์ >  >> ระบบ >> Windows Server

Enter-PSSession: รันคำสั่งระยะไกลใน Interactive Shell

Cmdlet Enter-PSSession ช่วยสร้าง Persistent Interactive PowerShell Session กับคอมพิวเตอร์ระยะไกล คำสั่งทั้งหมดที่พิมพ์ใน Command Prompt จะรันบนคอมพิวเตอร์ระยะไกล บทความนี้อธิบายคุณสมบัติหลักของ Enter-PSSession และวิธีใช้จัดการคอมพิวเตอร์ Windows 10/11 และ Windows Server 2022/2019/2016 แบบระยะไกล

Cmdlet Enter-PSSession ขับเคลื่อนโดย PowerShell Remoting Stack PSRemoting พัฒนาจาก Web Services for Management (WS-Management) และ WinRM Service (Windows Remote Management) Traffic ระหว่างคอมพิวเตอร์ถูกเข้ารหัสที่ระดับโปรโตคอล (สามารถเปิด SSL Encryption สำหรับ PSRemoting WinRM Traffic เพิ่มเติม) ใช้ Authentication Methods ต่างๆ รวมถึง NTLM และ Kerberos

ในกรณีง่ายๆ การสร้าง Interactive PowerShell Session กับคอมพิวเตอร์ระยะไกล ต้องระบุแค่ชื่อคอมพิวเตอร์ที่ต้องการเชื่อมต่อ (Option ComputerName) รันคำสั่ง:

Enter-PSSession hq-srv01.woshub.com

Enter-PSSession: รันคำสั่งระยะไกลใน Interactive Shell

หากผู้ใช้ปัจจุบันมีสิทธิ์เชื่อมต่อ Remote Host จะเชื่อมต่อ Interactive Shell บนคอมพิวเตอร์ระยะไกลได้

Prompt ขอ Credentials ก่อนเชื่อมต่อ:

Enter-PsSession –ComputerName hq-srv01.woshub.com –Credentials woshub\maxbak

หรือ:

$creds = Get-Credential
Enter-PSSession -ComputerName hq-srv01 -Credential $creds

สังเกตว่าชื่อคอมพิวเตอร์ระยะไกลแสดงใน Square Brackets ต้นบรรทัดของ PowerShell Prompt ([hq-srv01.woshub.com]) ทำให้ทราบว่ากำลังรันใน Local หรือ Remote Shell Session

Output ของคำสั่งที่รันระยะไกลจะแสดงใน Local Console ของคุณ รันคำสั่ง hostname เพื่อยืนยันว่ารันบนคอมพิวเตอร์ระยะไกล

รันคำสั่งใดๆ ใน Interactive Prompt นี้ได้ (ตามสิทธิ์ของคุณ)

ตัวอย่าง แสดง Network Settings ของ Windows ด้วย PowerShell:

Get-NetIPConfiguration

เปลี่ยน DNS Settings บนคอมพิวเตอร์ระยะไกล:

Set-DNSClientServerAddress –InterfaceIndex 6 –ServerAddresses 192.168.13.4, 192.168.100.4

Enter-PSSession: รันคำสั่งระยะไกลใน Interactive Shell

ออกจาก Interactive Remote Shell Session รัน Exit-PSSession หรือ exit PS Prompt จะกลับสู่ปกติ และกลับมายัง Local PowerShell Console:

Enter-PSSession: รันคำสั่งระยะไกลใน Interactive Shell

ก่อนหน้านี้ Admin ใช้ Tool PsExec รัน Interactive Command Prompt บน Remote Windows Computer แต่เมื่อมี Enter-PSSession ไม่ต้องใช้ External Tools แล้ว

ใน Windows Server 2016/2019/2022 PowerShell Remoting เปิดอยู่โดยค่าเริ่มต้น (เห็นได้ใน Server Manager → Local Server → Remote Management = Enabled)

Enter-PSSession: รันคำสั่งระยะไกลใน Interactive Shell

ใน Desktop Windows (Win10, Win11) PSRemoting และ WinRM ปิดอยู่

ตรวจสอบว่า PSRemoting เปิดอยู่บนคอมพิวเตอร์ปัจจุบันหรือไม่:

Get-PSSessionConfiguration

คำสั่งนี้ยังใช้ดูรายการ Users และ Groups ที่อนุญาตให้เชื่อมต่อผ่าน WinRM เพื่อใช้ PSRemoting บัญชีผู้ใช้ต้องเป็นสมาชิกกลุ่ม Administrators หรือ Remote Management Users เรียนรู้เพิ่มเติมเกี่ยวกับการเปิด WinRM PowerShell Remoting สำหรับ Non-admin Users

Enter-PSSession: รันคำสั่งระยะไกลใน Interactive Shell

ทดสอบว่าเชื่อมต่อคอมพิวเตอร์ตัวเองผ่าน PowerShell Remoting ได้หรือไม่:

Test-WSMan -ComputerName localhost

หากคืนค่า WSMan Schema Version แสดงว่าอนุญาต Remote Connections ผ่าน PS Remoting

Enter-PSSession: รันคำสั่งระยะไกลใน Interactive Shell

หาก PowerShell Remoting ปิดหรือไม่ได้ตั้งค่า จะเกิด Error:

Test-WSMan : The client cannot connect to the destination specified in the request. Verify that the service on the destination is running and is accepting requests. Consult the logs and documentation for the WS-Management service running on the destination, most commonly IIS or WinRM. If the destination is the WinRM service, run the following command on the destination to analyze and configure the WinRM service: "winrm quickconfig".

เปิดใช้งาน PowerShell Remoting:

Enable-PSRemoting -Force

คำสั่งนี้:

  • เปิด WinRM Service และตั้ง Startup Type เป็น Automatic
  • สร้าง Connection Point บน Default WinRM Port (TCP/5985 สำหรับ HTTP Traffic)
  • เพิ่ม Exception สำหรับ WS-Management ใน Windows Firewall (หากตั้งค่า PSRemoting เอง ให้เพิ่ม Firewall Rule ด้วย PowerShell หรือ GPO)
  • อนุญาต Remote PowerShell Sessions
  • Restart WinRM Service

ตรวจสอบว่า WinRM Service รันอยู่และตั้งค่า Start Automatically:

Get-Service WinRM | Select MachineName,Name,Status, StartType

Enter-PSSession: รันคำสั่งระยะไกลใน Interactive Shell

Enable-PSRemoting ทำงานได้เฉพาะ Domain และ Private Network Profiles หากต้องการเปิด PSRemoting บนคอมพิวเตอร์ใน Public Network ให้เปลี่ยน Network Location จาก Public เป็น Private หรือใช้คำสั่ง:

Enable-PSRemoting -SkipNetworkProfileCheck -Force

ในสภาพแวดล้อม Active Directory วิธีที่ง่ายที่สุดในการตั้งค่า Windows Remote Management (PSRemoting) แบบ Centralized บน Servers และ Computers คือผ่าน Group Policy

PowerShell เวอร์ชันใหม่ (v6 หรือ v7) รองรับ Secure Shell Protocol (SSH) สำหรับเชื่อมต่อ Remote Computer ผ่าน PowerShell Remoting ต้องมี SSH Connection Point บน Remote Computer (วิธีเปิด Built-in OpenSSH Server บน Windows 10?) เริ่ม Interactive PSRemoting Session ผ่าน SSH:

Enter-PSSession -HostName maxbak@hq-srv01.woshub.com

หรือ Authenticate ผ่าน SSH ใช้ RSA Key:

Enter-PSSession -HostName maxbak@hq-srv01.woshub.com:22 -KeyFilePath c:\PS\max_rsa_key

ใช้ Enter-PSSession ร่วมกับ New-PSSession:

$s = New-PSSession -ComputerName hq-srv01.woshub.com
Enter-PSSession -Session $s

Enter-PSSession รองรับ Authentication Methods หลายแบบ ตั้งค่าที่ต้องการด้วยพารามิเตอร์ -Authentication รองรับ Basic, Digest, Kerberos, CredSSP, NegotiateWithImplicitCredential, Negotiate Challenge

ในตัวอย่างข้างต้น แสดงการสร้าง Enter-PSSession แบบ Interactive ระหว่างคอมพิวเตอร์ใน Windows Domain เดียวกัน (พอระบุ FQDN หรือ Short Name ก็พอ Kerberos Authentication ถูกใช้) หากพยายามเชื่อมต่อ Remote Computer ด้วย IP Address หรือ CNAME จะไม่ได้รับ Authentication:

Enter-PSSession : Connecting to remote server 192.168.31.12 failed with the following error message: The WinRM client cannot process the request. Default authentication may be used with an IP address under the following conditions: the transport is HTTPS or the destination is in the TrustedHosts list, and explicit credentials are provided. Use winrm.cmd to configure TrustedHosts. Note that computers in the TrustedHosts list might not be authenticated.

Enter-PSSession: รันคำสั่งระยะไกลใน Interactive Shell

เพื่อเชื่อมต่อ Remote Computer ด้วย IP Address ให้เพิ่ม Host ใน Trusted Hosts List หรือใช้ SSL สำหรับ WinRM (ปลอดภัยกว่า)

เพิ่ม IP Address ใน Trusted Hosts:

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

เพิ่ม Trusted Host ด้วย Wildcard Mask:

Set-Item WSMan:\localhost\Client\TrustedHosts -Value *.woshub.com

แสดงรายการ Trusted Hosts:

Get-Item WSMan:\localhost\Client\TrustedHosts

เพิ่ม Host ของคุณใน Trusted Hosts บน Remote Computer แบบเดียวกัน

Restart Service:

Restart-Service WinRM

เชื่อมต่อ Remote Computer ด้วย IP Address:

Enter-PSSession -ComputerName 192.168.13.5 -Credential (Get-Credential -UserName woshub\maxbak)

Cmdlet Enter-PSSession และ New-PSSession สร้าง Persistent One-to-One Remote Session ใช้ส่วนใหญ่ใน Interactive Scenarios หากต้องการรัน Scripts หรือ Jobs แบบอัตโนมัติ หรือทำงานบนหลาย Remote Computers พร้อมกัน ให้ใช้คำสั่ง Invoke-Command