Windows网络服务配置与管理脚本详解
1. DNS区域创建相关操作
在进行DNS区域创建时,有多种不同的配置情况,以下是对应的代码示例:
{
"adp"
{
$dnsServer.createZone($ZoneName, $primary, $adintegrated) ;
exit
}
"ads"
{
$dnsServer.createZone($ZoneName, $secondary, $adintegrated,
$null, $aryIP) ; exit
}
"adst"
{ $dnsServer.createZone($ZoneName, $stuby, $adintegrated) }
"nadp"
{
$dnsServer.createZone($ZoneName, $primary, $nonadintegrated,
$Datafile) ; exit
}
"nads"
{
$dnsServer.createZone($ZoneName, $secondary, $nonadintegrated,
$Datafile, $aryIP) ; exit
}
"nadst"
{
$dnsServer.createZone($ZoneName, $stuby, $nonadintegrated,
$Datafile) ; exit
}
DEFAULT
{
"No valid action was specified. Printing help now ..." ;
$funHelp
}
}
这里根据不同的参数组合,可以创建不同类型的DNS区域,如主区域、辅助区域、存根区域等,并且区分是否与Active Directory集成。
2. WINS和DHCP服务概述
- WINS服务 :对于许多网络管理员来说,Windows Internet Naming Service (WINS) 就像节日聚会上举止不当的亲戚,虽然想摆脱它,但又担心未知后果。在过去,当计算机网络由运行NetBEUI协议的20或30个工作站组成时,WINS很有用。但如今,随着DNS、IPv4和IPv6的发展,大多数现代应用不再需要WINS,它仅用于支持某些关键的遗留应用,且由于其逐渐被淘汰,没有额外的管理和自动化功能,只能获取服务器配置信息。
- DHCP服务 :Dynamic Host Configuration Protocol (DHCP) 服务本质上是一种“即装即用”的服务。一旦正确设置,几乎不需要维护。Windows Server 2008对DHCP的改进减少了网络管理员对DHCP的主要担忧,即通过允许减少无线接入点的租约时间来避免地址耗尽。
3. ManageWinsDHCP.ps1脚本
该脚本用于获取WINS服务器和DHCP服务器的配置信息,还能在Active Directory中授权和取消授权DHCP服务器。
3.1 参数定义
param($computer, $ip, $action, [switch]$help)
参数说明如下:
| 参数 | 说明 |
| ---- | ---- |
| -computer | 确定脚本运行的位置 |
| -ip | 仅在Active Directory中授权或取消授权DHCP服务器时使用 |
| -action | 告诉脚本要执行的操作 |
| -help | 显示帮助文本的开关参数 |
3.2 帮助函数
function funHelp()
{
$helpText=@"
DESCRIPTION:
NAME: ManageWinsDHCP.ps1
Manages DHCP and WINS servers on a local or remote machine
PARAMETERS:
-computer Specifies the name of the server to run the script
-ip
IP address of the server to run the script
-action
Specifies action to perform < shoWins, shoDHCP,
shoAllDHCP, addDHCP, deleteDHCP >
-help
prints help file
SYNTAX:
ManageWinsDHCP.ps1
Displays message an action must be specified, and lists help
ManageWinsDHCP.ps1 -computer MunichServer -action shoWins
Lists Wins Server configuration on a remote server
named MunichServer
ManageWinsDHCP.ps1 -computer MunichServer -action shoDHCP
Lists DHCP Server configuration on a remote server
named MunichServer
ManageWinsDHCP.ps1 -action shoAllDHCP
Lists all authorized DHCP servers from Active Directory
ManageWinsDHCP.ps1 -action addDHCP -computer berlin -ip 192.168.1.1
Adds a DHCP server named berlin with ip address of 192.168.1.1 to be
authorized in Active Directory
ManageWinsDHCP.ps1 -action deleteDHCP -computer berlin -ip 192.168.1.1
Removes a previously authorized DHCP server named berlin with ip address
of 192.168.1.1 from Active Directory
ManageWinsDHCP.ps1 -help
Prints the help topic for the script
"@
$helpText
exit
}
该函数用于显示脚本的帮助信息,包括脚本描述、参数说明和语法示例。
3.3 参数检查
if($help)
{ "Printing help now..." ; funHelp }
if(!$action)
{
Write-error "An action must be specified ..." ;
funHelp
}
if(!$computer)
{ Write-warning "Using default server..." }
这段代码用于检查命令行参数的存在情况。如果
$help
变量存在,则显示帮助信息;如果
$action
变量不存在,则打印错误信息并显示帮助;如果
-computer
参数未提供,则提示使用默认服务器。
3.4 开关语句
switch($action)
{
"shoWins"
{ netsh wins dump $computer }
"shoDHCP"
{ netsh dhcp show server $computer }
"shoAllDHCP" { netsh dhcp show server }
"addDHCP"
{
if(!$computer -or !$ip)
{ "Both the computer name " +
"and the IP address must be specified ..." ;
funHelp
}
netsh dhcp add server $computer $ip
}
"deleteDHCP" {
if(!$computer -or !$ip)
{ "Both the computer name " +
"and the IP address must be specified ..." ;
funHelp
}
netsh dhcp delete server $computer $ip
}
}
该开关语句根据
-action
参数的值执行不同的操作。如果是
shoWins
,则打印WINS服务器配置;如果是
shoDHCP
,则打印当前服务器的DHCP信息;如果是
shoAllDHCP
,则列出Active Directory中所有授权的DHCP服务器;如果是
addDHCP
,则需要检查
$computer
和
$ip
变量是否存在,若存在则授权DHCP服务器;如果是
deleteDHCP
,同样需要检查这两个变量,若存在则取消授权DHCP服务器。
4. 整体流程
下面是
ManageWinsDHCP.ps1
脚本的执行流程:
graph TD;
A[开始] --> B[参数定义];
B --> C[检查帮助参数];
C -- 有帮助参数 --> D[显示帮助信息];
C -- 无帮助参数 --> E[检查动作参数];
E -- 无动作参数 --> D;
E -- 有动作参数 --> F[检查计算机参数];
F -- 无计算机参数 --> G[提示使用默认服务器];
F -- 有计算机参数 --> H[根据动作参数执行操作];
H --> I[结束];
G --> H;
D --> I;
通过以上步骤,我们可以使用
ManageWinsDHCP.ps1
脚本方便地管理WINS和DHCP服务器。
5. Windows Server 2008 Server Core初始配置
在使用Windows Server 2008 Server Core时,有两个初始任务无法通过Windows PowerShell远程执行:
-
启用远程管理
:使用
netsh工具启用Windows防火墙的远程管理功能,需在本地运行以下命令:
netsh firewall set service remoteadmin enable
注意 :可能需要额外的配置信息才能连接和管理远程服务器,若远程服务器未加入域,某些WMI操作可能需要额外配置,相关信息可在Microsoft Developer Network (MSDN) 上查询。
- 获取服务器IP地址 :使用以下命令获取服务器安装操作系统时分配的IP地址:
IPconfig / all
获取IP地址并配置好防火墙后,就可以使用Windows PowerShell通过IP地址管理服务器,因为IP地址比随机分配的计算机名更便于使用。
6. 加入域操作
将Windows Server 2008 Server Core加入域可以提供集成的安全上下文,更方便地使用WMI获取信息和管理服务器。以下是
JoinDomain.ps1
脚本的详细内容。
6.1 参数定义
param(
$computer="localhost",
$domainName,
$username,
$password,
[switch]$unjoin,
[switch]$reboot,
[switch]$help
)
参数说明如下:
| 参数 | 说明 |
| ---- | ---- |
| -computer | 目标计算机,默认为localhost |
| -domainName | 计算机要加入的域或工作组名称 |
| -username | 用户凭据 |
| -password | 用户密码 |
| -unjoin | 使脚本将计算机从域中移除 |
| -reboot | 用于重启计算机 |
| -help | 显示帮助信息 |
6.2 帮助函数
function funHelp()
{
$helpText=@"
DESCRIPTION:
NAME: JoinDomain.ps1
Joins computer to domain
PARAMETERS:
-computer
Specifies the name of the computer upon which to
run the script
-domainName name of the domain or workgroup
-username
user credentials
-password
user password
-unjoin
unjoin domain or workgroup
-reboot
reboots computer
-help
prints help file
SYNTAX:
JoinDomain.ps1
Displays message you must supply an action. calls help
JoinDomain.ps1 -reboot
Reboots the local computer
JoinDomain.ps1 -reboot -computer MunichServer
Reboots the remote computer named MunichServer. Munich must
be domain joined for this command to work.
JoinDomain.ps1 -computer MunichServer -domainName
nwtraders.com -username nwtraders\administrator -password Password1
Joins a remote computer named MunichServer to the nwtraders.com domain
using the nwtraders\administrator account and password of Password1.
When the join is complete, it will reboot the machine. The computer
account is placed in default location which is by default is the
computers container.
JoinDomain.ps1 -help
Displays the help topic for the script
"@
$helpText
exit
}
该函数用于显示
JoinDomain.ps1
脚本的帮助信息,包括脚本描述、参数说明和语法示例。
6.3 重启函数
Function funReboot()
{
if($computer -ne "localhost")
{
if($username)
{
$objWMI = Get-WmiObject -Class Win32_operatingsystem `
-computername $computer -credential $username
$objWMI.psbase.Scope.Options.EnablePrivileges = $true
$objWMI.reboot()
}
else
{
$objWMI = Get-WmiObject -Class Win32_operatingsystem `
-computername $computer
$objWMI.psbase.Scope.Options.EnablePrivileges = $true
$objWMI.reboot()
}
exit
}
else
{
$objWMI = Get-WmiObject -Class Win32_operatingsystem `
-computername $computer
$objWMI.psbase.Scope.Options.EnablePrivileges = $true
$objWMI.reboot()
exit
}
}
该函数用于重启计算机,根据
$computer
变量的值判断是本地计算机还是远程计算机,并处理不同的凭据情况。
6.4 加入域函数
Function FunJoinDomain()
{
$command = "netdom join $computer /domain:$domainName " + `
"/userD:$userName /passwordD:$password"
$sdcommand = "shutdown /m \\$computer /r " + `
"/c" + "joined domain"
invoke-expression $command
start-sleep -seconds 2
"We will now reboot $computer"
Invoke-expression $sdcommand
exit
}
该函数使用
netdom
命令将计算机加入域,并在加入后重启计算机。
6.5 退出域函数
Function FunUnjoin()
{
$option = 0 # 2 = disable but not delete account in AD
$objWMI = Get-WmiObject -Class Win32_computersystem `
-computername $computer
$objWMI.psbase.Scope.Options.EnablePrivileges = $true
$objWMI.UnjoinDomainOrWorkgroup($password,$userName,$option)
exit
}
该函数使用WMI将计算机从域中移除。
6.6 参数检查
if($help)
{ "Obtaining help ..." ; funhelp }
if($domainName)
{
"Joining $computer to $domainName"
FunJoinDomain
}
if($reboot)
{
"Rebooting $computer now ..."
FunReboot
}
if(!$help -or !$domainname -or !$reboot)
{
"you must supply an action ..."
funhelp
}
这段代码用于检查命令行参数,根据不同的参数调用相应的函数。
7. JoinDomain.ps1脚本执行流程
graph TD;
A[开始] --> B[参数定义];
B --> C[检查帮助参数];
C -- 有帮助参数 --> D[显示帮助信息];
C -- 无帮助参数 --> E[检查域名参数];
E -- 有域名参数 --> F[加入域并重启];
E -- 无域名参数 --> G[检查重启参数];
G -- 有重启参数 --> H[重启计算机];
G -- 无重启参数 --> I[提示需提供操作并显示帮助];
F --> J[结束];
H --> J;
D --> J;
I --> J;
通过以上详细介绍,我们可以使用
ManageWinsDHCP.ps1
脚本管理WINS和DHCP服务器,使用
JoinDomain.ps1
脚本将Windows Server 2008 Server Core加入域并进行相关操作,提高网络服务管理的效率和便捷性。
超级会员免费看
2468

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



