日期:2014-05-17  浏览次数:20833 次

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

PowerShell2.0之Windows排错(六)检查网络故障
2011年01月21日
  网络故障对于用户来说是很复杂的问题,因为它可能涉及很多方面的知识,不容易查找和解决。
  为检查网络故障,创建名为“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