zoukankan      html  css  js  c++  java
  • PowerShell2.0之Windows排错(六)检查网络故障

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

    为检查网络故障,创建名为“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所示。

    image

    图1 执行效果

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

    作者: 付海军
    出处:http://fuhj02.cnblogs.com
    版权:本文版权归作者和博客园共有
    转载:欢迎转载,为了保存作者的创作热情,请按要求【转载】,谢谢
    要求:未经作者同意,必须保留此段声明;必须在文章中给出原文连接;否则必究法律责任
    个人网站: http://txj.lzuer.com/

  • 相关阅读:
    web应用/http协议/web框架
    算法-排序-1.冒泡排序/2.选择排序/3.插入排序
    算法-基础和查找-1.汉诺塔/2.顺序查找/3.二分查找/4.顺序查找和二分查找的比较
    前端开发
    前端开发
    前端开发
    前端开发
    数据库
    LeetCode : Add Binary
    LeetCode : Length of Last Word
  • 原文地址:https://www.cnblogs.com/fuhj02/p/1941742.html
Copyright © 2011-2022 走看看