Trong bài viết này chúng tôi sẽ chỉ cách cấu hình tham số network adapter trong Windows qua PowerShell. Chúng ta sẽ học cách lấy và đặt địa chỉ IP tĩnh và DNS (nameservers), cấu hình network interface lấy cấu hình IP từ DHCP server. Bạn có thể dùng các cmdlet này để cấu hình network trong phiên bản Core/Nano của Windows Server, trong Hyper-V Server, thay đổi cài đặt IP trên máy tính/server từ xa và trong script PS của bạn.
Trước đây, lệnh netsh interface ipv4 được dùng để cấu hình cài đặt mạng Windows từ CLI. Trong PowerShell 3.0 và mới hơn, bạn có thể dùng module PowerShell tích hợp NetTCPIP để quản lý cài đặt mạng Windows.
Để lấy danh sách cmdlet trong module này, chạy lệnh sau:
get-command -module NetTCPIP

Module này cũng bao gồm cmdlet Test-NetConnection bạn có thể dùng để test tính khả dụng cổng TCP trên máy tính từ xa.
Quản Lý Network Adapter Với PowerShell
Hiển thị danh sách network interface có sẵn trên máy tính:
Get-NetAdapter
Cmdlet trả về tên interface, trạng thái (Up/Down), địa chỉ MAC và tốc độ cổng.
Trong ví dụ này, tôi có nhiều network adapter trên máy tính (bên cạnh kết nối vật lý, Ethernet0, tôi có một số interface Hyper-V và VMWare Player).

Bạn có thể tham chiếu network interface bằng tên hoặc index (cột Index). Trong ví dụ của chúng tôi, để chọn physical LAN adapter Intel 82574L, dùng lệnh:
Get-NetAdapter -Name "Ethernet0"
hoặc:
Get-NetAdapter -InterfaceIndex 8

Bạn có thể đổi tên adapter:
Rename-NetAdapter -Name Ethernet0 -NewName LAN
Để vô hiệu hóa network interface, dùng lệnh:
Get-NetAdapter -InterfaceIndex 13| Disable-NetAdapter
Khi bật interface, bạn không thể dùng index vì nó chưa được gán. Bạn có thể chỉ định tên adapter hoặc description:
Enable-NetAdapter -InterfaceDescription “Hyper-V Virtual Ethernet Adapter"

Nếu VLAN được chỉ định cho adapter, bạn có thể hiển thị nó như sau:
Get-NetAdapter | ft Name, Status, Linkspeed, VlanID
Đây là cách lấy thông tin về driver network adapter dùng:
Get-NetAdapter | ft Name, DriverName, DriverVersion, DriverInformation, DriverFileName

Thông tin về physical network adapter (PCI slot, bus, v.v.):
Get-NetAdapterHardwareInfo
Cách Xem Cài Đặt TCP/IP Network Adapter Với PowerShell?
Để lấy cài đặt network adapter hiện tại (địa chỉ IP, DNS, default gateway):
Get-NetIPConfiguration -InterfaceAlias Ethernet0

Để hiển thị thông tin chi tiết về cấu hình TCP/IP hiện tại của network adapter, dùng lệnh:
Get-NetIPConfiguration -InterfaceAlias Ethernet0 -Detailed
Trong trường hợp này, network profile được gán (NetProfile.NetworkCategory) của interface, cài đặt MTU (NetIPv4Interface.NlMTU), việc lấy địa chỉ IP từ DHCP có được bật (NetIPv4Interface.DHCP) và thông tin hữu ích khác được hiển thị.

Để chỉ lấy địa chỉ IPv4 của interface:
(Get-NetAdapter -Name ethernet0 | Get-NetIPAddress).IPv4Address
Dùng PowerShell Đặt Static IP Address
Hãy thử đặt địa chỉ IP tĩnh cho NIC. Để thay đổi địa chỉ IP, subnet mask và default gateway cho network interface dùng:
New-NetIPAddress –IPAddress 192.168.2.50 -DefaultGateway 192.168.2.1 -PrefixLength 24 -InterfaceIndex 8
Bạn có thể đặt địa chỉ IP dùng cấu trúc mảng (trực quan hơn):
$ipParams = @{
InterfaceIndex = 8
IPAddress = "192.168.2.50"
PrefixLength = 24
AddressFamily = "IPv4"
}
New-NetIPAddress @ipParams
Bạn có thể dùng New-NetIPAddress để thêm địa chỉ IP thứ hai (alias) cho network adapter.
Nếu địa chỉ IP tĩnh đã được cấu hình và bạn muốn thay đổi nó, cmdlet Set-NetIPAddress được dùng:
Set-NetIPAddress -InterfaceIndex 8 -IPAddress 192.168.2.90
Để tắt lấy địa chỉ IP từ DHCP cho adapter của bạn, chạy lệnh:
Set-NetIPInterface -InterfaceAlias Ethernet0 -Dhcp Disabled
Để xem bảng định tuyến, cmdlet Get-NetRoute được dùng. Để thêm route mới, dùng cmdlet New-NetRoute:
New-NetRoute -DestinationPrefix "0.0.0.0/0" -NextHop "192.168.2.2" -InterfaceIndex 8
Để tắt giao thức IPv6 cho network adapter:
Get-NetAdapterBinding -InterfaceAlias Ethernet0 | Set-NetAdapterBinding -Enabled:$false -ComponentID ms_tcpip6
Set-DnsClientServerAddress: Đặt Địa Chỉ Primary Và Secondary DNS Server
Để đặt địa chỉ IP primary và secondary DNS server trong Windows, dùng cmdlet Set-DNSClientServerAddress. Ví dụ:
Set-DNSClientServerAddress –InterfaceIndex 8 –ServerAddresses 192.168.2.11,10.1.2.11
Bạn cũng có thể đặt nameservers dùng mảng:
$dnsParams = @{
InterfaceIndex = 8
ServerAddresses = ("8.8.8.8","8.8.4.4")
}
Set-DnsClientServerAddress @dnsParams
Sau khi thay đổi cài đặt DNS, bạn có thể xóa cache resolver:
Clear-DnsClientCache
Cách Thay Đổi Static IP Address Sang DHCP Dùng PowerShell?
Để cho phép máy tính lấy địa chỉ IP động từ DHCP server cho network adapter, chạy lệnh:
Set-NetIPInterface -InterfaceAlias Ethernet0 -Dhcp Enabled
Xóa cài đặt DNS server:
Set-DnsClientServerAddress –InterfaceIndex 8 -ResetServerAddresses
Và khởi động lại adapter để tự động lấy địa chỉ IP từ DHCP server:
Restart-NetAdapter -InterfaceAlias Ethernet0
Nếu trước đó bạn đã cấu hình default gateway, hãy xóa nó:
Set-NetIPInterface -InterfaceAlias Ethernet0| Remove-NetRoute -Confirm:$false
Cách Thay Đổi IP Address Và DNS Settings Từ Xa Với PowerShell?
Bạn có thể dùng PowerShell để từ xa thay đổi địa chỉ IP hoặc cài đặt DNS server trên nhiều máy tính từ xa. Giả sử, nhiệm vụ của bạn là thay đổi cài đặt DNS cho tất cả server trong container AD (Organizational Unit) cụ thể. Để lấy danh sách máy tính trong script dưới, cmdlet Get-ADComputer được dùng, và WinRM dùng để kết nối đến máy tính từ xa (cmdlet 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}
}
}