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

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

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

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

image

图1 须要管理大量网络适配器函数

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

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

function funStatus($status)blog

{ip

switch($status)ci

{开发

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

相关文章
相关标签/搜索