ネットワークの設定 (静的アドレス、DHCP)

参考サイト
ネットワークカードを設定する
PowerShell Tutorial 11 – Part 2: Scripting with Windows Management Instrumentation (WMI) – Methods

■静的IPを設定

$adapterName = "ローカル エリア接続"
$description = "Realtek xxxxx"
$serviceName = ""

$staticIP = "192.168.0.2"
$subnetMask = "255.255.255.0"
$gateway = "192.168.0.1"
$DNS = "123.123.123.123","123.123.123.122"

#Adapter名もしくはデバイス名からデバイス識別名を取得
$adapter = Get-WMIObject Win32_NetworkAdapter `
    | ? { $_.NetConnectionID -eq $adapterName }
if($adapter -eq $null) {
    $adapter = Get-WMIObject Win32_NetworkAdapter `
        | ? { $_.Name -eq $description }
}
$serviceName = $adapter.ServiceName

#デバイス識別名を取得出来なければ終了する。
if($serviceName -eq $null -or $serviceName.Length -le 0) { exit 1 }

#デバイス識別名からIPの設定を行う
$nic = Get-WMIObject Win32_NetworkAdapterConfiguration `
    | ? { $_.ServiceName -eq $serviceName }
$nic.EnableStatic($staticIP,$subnetMask)
$nic.SetGateways($gateway,[UInt16]1)
$nic.SetDNSServerSearchOrder($DNS)
$nic.SetDynamicDNSRegistration("TRUE")


■静的IPを設定

$adapterName = "ローカル エリア接続"
$description = "Realtek xxxxx"
$serviceName = ""

$staticIP = "192.168.0.2"
$subnetMask = "255.255.255.0"
$gateway = "192.168.0.1"
$DNS = "123.123.123.123","123.123.123.122"

#Adapter名もしくはデバイス名からデバイス識別名を取得
$adapter = Get-WMIObject Win32_NetworkAdapter `
    | ? { $_.NetConnectionID -eq $adapterName }
if($adapter -eq $null) {
    $adapter = Get-WMIObject Win32_NetworkAdapter `
        | ? { $_.Name -eq $description }
}
$serviceName = $adapter.ServiceName

#デバイス識別名を取得出来なければ終了する。
if($serviceName -eq $null -or $serviceName.Length -le 0) { exit 1 }

#デバイス識別名からIPの設定を行う
$nic = Get-WMIObject Win32_NetworkAdapterConfiguration `
    | ? { $_.ServiceName -eq $serviceName }
$nic.EnableDHCP()
$nic.SetDNSServerSearchOrder()