Windows PowerShell 脚本:IP 地址与 DNS 设置配置指南
1. 设置静态 IP 地址
1.1 脚本参数设置
在将计算机加入域后,需要设置静态 IP 地址。可以使用
Get-WmiObject
cmdlet 检索 WMI 类以辅助此过程。在
SetIP.ps1
脚本中,首先要创建命令行参数,代码如下:
param(
$computer="localhost",
$ip,
$sm,
$dg,
[switch]$list,
[switch]$help
)
参数说明如下:
| 参数 | 说明 |
| ---- | ---- |
| -computer | 要运行脚本的计算机名称,默认为本地计算机(localhost) |
| -ip | 要配置的 IP 地址 |
| -sm | 要配置的子网掩码 |
| -dg | 要配置的默认网关 |
| -list | 查询所有网络适配器并报告其配置 |
| -help | 打印帮助文件 |
1.2 帮助文本函数
创建
funHelp()
函数来显示帮助信息,代码如下:
function funHelp()
{
$helpText=@"
DESCRIPTION:
NAME: SetIP.ps1
Sets a static IP address on a local or remote machine.
PARAMETERS:
-computer Specifies the name of the computer upon which to run the script
-ip IP address to configure
-sm Subnet mask to configure
-dg Default gateway to configure
-list Queries all network adapters and reports their configuration
-help prints help file
SYNTAX:
SetIP.ps1
Displays message an action is required, and calls help
SetIP.ps1 -list -computer MunichServer
Lists all the network adapters and their configuration on a computer named MunichServer
SetIP.ps1 -ip "10.0.0.1" -sm "255.0.0.0" -dg "10.0.0.5"
Sets the Ip address to 10.0.0.1 and the subnet mask to 255.0.0.0 and the default Gateway to 10.0.0.5 on the local machine
SetIP.ps1 -help
Displays the help topic for the script
"@
$helpText
exit
}
1.3 评估返回值函数
FunEvalRTN()
函数用于将方法调用的返回值从整数转换为更易理解的字符串,代码如下:
function FunEvalRTN($rtn)
{
Switch ($rtn.returnvalue)
{
0 { Write-Host -foregroundcolor green "No errors for $strCall" }
66 { Write-Host -foregroundcolor red "$strCall reports invalid subnetMask" }
67 { Write-Host -foregroundcolor red "$strCall reports an error occurred processing request" }
70 { Write-Host -ForegroundColor red "$strCall reports invalid IP" }
71 { Write-Host -ForegroundColor red "$strCall reports invalid gateway" }
91 { Write-Host -ForegroundColor red "$strCall reports access denied" }
96 { Write-Host -ForegroundColor red "$strCall reports unable to contact dns server" }
DEFAULT { Write-Host -ForegroundColor red "$strCall service reports ERROR $($rtn.returnValue)" }
}
$rtn=$strCall=$null
}
1.4 查询网络适配器信息函数
funlist()
函数用于执行两个单独的 WMI 查询,以获取网络适配器和其配置信息,代码如下:
Function funlist()
{
Write-host "Listing Network adapters on $($computer) `n"
Get-WmiObject -Class win32_networkadapter -computername $computer | format-list [a-z]*
Write-host "Listing network adapter configuration on $($computer) `n"
Get-WmiObject -Class win32_networkadapterconfiguration -computername $computer | format-list [a-z]*
exit
}
1.5 参数检查与 IP 配置
在脚本中,需要检查命令行参数。如果指定了
-help
参数,则调用
funHelp()
函数;如果指定了
-list
参数,则调用
funlist()
函数。如果
-ip
、
-sm
或
-dg
参数缺失,则打印提示信息并调用
funHelp()
函数。代码如下:
if($help) { funhelp }
if($list) { funlist }
if(!$ip -or !$sm -or !$dg)
{
"An action is required ... "
funhelp
}
接下来,创建
Win32_NetworkAdapterConfiguration
WMI 类的实例,以配置网络适配器的 IP 地址和默认网关,代码如下:
$global:RTN = $null
$metric = [int32[]]1
$objWMI = Get-WmiObject -Class win32_networkadapterconfiguration -computer $computer -filter "ipenabled = 'true'"
$RTN=$objwmi.EnableStatic($ip, $sm)
$strCall="enable static IP and subnet mask"
FunEvalRTN($rtn)
$RTN=$objwmi.SetGateways($dg, $metric)
$strCall="enable set default gateway and metric"
FunEvalRTN($rtn)
下面是设置静态 IP 地址的流程 mermaid 图:
graph TD;
A[开始] --> B[设置脚本参数];
B --> C[检查 -help 参数];
C -- 是 --> D[调用 funHelp() 函数并退出];
C -- 否 --> E[检查 -list 参数];
E -- 是 --> F[调用 funlist() 函数并退出];
E -- 否 --> G[检查 -ip、-sm、-dg 参数是否缺失];
G -- 是 --> H[打印提示并调用 funHelp() 函数并退出];
G -- 否 --> I[创建 WMI 类实例];
I --> J[调用 EnableStatic() 方法设置 IP 和子网掩码];
J --> K[评估返回值];
K --> L[调用 SetGateways() 方法设置默认网关];
L --> M[评估返回值];
M --> N[结束];
2. 配置 DNS 设置
2.1 脚本参数设置
在
SetDNS.ps1
脚本中,首先要创建命令行参数,代码如下:
param(
$computer="localhost",
$dnsdomain,
$dnsServer,
$dnsSuffix,
[switch]$list,
[switch]$help
)
参数说明如下:
| 参数 | 说明 |
| ---- | ---- |
| -computer | 要连接的计算机名称,默认为本地计算机(localhost) |
| -dnsdomain | 要在客户端计算机上配置的 DNS 域名 |
| -dnsserver | 主 DNS 服务器的 IP 地址 |
| -dnssuffix | DNS 后缀 |
| -list | 查询所有绑定 IP 的网络适配器 |
| -help | 显示帮助信息 |
2.2 帮助文本函数
创建
funHelp()
函数来显示帮助信息,代码如下:
function funHelp()
{
$helpText=@"
DESCRIPTION:
NAME: SetDNS.ps1
Sets DNS configuration on a local or remote machine.
PARAMETERS:
-computer Specifies the name of the computer upon which to run the script
-list Queries all IP bound network adapters
-dnsserver Dns server
-dnsDomain DNS domain name
-dnsSuffix The dns suffix
-help prints help file
SYNTAX:
SetDNS.ps1
Displays a message an action is required and calls help
SetDNS.ps1 -list -computer MunichServer
Lists all the network adapters and configuration on a computer named MunichServer
SetDNS.ps1 -dnsServer "10.0.0.2" -dnsDomain "nwtraders.com" -dnsSuffix "nwtraders.com"
Sets the dns server to 10.0.0.2, the dnsDomain to nwtraders.com, the dns search suffix to nwtraders.com on the local machine
SetDNS.ps1 -dnsServer "10.0.0.2" -dnsDomain "nwtraders.com" -dnsSuffix "nwtraders.com" -computer munichServer
Sets the dns server to 10.0.0.2, the dnsDomain to nwtraders.com, the dns search suffix to nwtraders.com on a remote computer named munichserver
SetDNS.ps1 -help
Displays the help topic for the script
"@
$helpText
exit
}
2.3 评估返回值函数
评估返回值的
FunEvalRTN()
函数与
SetIP.ps1
脚本中的相同,代码如下:
function FunEvalRTN($rtn)
{
Switch ($rtn.returnvalue)
{
0 { Write-Host -foregroundcolor green "No errors for $strCall" }
66 { Write-Host -foregroundcolor red "$strCall reports invalid subnetMask" }
70 { Write-Host -ForegroundColor red "$strCall reports invalid IP" }
71 { Write-Host -ForegroundColor red "$strCall reports invalid gateway" }
91 { Write-Host -ForegroundColor red "$strCall reports access denied" }
96 { Write-Host -ForegroundColor red "$strCall reports unable to contact dns server" }
DEFAULT { Write-Host -ForegroundColor red "$strCall service reports ERROR $($rtn.returnValue)" }
}
$rtn=$strCall=$null
}
2.4 查询网络适配器信息函数
funlist()
函数与
SetIP.ps1
脚本中的相同,代码如下:
Function funlist()
{
Write-host "Listing Network adapters on $($computer) `n"
Get-WmiObject -Class win32_networkadapter -computername $computer | format-list [a-z]*
Write-host "Listing network adapter configuration on $($computer) `n"
Get-WmiObject -Class win32_networkadapterconfiguration -computername $computer | format-list [a-z]*
exit
}
2.5 参数检查与 DNS 配置
在脚本中,需要检查命令行参数。如果指定了
-help
参数,则调用
funHelp()
函数;如果指定了
-list
参数,则调用
funlist()
函数。如果
$dnsdomain
、
$dnsserver
或
$dnssuffix
变量缺失,则打印提示信息并调用
funHelp()
函数。代码如下:
if($help) { funhelp }
if($list) { funlist }
if(!$dnsdomain -or !$dnsServer -or !$dnsSuffix)
{
"An action is required ... "
funhelp
}
接下来,声明变量并连接到 WMI,以配置 DNS 设置,代码如下:
$global:RTN = $null
$namespace = "root\cimv2"
$class = "win32_networkadapterconfiguration"
$objWMI = Get-WmiObject -Class $class -namespace $namespace -computername $computer -filter "ipenabled = 'true'"
$RTN=$objwmi.SetDNSDomain($dnsdomain)
$strCall="Setting the DNS domain name"
FunEvalRTN($rtn)
$RTN=$objwmi.SetDNSServerSearchOrder($dnsServer)
$strCall="Set the dns server search order"
FunEvalRTN($rtn)
$wmiclass = "\\$computer" + "\" + $namespace + ":" + $class
$wmi = [wmiclass]"$wmiclass"
$rtn = $wmi.SetDNSSuffixSearchOrder($dnsSuffix)
$strCall="Set the dns suffix search order"
FunEvalRTN($rtn)
下面是配置 DNS 设置的流程 mermaid 图:
graph TD;
A[开始] --> B[设置脚本参数];
B --> C[检查 -help 参数];
C -- 是 --> D[调用 funHelp() 函数并退出];
C -- 否 --> E[检查 -list 参数];
E -- 是 --> F[调用 funlist() 函数并退出];
E -- 否 --> G[检查 $dnsdomain、$dnsserver、$dnssuffix 变量是否缺失];
G -- 是 --> H[打印提示并调用 funHelp() 函数并退出];
G -- 否 --> I[声明变量并连接到 WMI];
I --> J[调用 SetDNSDomain() 方法设置 DNS 域名];
J --> K[评估返回值];
K --> L[调用 SetDNSServerSearchOrder() 方法设置 DNS 服务器搜索顺序];
L --> M[评估返回值];
M --> N[调用 SetDNSSuffixSearchOrder() 方法设置 DNS 后缀搜索顺序];
N --> O[评估返回值];
O --> P[结束];
3. 函数复用技术探讨
3.1 函数复用的 dot - sourcing 技术
在编写脚本时,有时会需要复用其他脚本中的函数。一种常见的技术是 dot - sourcing(点源),也被一些语言称为包含(include)。例如在编写
SetDNS.ps1
脚本时,其实可以复用
SetIP.ps1
脚本中的
FunEvalRTN()
函数,而不必复制粘贴代码。
使用 dot - sourcing 技术,只需要在脚本中使用一个句点、一个空格和要包含的脚本的路径,示例如下:
. c:\fso\functionlib.ps1
这样,
functionlib.ps1
脚本中的函数就可以在当前脚本中使用了。
3.2 dot - sourcing 技术示例
下面通过两个示例脚本来演示 dot - sourcing 技术。
3.2.1
FunctionLib.ps1
脚本
在
FunctionLib.ps1
脚本中定义了两个函数
addOne()
和
addTwo()
,代码如下:
function addOne($intIN)
{
$intIN ++
$intIN
}
function addTwo($intIN)
{
$intIn+=2
$intIn
}
3.2.2
CallFunctionLib.ps1
脚本
CallFunctionLib.ps1
脚本通过 dot - sourcing 技术调用
FunctionLib.ps1
脚本中的函数,代码如下:
. c:\fso\functionlib.ps1
addone(1)
addtwo(2)
get-childitem function:\
在
CallFunctionLib.ps1
脚本中,调用
addone(1)
和
addtwo(2)
就像调用普通函数一样。
3.3 dot - sourcing 技术的优缺点
| 优点 | 缺点 |
|---|---|
| 节省复制粘贴函数代码的麻烦 | 包含的脚本必须始终伴随调用脚本,限制了脚本的可移植性 |
| 故障排除更困难,因为需要考虑两个脚本文件 | |
| 脚本更难阅读,需要打开两个脚本才能跟踪潜在问题 |
3.4 函数复用的最佳实践
在编写
SetDNS.ps1
脚本时,为了使脚本完全可移植,不建议使用 dot - sourcing 技术将
FunEvalRTN()
函数放在包含文件中,而是直接将该函数复制粘贴到
SetDNS.ps1
脚本文件中。
4. 总结
4.1 脚本功能总结
本文介绍了两个重要的 Windows PowerShell 脚本
SetIP.ps1
和
SetDNS.ps1
的编写和使用。
-
SetIP.ps1 脚本
:用于设置本地或远程计算机的静态 IP 地址、子网掩码和默认网关。通过一系列函数和参数检查,确保脚本的正确执行。
-
SetDNS.ps1 脚本
:用于配置本地或远程计算机的 DNS 设置,包括 DNS 域名、DNS 服务器和 DNS 后缀。同样通过函数和参数检查来保证配置的准确性。
4.2 函数复用总结
探讨了函数复用的 dot - sourcing 技术,虽然该技术有节省代码编写的优点,但也存在可移植性差、故障排除困难和阅读性差等缺点。在实际编写脚本时,需要根据具体情况选择合适的函数复用方式。
4.3 整体流程总结
下面是一个整体的流程图,展示了设置 IP 地址和 DNS 配置的主要流程:
graph LR;
classDef startend fill:#F5EBFF,stroke:#BE8FED,stroke-width:2px;
classDef process fill:#E5F6FF,stroke:#73A6FF,stroke-width:2px;
classDef decision fill:#FFF6CC,stroke:#FFBC52,stroke-width:2px;
A([开始]):::startend --> B{选择操作}:::decision;
B -->|设置 IP 地址| C(SetIP.ps1 脚本):::process;
B -->|配置 DNS 设置| D(SetDNS.ps1 脚本):::process;
C --> E(参数检查):::process;
E -->|参数完整| F(配置 IP 地址和网关):::process;
E -->|参数缺失| G(显示帮助信息):::process;
D --> H(参数检查):::process;
H -->|参数完整| I(配置 DNS 设置):::process;
H -->|参数缺失| J(显示帮助信息):::process;
F --> K([结束]):::startend;
G --> K;
I --> K;
J --> K;
通过以上的脚本和流程,网络管理员可以方便地使用 Windows PowerShell 来配置计算机的 IP 地址和 DNS 设置,提高网络管理的效率和准确性。
超级会员免费看
2126

被折叠的 条评论
为什么被折叠?



