PowerShell2.0之維護網絡(二)處理適配器配置

在處理全部適配器的狀態後,還能夠查詢每一個網絡適配器的詳細配置信息。能夠經過選擇「控制面板」|「網絡和共享中心」|「網絡鏈接」選項,打開「網絡鏈接」窗口。在其中顯示每一個適配器的詳細信息並作相應調整,如圖1所示。網絡

image

圖1 在「網絡鏈接」窗口中查看每一個適配器的詳細信息ide

建立名爲「GetNetAdapterConfig.ps1」的腳本收集特定網絡適配器的用於排錯的詳細信息,而且經過指定關鍵字僅返回有關網絡適配器的特定配置信息,其代碼以下:函數

param($computer="localhost",$query,$help)網站

function funHelp()命令行

{orm

$helpText=@"對象

DESCRIPTION:blog

NAME: GetNetAdapterConfig.ps1dns

Produces a listing of network adapter configuration informationip

on a local or remote machine.

PARAMETERS:

-computer Specifies the name of the computer to run the script

-help prints help file

-query the type of query < ip, dns, dhcp, all >

SYNTAX:

GetNetAdapterConfig.ps1 -computerName WebServer

Lists default network adapter configuration on a

computer named WebServer

GetNetAdapterConfig.ps1 -computerName WebServer -query IP

Lists IPaddress, IPsubnet, DefaultIPgateway, MACAddress

on a computer named WebServer

GetNetAdapterConfig.ps1 -computerName WebServer -query DNS

Lists DNSDomain, DNSDomainSuffixSearchOrder, DNSServerSearchOrder,

DomainDNSRegistrationEnabled on a computer named WebServer

GetNetAdapterConfig.ps1 -computerName WebServer -query DHCP

Lists Index,DHCPEnabled, DHCPLeaseExpires, DHCPLeaseObtained,

DHCPServer on a computer named WebServer

GetNetAdapterConfig.ps1 -computerName WebServer -query ALL

Lists all network adapter configuration information on a computer

named WebServer

GetNetAdapterConfig.ps1 -help ?

Prints the help topic for the script

"@

$helpText

exit

}

if($help) { "Printing help now..." ; funHelp }

$class="win32_networkadapterconfiguration"

$IPproperty="IPaddress, IPsubnet, DefaultIPgateway, MACAddress"

$dnsProperty="DNSDomain, DNSDomainSuffixSearchOrder, `

DNSServerSearchOrder, DomainDNSRegistrationEnabled"

$dhcpProperty="Index,DHCPEnabled, DHCPLeaseExpires, `

DHCPLeaseObtained, DHCPServer"

if($query)

{

switch($query)

{

"ip" { $query="Select $IPproperty from $class" }

"dns" { $query="Select $dnsProperty from $class" }

"dhcp" { $query="Select $dhcpProperty from $class" }

"all" {

$query = "Select * from $class" ; `

Get-WmiObject -Query $query | format-list * ;

exit

}

DEFAULT {

$query = "Select * from $class" ; `

Get-WmiObject -Query $query ; exit

}

}

}

ELSE

{

$query = "Select * from $class" ; `

Get-WmiObject -Query $query ; exit

}

Get-WmiObject -query $query | format-table [a-z]* -AutoSize

該腳本使用param語句定義了3個參數,即-computer、-query及-help。其中設置-computer的默認值爲localhost。若是用戶未指定計算機,則默認返回本地計算機的網絡配置。

接下來針對幫助信息建立的funhelp函數用於輸出幫助信息,當用戶未輸入參數或輸入錯誤的參數時,調用該函數提示輸入的錯誤及正確的輸入。腳本中使用if語句檢查$help變量是否存在,由於只有用戶在調用腳本時指定了-help參數,該變量纔會存在。隨後初始化用於WMI查詢的變量,這些變量的存在是爲了便於在後面對命令行應用中實現靈活調用。在用戶輸入的參數中包括-query參數時,腳本將會調用switch語句判斷$query變量值。並執行相應的操做,輸出ip、dns、dhcp和全部條目的信息。在$query變量無輸入的狀況下還特別使用ELSE語句將WMI對象中的全部屬性發送給Get-WmiObject cmdlet查詢,在控制檯輸出當前計算機中的全部網絡鏈接的信息。在代碼的最後將Get-WmiObject cmdlet查詢的結果經過管道傳遞給Format-Table cmdlet格式化輸出,在使用Format-Tables時須要指定符合Get-WmiObject cmdlet輸出代碼格式的參數。該腳本的執行結果如圖2和圖3所示。

image

圖2 查詢當前主機DHCP信息

image

圖3 未指定檢索項默認會輸出全部的網絡適配器信息

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

相關文章
相關標籤/搜索