• PowerShell2.0之维护网络(一)查看网络设置


    本文将会介绍如何使用PowerShell管理网络,包括设置和TCP/IP堆栈有关的选项;通过不同脚本提供网络适配器的状态信息,网络适配器的连接状态及属性;设置静态IP、启动DHCP及配置DNS服务器;获取防火墙设置信息并设置有关选项以启用远程管理,以及远程共享文件等。

    Windows Vista开始在网络功能方面有了很大改善,包括新的防火墙服务及IPv6协议的增强支持等。同时从Windows Vista开始WMI中增加了很多用于操作防火墙和IPv6的新特性和计数器,可以显示和使用IPv6地址。

    Windows Vista和Windows Server 2008中包括强大的网络功能,允许用户以简单且便捷的方式操作网络。但是给网络管理员带来大量麻烦,如管理大量网络适配器,如图1所示。

    image

    图1 需要管理大量网络适配器

    为了有效地管理网络设备,创建名为“GetNetAdapterStatus.ps1”的脚本用于检测网络适配器的状态,其代码如下:

    param($computer="localhost",$help)

    function funStatus($status)

    {

    switch($status)

    {

    0 { " Disconnected" }

    1 { " Connecting" }

    2 { " Connected" }

    3 { " Disconnecting" }

    4 { " Hardware not present" }

    5 { " Hardware disabled" }

    6 { " Hardware malfunction" }

    7 { " Media disconnected" }

    8 { " Authenticating" }

    9 { " Authentication succeeded" }

    10 { " Authentication failed" }

    }

    }

    function funHelp()

    {

    $helpText=@"

    DESCRIPTION:

    NAME: GetNetAdapterStatus.ps1

    Produces a listing of network adapters and status on a local or remote machine.

    PARAMETERS:

    -computerName Specifies the name of the computer upon which to run the script

    -help prints help file

    SYNTAX:

    GetNetAdapterStatus.ps1 -computer WebServer

    Lists all the network adapters and status on a computer named WebServer

    GetNetAdapterStatus.ps1

    Lists all the network adapters and status on local computer

    GetNetAdapterStatus.ps1 -help ?

    Displays the help topic for the script

    "@

    $helpText

    exit

    }

    function funline ($strIN)

    {

    $num = $strIN.length

    for($i=1 ; $i -le $num ; $i++)

    { $funline = $funline + "=" }

    Write-Host -ForegroundColor yellow $strIN

    Write-Host -ForegroundColor darkYellow $funline

    }

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

    $objWMI=Get-WmiObject -Class win32_networkadapter -computer $computer

    funline("Network adapters and status on $computer")

    foreach($net in $objWMI)

    {

    Write-Host "$($net.name)"

    funstatus($net.netconnectionstatus)

    }

    为了获取网络适配器的状态,在该脚本中使用Win32_NetWorkAdapter WMI类返回状态代码。并创建了一个名为“funStatus”的函数,通过switch语句的代码块包括Win32_NetWorkAdapterWMI类定义的所有可能的状态代码。状态代码及其含义在Windows软件开发包(SDK)中有详细介绍,说明如下。

    Ø 0:Disconnected(断开)。

    Ø 1:Connecting(连接中)。

    Ø 2:Connected(已连接)。

    Ø 3:Disconnecting(断开中)。

    Ø 4:Hardware not present(硬件不存在)。

    Ø 5:Hardware disabled(硬件已禁用)。

    Ø 6:Hardware malfunction(硬件故障)。

    Ø 7:Media disconnected(媒介断开)。

    Ø 8:Authenticating(权限认证中)。

    Ø 9:Authentication succeeded(权限认证成功)。

    Ø 10:Authentication failed(权限认证失败)。

    在该脚本中通过funStatus函数将状态值转换为便于理解的内容,其执行结果如图2所示。

    image

    图2 执行结果

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

  • 相关阅读:
    Windows Phone本地数据库(SQLCE):3、[table]attribute(翻译) (转)
    深入理解 Node.js 中 EventEmitter源码分析(3.0.0版本)
    深入理解 Getter和Setter 对对象的属性监听
    深入理解Proxy 及 使用Proxy实现vue数据双向绑定
    深入理解 ES6中的 Reflect
    深入理解 Object.defineProperty 及实现数据双向绑定
    Egg入门学习(三)---理解中间件作用
    学习使用PM2管理nodejs进程
    Egg入门学习(二)---理解service作用
    Egg入门学习(一)
  • 原文地址:https://www.cnblogs.com/fuhj02/p/1942261.html
Copyright © 2020-2023  润新知