PowerShell2.0之維護網絡(一)查看網絡設置

本文將會介紹如何使用PowerShell管理網絡,包括設置和TCP/IP堆棧有關的選項;經過不一樣腳本提供網絡適配器的狀態信息,網絡適配器的鏈接狀態及屬性;設置靜態IP、啓動DHCP及配置DNS服務器;獲取防火牆設置信息並設置有關選項以啓用遠程管理,以及遠程共享文件等。服務器

Windows Vista開始在網絡功能方面有了很大改善,包括新的防火牆服務及IPv6協議的加強支持等。同時從Windows Vista開始WMI中增長了不少用於操做防火牆和IPv6的新特性和計數器,能夠顯示和使用IPv6地址。網絡

Windows Vista和Windows Server 2008中包括強大的網絡功能,容許用戶以簡單且便捷的方式操做網絡。可是給網絡管理員帶來大量麻煩,如管理大量網絡適配器,如圖1所示。ide

image

圖1 須要管理大量網絡適配器函數

爲了有效地管理網絡設備,建立名爲「GetNetAdapterStatus.ps1」的腳本用於檢測網絡適配器的狀態,其代碼以下:網站

param($computer="localhost",$help).net

function funStatus($status)blog

{ip

switch($status)ci

{開發

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)

}

爲了獲取網絡適配器的狀態,在該腳本中使用Win32_NetWorkAdapter WMI類返回狀態代碼。並建立了一個名爲「funStatus」的函數,經過switch語句的代碼塊包括Win32_NetWorkAdapterWMI類定義的全部可能的狀態代碼。狀態代碼及其含義在Windows軟件開發包(SDK)中有詳細介紹,說明以下。

? 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(權限認證失敗)。

在該腳本中經過funStatus函數將狀態值轉換爲便於理解的內容,其執行結果如圖2所示。

image

圖2 執行結果

 

做者: 付海軍
出處:http://fuhj02.blog.51cto.com
版權:本文版權歸做者和51cto共有
轉載:歡迎轉載,爲了保存做者的創做熱情,請按要求【轉載】,謝謝
要求:未經做者贊成,必須保留此段聲明;必須在文章中給出原文鏈接;不然必究法律責任
我的網站: http://txj.lzuer.com/

相關文章
相關標籤/搜索