本文將會介紹如何使用PowerShell管理網(wǎng)絡(luò),包括設(shè)置和TCP/IP堆棧有關(guān)的選項;通過不同腳本提供網(wǎng)絡(luò)適配器的狀態(tài)信息,網(wǎng)絡(luò)適配器的連接狀態(tài)及屬性;設(shè)置靜態(tài)IP、啟動DHCP及配置DNS服務(wù)器;獲取防火墻設(shè)置信息并設(shè)置有關(guān)選項以啟用遠(yuǎn)程管理,以及遠(yuǎn)程共享文件等。
Windows Vista開始在網(wǎng)絡(luò)功能方面有了很大改善,包括新的防火墻服務(wù)及IPv6協(xié)議的增強(qiáng)支持等。同時從Windows Vista開始WMI中增加了很多用于操作防火墻和IPv6的新特性和計數(shù)器,可以顯示和使用IPv6地址。
Windows Vista和Windows Server 2008中包括強(qiáng)大的網(wǎng)絡(luò)功能,允許用戶以簡單且便捷的方式操作網(wǎng)絡(luò)。但是給網(wǎng)絡(luò)管理員帶來大量麻煩,如管理大量網(wǎng)絡(luò)適配器,如圖1所示。
圖1 需要管理大量網(wǎng)絡(luò)適配器
為了有效地管理網(wǎng)絡(luò)設(shè)備,創(chuàng)建名為“GetNetAdapterStatus.ps1”的腳本用于檢測網(wǎng)絡(luò)適配器的狀態(tài),其代碼如下:
param($computer="localhost",$help)
function funStatus($status)
{
switch($status)
{
0 { " Disconnected" }
1 { " Connecting" }
2 { " Connected" }
3 { " Disconnecting" }
4 { " Hardware not present" }
5 { " Hardware disabled" }
6 { " Hardware malfunction" }
7 { " Media disconnected" }
8 { " Authenticating" }
9 { " Authentication succeeded" }
10 { " Authentication failed" }
}
}
function funHelp()
{
$helpText=@"
DESCRIPTION:
NAME: GetNetAdapterStatus.ps1
Produces a listing of network adapters and status on a local or remote machine.
PARAMETERS:
-computerName Specifies the name of the computer upon which to run the script
-help prints help file
SYNTAX:
GetNetAdapterStatus.ps1 -computer WebServer
Lists all the network adapters and status on a computer named WebServer
GetNetAdapterStatus.ps1
Lists all the network adapters and status on local computer
GetNetAdapterStatus.ps1 -help ?
Displays the help topic for the script
"@
$helpText
exit
}
function funline ($strIN)
{
$num = $strIN.length
for($i=1 ; $i -le $num ; $i++)
{ $funline = $funline + "=" }
Write-Host -ForegroundColor yellow $strIN
Write-Host -ForegroundColor darkYellow $funline
}
if($help) { "Printing help now..." ; funHelp }
$objWMI=Get-WmiObject -Class win32_networkadapter -computer $computer
funline("Network adapters and status on $computer")
foreach($net in $objWMI)
{
Write-Host "$($net.name)"
funstatus($net.netconnectionstatus)
}
為了獲取網(wǎng)絡(luò)適配器的狀態(tài),在該腳本中使用Win32_NetWorkAdapter WMI類返回狀態(tài)代碼。并創(chuàng)建了一個名為“funStatus”的函數(shù),通過switch語句的代碼塊包括Win32_NetWorkAdapterWMI類定義的所有可能的狀態(tài)代碼。狀態(tài)代碼及其含義在Windows軟件開發(fā)包(SDK)中有詳細(xì)介紹,說明如下。
Ø 0:Disconnected(斷開)。
Ø 1:Connecting(連接中)。
Ø 2:Connected(已連接)。
Ø 3:Disconnecting(斷開中)。
Ø 4:Hardware not present(硬件不存在)。
Ø 5:Hardware disabled(硬件已禁用)。
Ø 6:Hardware malfunction(硬件故障)。
Ø 7:Media disconnected(媒介斷開)。
Ø 8:Authenticating(權(quán)限認(rèn)證中)。
Ø 9:Authentication succeeded(權(quán)限認(rèn)證成功)。
Ø 10:Authentication failed(權(quán)限認(rèn)證失。。
在該腳本中通過funStatus函數(shù)將狀態(tài)值轉(zhuǎn)換為便于理解的內(nèi)容,其執(zhí)行結(jié)果如圖2所示。
作者: 付海軍
出處:http://fuhj02.cnblogs.com
版權(quán):本文版權(quán)歸作者和博客園共有
轉(zhuǎn)載:歡迎轉(zhuǎn)載,為了保存作者的創(chuàng)作熱情,請按要求【轉(zhuǎn)載】,謝謝