您的位置:首页 > 理论基础 > 计算机网络

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

2011-01-21 23:32 357 查看
网络故障对于用户来说是很复杂的问题,因为它可能涉及很多方面的知识,不容易查找和解决。
为检查网络故障,创建名为“GetActiveNicAndConfig.ps1”的脚本,其代码如下:
param($computer = $env:computername, [switch]$full, [switch]$help)
function funline ($strIN)
{
$strLine= "=" * $strIn.length
Write-Host -ForegroundColor yellow $strIN
Write-Host -ForegroundColor darkYellow $strLine
}
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所示。



图1 执行效果
需要强调的是由于管理网络适配器的WMI管理类是存在差异,所以该脚本仅适用于Windows Vista和Windows Server 2008系统。

 
作者: 付海军
出处:http://fuhj02.blog.51cto.com
版权:本文版权归作者和51cto共有
转载:欢迎转载,为了保存作者的创作热情,请按要求【转载】,谢谢
要求:未经作者同意,必须保留此段声明;必须在文章中给出原文连接;否则必究法律责任
个人网站: http://txj.lzuer.com/
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息