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

คำสั่งพื้นฐานสำหรับกำหนดค่าและจัดการ Windows Server Core

ในบทความนี้ ฉันพยายามอธิบายคำสั่ง cmd และ PowerShell พื้นฐานที่ใช้ในการกำหนดค่าและจัดการ Windows Server Core ฉันคิดว่าคู่มือนี้จะมีประโยชน์ทั้งสำหรับผู้เริ่มต้นและผู้ดูแลระบบที่มีประสบการณ์ในฐานะข้อมูลอ้างอิงสำหรับคำสั่งการบริหาร Server Core พื้นฐาน

ข้อดีของ Server Core

  • ความต้องการทรัพยากรต่ำกว่า;
  • ความเสถียรและความปลอดภัยที่ดีขึ้น; ต้องการอัปเดตน้อยลง (เนื่องจากโค้ดและส่วนประกอบที่ใช้น้อยลง);
  • เหมาะอย่างยิ่งสำหรับใช้เป็นเซิร์ฟเวอร์บทบาทโครงสร้างพื้นฐาน (Active Directory Domain Controller, DHCP Server, Hyper-V Host, SMB File Server ฯลฯ)

Server Core ได้รับอนุญาตเป็นอินสแตนซ์ Windows Server ทั่วไปทางกายภาพหรือเสมือน (ไม่เหมือน Hyper-V Server ที่ฟรีทั้งหมด)

ในการติดตั้ง Windows Server 2016/2019 ในโหมด Core คุณต้องเลือกการติดตั้งทั่วไป หากคุณเลือก Windows Server (Desktop Experience) จะติดตั้งเวอร์ชัน GUI ของระบบปฏิบัติการ (ใน Windows Server เวอร์ชันก่อนหน้านี้เรียกว่า Server with a GUI)

⚠ หมายเหตุ: คุณไม่สามารถสลับระหว่าง Full GUI และ Core Modes ใน Windows Server 2016/2019 ได้โดยไม่ต้องติดตั้งเซิร์ฟเวอร์ใหม่

หลังจากติดตั้ง Windows Server Core คุณจะได้รับแจ้งให้ตั้งรหัสผ่านผู้ดูแลระบบท้องถิ่น

เมื่อคุณล็อกอินเข้าสู่ Server Core Command Prompt จะปรากฏขึ้น (cmd.exe) หากคุณต้องการให้คอนโซล PowerShell รันแทน ให้ทำการเปลี่ยนแปลงใน Registry รันคำสั่งด้านล่าง:

Powershell.exe
Set-ItemProperty -Path 'HKLM:\Software\Microsoft\Windows NT\CurrentVersion\WinLogon' -Name Shell -Value 'PowerShell.exe'

และรีสตาร์ทเซิร์ฟเวอร์ของคุณ:

Restart-Computer -Force

หากคุณปิดหน้าต่าง Command Prompt โดยไม่ได้ตั้งใจ ให้กด Ctrl+Alt+Delete เปิด Task Manager -> File -> Run -> และรัน cmd.exe (หรือ PowerShell.exe)

กำหนดค่า Windows Server Core โดยใช้ SCONFIG

คุณสามารถใช้สคริปต์ sconfig ที่สร้างมาอยู่แล้วสำหรับการกำหนดค่า Server Core พื้นฐาน เพียงรันคำสั่ง sconfig ในคอนโซลของคุณ คุณจะเห็นเมนูพร้อมรายการหลายอย่าง:

โดยใช้เมนู Server Configuration คุณสามารถทำสิ่งต่อไปนี้ได้:

  • เพิ่มคอมพิวเตอร์เข้าสู่โดเมนหรือ Workgroup
  • เปลี่ยนชื่อคอมพิวเตอร์ (Hostname)
  • เพิ่ม Local Administrator
  • อนุญาต/ปฏิเสธการจัดการระยะไกลและการตอบสนอง ICMP
  • กำหนดค่าการตั้งค่า Windows Update
  • ติดตั้ง Windows Updates
  • เปิด/ปิดใช้งาน RDP
  • กำหนดค่าการตั้งค่า Network Adapter (IP Address, Gateway, DNS Server)
  • ตั้งค่าวันที่และเวลา
  • เปลี่ยนการตั้งค่า Telemetry
  • เปิดใช้งานอินสแตนซ์ Windows Server ของคุณ
  • Log off, Restart หรือ Shut down เซิร์ฟเวอร์ของคุณ

แต่ละรายการใน sconfig มีหมายเลขของมัน เพื่อเปิดรายการที่คุณต้องการ ให้พิมพ์ number ของมันและกด Enter

คำสั่ง PowerShell พื้นฐานสำหรับกำหนดค่า Server Core

ข้อมูลระบบ

Get-ComputerInfo | select WindowsProductName, WindowsVersion, OsHardwareAbstractionLayer
$PSVersionTable

รีสตาร์ทและ Log off

Restart-Computer
logoff

กำหนดค่าการตั้งค่าเครือข่ายบน Server Core ด้วย PowerShell

ตอนนี้คุณต้องกำหนดค่าการตั้งค่าเครือข่ายโดยใช้ PowerShell (โดยค่าเริ่มต้น Windows ถูกกำหนดค่าให้รับที่อยู่ IP จาก DHCP) แสดงรายการ Network Adapters:

Get-NetIPConfiguration

ระบุดัชนีของอินเทอร์เฟซ Network Adapter (InterfaceIndex) ที่คุณต้องการเปลี่ยนและตั้งค่าที่อยู่ IP ใหม่และ DNS Servers:

New-NetIPaddress -InterfaceIndex 4 -IPAddress 192.168.1.100 -PrefixLength 24 -DefaultGateway 192.168.1.1
Set-DNSClientServerAddress –InterfaceIndex 4 -ServerAddresses 192.168.1.11,192.168.101.11

ตรวจสอบการตั้งค่าเครือข่ายปัจจุบัน:

Get-NetIPConfiguration

หากคุณต้องการรีเซ็ตที่อยู่ IP และกลับไปรับ IP จาก DHCP ให้รันคำสั่งด้านล่าง:

Set-DnsClientServerAddress –InterfaceIndex 4 –ResetServerAddresses
Set-NetIPInterface –InterfaceIndex 4 -Dhcp Enabled

เปิด/ปิดใช้งาน Network Adapter

Disable-NetAdapter -Name "Ethernet0"
Enable-NetAdapter -Name "Ethernet 0"

เปิด/ปิด/ตรวจสอบสถานะ IPv6

Disable-NetAdapterBinding -Name "Ethernet0" -ComponentID ms_tcpip6
Enable-NetAdapterBinding -Name "Ethernet0" -ComponentID ms_tcpip6
Get-NetAdapterBinding -ComponentID ms_tcpip6

กำหนดค่า WinHTTP Proxy

netsh Winhttp set proxy :

ตั้งค่าวันที่/เวลา/Time Zone

Set-Date -Date "07/21/2021 09:00"
Set-TimeZone "Central Europe Standard Time"

ตั้งค่าชื่อคอมพิวเตอร์ เข้าร่วมโดเมน และเปิดใช้งาน Server Core

Rename-Computer -NewName be-srv01 -PassThru
Add-Computer -DomainName "corp.woshub.com" -Restart
Add-LocalGroupMember -Group "Administrators" -Member "corp\jsmith"
slmgr.vbs –ipk 
slmgr.vbs –ato

หรือคุณอาจเปิดใช้งานโฮสต์บนเซิร์ฟเวอร์ KMS ตัวอย่างเช่น เพื่อเปิดใช้งาน Windows Server Core 2019 Standard บน KMS Host:

slmgr /ipk N69G4-B89J2-4G8F4-WWYCC-J464C
slmgr /skms kms.corp.woshub.com:1688
slmgr /ato

เปิดใช้งานการจัดการระยะไกลของ Windows Server Core

cscript C:\Windows\System32\Scregedit.wsf /ar 0
Configure-SMRemoting.exe –Enable
Enable-NetFirewallRule -DisplayGroup "Windows Remote Management"
Configure-SMRemoting.exe -Get
Enable-PSRemoting –force

คุณสามารถจัดการเซิร์ฟเวอร์ที่รัน Windows Server Core ได้จากระยะไกลจากเซิร์ฟเวอร์อื่น (โดยใช้ ServerManager.exe) ผ่านเบราว์เซอร์โดยใช้ Windows Admin Center (WAC) จากเวิร์กสเตชันใดๆ โดยใช้ RSAT คุณยังสามารถเชื่อมต่อกับมันได้โดยใช้ RDP, PowerShell Remoting หรือ SSH (Windows เวอร์ชันปัจจุบันมี SSH Server ในตัว)

กำหนดค่า Windows Firewall บน Server Core

Set-NetFirewallProfile -Profile Domain,Public,Private -Enabled True
Get-NetConnectionProfile | Set-NetConnectionProfile -NetworkCategory Private
Get-NetFirewallProfile | Set-NetFirewallProfile -enabled false

อนุญาตการเชื่อมต่อโดยใช้เครื่องมือจัดการระยะไกล:

Enable-NetFireWallRule -DisplayName "Windows Management Instrumentation (DCOM-In)"
Enable-NetFireWallRule -DisplayGroup "Remote Event Log Management"
Enable-NetFireWallRule -DisplayGroup "Remote Service Management"
Enable-NetFireWallRule -DisplayGroup "Remote Volume Management"
Enable-NetFireWallRule -DisplayGroup "Remote Scheduled Tasks Management"
Enable-NetFireWallRule -DisplayGroup "Windows Firewall Remote Management"
Enable-NetFirewallRule -DisplayGroup "Remote Administration"

การติดตั้ง Updates บน Windows Server Core

สำหรับการจัดการตัวเลือกการอัปเดต ดีกว่าที่จะใช้ Windows Update Group Policies อย่างไรก็ตาม คุณสามารถตั้งค่าการอัปเดตด้วยตนเองได้

# ปิดใช้งานอัปเดตอัตโนมัติ
Set-ItemProperty -Path HKLM:\Software\Policies\Microsoft\Windows\WindowsUpdate\AU -Name AUOptions -Value 1

# ดาวน์โหลดอัปเดตที่มีอยู่โดยอัตโนมัติ
Set-ItemProperty -Path HKLM:\Software\Policies\Microsoft\Windows\WindowsUpdate\AU -Name AUOptions -Value 3

# ดึงรายการอัปเดตที่ติดตั้งแล้ว
Get-Hotfix
# หรือ
wmic qfe list

# ติดตั้ง Windows Updates ด้วยตนเองโดยใช้เครื่องมือ wusa
wusa kbnamexxxxx.msu /quiet

สำหรับการติดตั้งและจัดการอัปเดตจาก Command Prompt สะดวกที่จะใช้โมดูล PSWindowsUpdate สำหรับ PowerShell

จัดการ Roles, Services และ Processes ของ Windows Core

Get-WindowsFeature
Get-WindowsFeature | Where-Object {$_.installstate -eq "installed"} | ft Name,Installstate
Install-WindowsFeature DNS -IncludeManagementTools
Get-Service
Get-Service | Where-Object {$_.status -eq "stopped"}
Restart-Service -Name spooler
Get-Process cmd, wuaucl* | Select-Object ProcessName, StartTime, MainWindowTitle, Path, Company | ft

คำสั่ง Windows Server Core ที่มีประโยชน์

ข้อมูลสถานะและสุขภาพของดิสก์กายภาพ

Get-PhysicalDisk | Sort Size | FT FriendlyName, Size, MediaType, SpindleSpeed, HealthStatus, OperationalStatus -AutoSize

ข้อมูลพื้นที่ดิสก์ว่าง

Get-WmiObject -Class Win32_LogicalDisk |
Select-Object -Property DeviceID, VolumeName,
@{Label='FreeSpace (Gb)'; expression={($_.FreeSpace/1GB).ToString('F2')}},
@{Label='Total (Gb)'; expression={($_.Size/1GB).ToString('F2')}},
@{label='FreePercent'; expression={[Math]::Round(($_.freespace / $_.size) * 100, 2)}} | ft

ข้อมูลเกี่ยวกับ 10 ครั้งล่าสุดที่รีบูทเซิร์ฟเวอร์

Get-EventLog system | where-object {$_.eventid -eq 6006} | select -last 10

รายการโปรแกรมที่ติดตั้งแล้ว

Get-ItemProperty HKLM:\Software\Wow6432Node\Microsoft\Windows\CurrentVersion\Uninstall\* | Select-Object DisplayName, DisplayVersion, Publisher, InstallDate | Format-Table –AutoSize

ดาวน์โหลดและแยกไฟล์ ZIP จากเว็บไซต์ภายนอก

Invoke-WebRequest https://servername/file.zip -outfile file.zip
Expand-Archive -path '.\file.zip' -DestinationPath C:\Users\Administrator\Documents\

คัดลอกไฟล์ทั้งหมดจากไดเรกทอรีไปยังคอมพิวเตอร์ระยะไกลผ่านเครือข่าย

$session = New-PSSession -ComputerName be-dc01
Copy-Item -Path "C:\Logs\*" -ToSession $session -Destination "C:\Logs\" -Recurse -Force

ติดตั้ง Device Driver

Pnputil –i –a c:\install\hpspp\hpdp.inf

Server Core App Compatibility Feature on Demand (FOD)

Microsoft ยังเสนอแพ็กเกจพิเศษ Server Core App Compatibility Feature on Demand (FOD) ซึ่งช่วยให้คุณติดตั้งเครื่องมือและ Snap-in แบบกราฟิกบางส่วนบน Windows Core Server 2019 (MMC, Eventvwr, Hyper-V Manager, PerfMon, Resmon, Explorer.exe, Device Manager, Powershell ISE) คุณสามารถดาวน์โหลด FOD เป็นไฟล์ ISO หากการสมัครสมาชิก Microsoft ของคุณยังคงใช้งานอยู่ คุณสามารถติดตั้งได้ดังนี้:

Add-WindowsCapability -Online -Name ServerCore.AppCompatibility~~~~0.0.1.0

การติดตั้ง Server Core App Compatibility Feature on Demand จะใช้ RAM เพิ่มเติมประมาณ 200 MB บน Server Core ของคุณ

ในบทความนี้ ฉันพยายามรวบรวมคำสั่งที่มีประโยชน์ที่สุดซึ่งจำเป็นสำหรับการบริหาร Windows Server Core ทุกวัน จากเวลาไปเวลา ฉันจะอัปเดตบทความและเพิ่มคำสั่งใหม่หากดูเหมือนว่าจำเป็นสำหรับงานประจำวัน