บทความนี้จะแสดงวิธีกำหนดค่าพารามิเตอร์ network adapter ใน Windows ผ่าน PowerShell เราจะเรียนรู้วิธีรับและตั้งค่า static IP address และ DNS (nameservers) กำหนดค่า network interface ให้รับ IP configuration จาก DHCP server คุณสามารถใช้คอมมานด์เลตเหล่านี้เพื่อกำหนดค่าเครือข่ายใน Core/Nano versions ของ Windows Server, ใน Hyper-V Server, เพื่อเปลี่ยนการตั้งค่า IP บนคอมพิวเตอร์/เซิร์ฟเวอร์ระยะไกล และในสคริปต์ PS ของคุณ
ก่อนหน้านี้ คำสั่ง netsh interface ipv4 ถูกใช้เพื่อกำหนดค่าการตั้งค่าเครือข่าย Windows จาก CLI ใน PowerShell 3.0 และใหม่กว่า คุณสามารถใช้โมดูล PowerShell ส่วนตัว NetTCPIP เพื่อจัดการการตั้งค่าเครือข่าย Windows
เพื่อรับรายการคอมมานด์เลตในโมดูลนี้ ให้รันคำสั่งต่อไปนี้:
get-command -module NetTCPIP

โมดูลนี้ยังรวมคอมมานด์เลต Test-NetConnection ที่คุณสามารถใช้ทดสอบความพร้อมของ TCP port บนคอมพิวเตอร์ระยะไกล
การจัดการ Network Adapters ด้วย PowerShell
แสดงรายการ network interfaces ที่พร้อมใช้งานบนคอมพิวเตอร์:
Get-NetAdapter
คอมมานด์เลตคืนค่าชื่ออินเตอร์เฟซ สถานะ (Up/Down) ที่อยู่ MAC และความเร็วพอร์ต
ในตัวอย่างนี้ ฉันมี network adapters หลายตัวบนคอมพิวเตอร์ของฉัน (นอกจากการเชื่อมต่อทางกายภาพ Ethernet0 ฉันยังมีอินเตอร์เฟซเครือข่าย Hyper-V และ VMWare Player)

คุณสามารถอ้างอิง network interfaces โดยชื่อหรือ indexes (คอลัมน์ Index) ในตัวอย่างของเรา เพื่อเลือก physical LAN adapter Intel 82574L ให้ใช้คำสั่ง:
Get-NetAdapter -Name "Ethernet0"
หรือ:
Get-NetAdapter -InterfaceIndex 8

คุณสามารถเปลี่ยนชื่อ adapter ได้:
Rename-NetAdapter -Name Ethernet0 -NewName LAN
เพื่อปิดใช้งาน network interface ให้ใช้คำสั่งนี้:
Get-NetAdapter -InterfaceIndex 13| Disable-NetAdapter
เมื่อคุณเปิดใช้งาน interface คุณไม่สามารถใช้ index ได้ เนื่องจากยังไม่ได้กำหนด คุณสามารถระบุชื่อ adapter หรือ description:
Enable-NetAdapter -InterfaceDescription "Hyper-V Virtual Ethernet Adapter"

หากมี VLAN ระบุสำหรับ adapter คุณสามารถแสดงได้ดังนี้:
Get-NetAdapter | ft Name, Status, Linkspeed, VlanID
นี่คือวิธีที่คุณสามารถรับข้อมูลเกี่ยวกับ network adapter driver ที่ใช้:
Get-NetAdapter | ft Name, DriverName, DriverVersion, DriverInformation, DriverFileName

ข้อมูลเกี่ยวกับ physical network adapters (PCI slot, bus ฯลฯ):
Get-NetAdapterHardwareInfo
วิธีดูการตั้งค่า TCP/IP Network Adapter ด้วย PowerShell
เพื่อรับการตั้งค่า network adapter ปัจจุบัน (IP address, DNS, default gateway):
Get-NetIPConfiguration -InterfaceAlias Ethernet0

เพื่อแสดงข้อมูลโดยละเอียดเกี่ยวกับการกำหนดค่า TCP/IP ของ network adapter ปัจจุบัน ให้ใช้คำสั่งนี้:
Get-NetIPConfiguration -InterfaceAlias Ethernet0 -Detailed
ในกรณีนี้ จะแสดง network profile ที่กำหนด (NetProfile.NetworkCategory) ของอินเตอร์เฟซ, การตั้งค่า MTU (NetIPv4Interface.NlMTU), ว่าการรับ IP address จาก DHCP เปิดใช้งานหรือไม่ (NetIPv4Interface.DHCP) และข้อมูลที่มีประโยชน์อื่นๆ

เพื่อรับเฉพาะที่อยู่ IPv4 ของ interface:
(Get-NetAdapter -Name ethernet0 | Get-NetIPAddress).IPv4Address
ใช้ PowerShell ตั้งค่า Static IP Address
ลองตั้งค่า static IP address สำหรับ NIC เพื่อเปลี่ยน IP address, subnet mask และ default gateway สำหรับ network interface ให้ใช้:
New-NetIPAddress –IPAddress 192.168.2.50 -DefaultGateway 192.168.2.1 -PrefixLength 24 -InterfaceIndex 8
คุณสามารถตั้งค่า IP address โดยใช้โครงสร้าง array (ดูง่ายขึ้น):
$ipParams = @{
InterfaceIndex = 8
IPAddress = "192.168.2.50"
PrefixLength = 24
AddressFamily = "IPv4"
}
New-NetIPAddress @ipParams
คุณสามารถใช้ New-NetIPAddress เพื่อเพิ่มที่อยู่ IP ที่สอง (alias) ให้กับ network adapter
หาก static IP address ถูกกำหนดค่าแล้วและคุณต้องการเปลี่ยน คอมมานด์เลต Set-NetIPAddress จะถูกใช้:
Set-NetIPAddress -InterfaceIndex 8 -IPAddress 192.168.2.90
เพื่อปิดใช้งานการรับ IP address จาก DHCP สำหรับ adapter ของคุณ ให้รันคำสั่ง:
Set-NetIPInterface -InterfaceAlias Ethernet0 -Dhcp Disabled
เพื่อดูตารางการกำหนดเส้นทาง ใช้คอมมานด์เลต Get-NetRoute ในการเพิ่มเส้นทางใหม่ ใช้คอมมานด์เลต New-NetRoute:
New-NetRoute -DestinationPrefix "0.0.0.0/0" -NextHop "192.168.2.2" -InterfaceIndex 8
เพื่อปิดใช้งานโปรโตคอล IPv6 สำหรับ network adapter:
Get-NetAdapterBinding -InterfaceAlias Ethernet0 | Set-NetAdapterBinding -Enabled:$false -ComponentID ms_tcpip6
Set-DnsClientServerAddress: ตั้งค่าที่อยู่เซิร์ฟเวอร์ DNS หลักและรอง
เพื่อตั้งค่าที่อยู่ IP ของ primary และ secondary DNS server ใน Windows ให้ใช้คอมมานด์เลต Set-DNSClientServerAddress ตัวอย่าง:
Set-DNSClientServerAddress –InterfaceIndex 8 –ServerAddresses 192.168.2.11,10.1.2.11
คุณยังสามารถตั้งค่า nameservers โดยใช้ array:
$dnsParams = @{
InterfaceIndex = 8
ServerAddresses = ("8.8.8.8","8.8.4.4")
}
Set-DnsClientServerAddress @dnsParams
หลังจากเปลี่ยนการตั้งค่า DNS ของคุณ คุณสามารถล้าง resolver cache:
Clear-DnsClientCache
วิธีเปลี่ยน Static IP Address เป็น DHCP โดยใช้ PowerShell
เพื่ออนุญาตให้คอมพิวเตอร์รับ dynamic IP address จาก DHCP server สำหรับ network adapter ให้รันคำสั่งนี้:
Set-NetIPInterface -InterfaceAlias Ethernet0 -Dhcp Enabled
ล้างการตั้งค่า DNS server:
Set-DnsClientServerAddress –InterfaceIndex 8 -ResetServerAddresses
และรีสตาร์ท adapter ของคุณเพื่อรับ IP address อัตโนมัติจาก DHCP server:
Restart-NetAdapter -InterfaceAlias Ethernet0
หากคุณเคยมี default gateway ที่กำหนดค่าไว้ ให้ลบมัน:
Set-NetIPInterface -InterfaceAlias Ethernet0| Remove-NetRoute -Confirm:$false
วิธีเปลี่ยน IP Address และ DNS settings แบบระยะไกลด้วย PowerShell
คุณสามารถใช้ PowerShell เพื่อเปลี่ยน IP address หรือการตั้งค่า DNS server บนคอมพิวเตอร์ระยะไกลหลายเครื่อง สมมติว่าภารกิจของคุณคือเปลี่ยนการตั้งค่า DNS สำหรับเซิร์ฟเวอร์ทั้งหมดใน AD container (Organizational Unit) เฉพาะ ในการรับรายการคอมพิวเตอร์ในสคริปต์ด้านล่าง ใช้คอมมานด์เลต Get-ADComputer และ WinRM ใช้เพื่อเชื่อมต่อกับคอมพิวเตอร์ระยะไกล (คอมมานด์เลต Invoke-Command):
$Servers = Get-ADComputer -SearchBase 'OU=Servers,OU=Berlin,OU=DE,DC=woshub,DC=cpm' -Filter '(OperatingSystem -like "Windows Server*")' | Sort-Object Name
ForEach ($Server in $Servers) {
Write-Host "Server $($Server.Name)"
Invoke-Command -ComputerName $Server.Name -ScriptBlock {
$NewDnsServerSearchOrder = "192.168.2.11","8.8.8.8"
$Adapters = Get-WmiObject Win32_NetworkAdapterConfiguration | Where-Object {$_.DHCPEnabled -ne 'True' -and $_.DNSServerSearchOrder -ne $null}
Write-Host "Old DNS settings: "
$Adapters | ForEach-Object {$_.DNSServerSearchOrder}
$Adapters | ForEach-Object {$_.SetDNSServerSearchOrder($NewDnsServerSearchOrder)} | Out-Null
$Adapters = Get-WmiObject Win32_NetworkAdapterConfiguration | Where-Object {$_.DHCPEnabled -ne 'True' -and $_.DNSServerSearchOrder -ne $null}
Write-Host "New DNS settings: "
$Adapters | ForEach-Object {$_.DNSServerSearchOrder}
}
}