PowerShell2.0之Windows排错(六)检查网络故障

网络故障对于用户来讲是很复杂的问题,由于它可能涉及不少方面的知识,不容易查找和解决。网络

为检查网络故障,建立名为“GetActiveNicAndConfig.ps1”的脚本,其代码以下:ide

param($computer = $env:computername, [switch]$full, [switch]$help)函数

function funline ($strIN)网站

{orm

$strLine= "=" * $strIn.length对象

Write-Host -ForegroundColor yellow $strIN blog

Write-Host -ForegroundColor darkYellow $strLineip

}开发

function funHelp()字符串

{

$helpText=@"

DESCRIPTION:

NAME: GetActiveNicAndConfig.ps1

Displays

PARAMETERS:

-computer the name of the computer

-full prints complete information

-help prints help file

SYNTAX:

GetActiveNicAndConfig.ps1 -computer WebServer

Displays network adapter info and network adapter configuration info on a computer

named WebServer

GetActiveNicAndConfig.ps1

Displays network adapter info and network adapter configuration info on the local machine

GetActiveNicAndConfig.ps1 -computer WebServer -full

Displays full network adapter info and full network adapter configuration info on a computer named WebServer

GetActiveNicAndConfig.ps1 -help ?

Displays the help topic for the script

"@

$helpText

exit

}

if($help){ "Obtaining help ..." ; funhelp }

New-Variable -Name c_netConnected -value 2 -option constant

$nic = Get-WmiObject -Class win32_networkadapter -computername $computer `

-filter "NetConnectionStatus = $c_netConnected"

$nicConfig = Get-WmiObject -Class win32_networkadapterconfiguration `

-filter "interfaceindex = $($nic.interfaceindex)"

if($full)

{

funline("Full Network adapter information for $($computer)")

format-list -InputObject $nic -property [a-z]*

funline("Full Network adapter configuration for $($computer)")

format-list -InputObject $nicConfig -property [a-z]*

}

ELSE

{

funline("Basic Network adapter information for $($computer)")

format-list -InputObject $nic

funline("Basic Network adapter configuration for $($computer)")

format-list -InputObject $nicConfig

}

该脚本中使用param语句定义了3个参数,即-computer、-full和-help,其中将-computer变量设置为系统环境变量中的计算机名称。

funline函数得到输入字符串的长度,并用这个长度来生成分隔线。结果保存在$strLine变量中,而后输出带下画线的字符串。

随后的代码“New-Variable –Name c_netConnected –value 2 –option constant”建立保存已经链接网络的网络适配器数量的变量c_netConnected,其中的数值来自Windows软件开发包(SDK)。使用Get-WmiObject cmdlet并选择Win32_NetworkAdapter WMI类链接到$computer变量指定的计算机,而后查找当前已经链接的网络适配器并将结果Management对象保存在$nic变量中。可使用该对象查找相关的网络适配器配置对象,这里使用$nic.InterfaceIndex属性。由于该属性也能够用于Win32_NetworkAdapterConfiguration WMI类,将结果Management对象保存在$nicConfig变量中;另外还须要定义反馈信息数量的参数,在调用该脚本时提供了-full参数得到完整的网络适配器及其配置信息。若是没有-full参数,则仅输出每一个WMI类的默认值。这时可使用Format-List cmdlet和-InputObject参数输出信息。该脚本的执行结果如图1所示。

image

图1 执行效果

须要强调的是因为管理网络适配器的WMI管理类是存在差别,因此该脚本仅适用于Windows Vista和Windows Server 2008系统。

 

做者: 付海军
出处:http://fuhj02.blog.51cto.com
版权:本文版权归做者和51cto共有
转载:欢迎转载,为了保存做者的创做热情,请按要求【转载】,谢谢
要求:未经做者赞成,必须保留此段声明;必须在文章中给出原文链接;不然必究法律责任
我的网站: http://txj.lzuer.com/

相关文章
相关标签/搜索