我一直在寻找脚本来编写Azure实验室的构建脚本,以便可以对其进行构建,将其用于特定目的,然后将其撕下并重新开始使用。我整理在一起的脚本之一是在本地服务器和远程服务器上使用Powershell配置非集成DNS区域。我创建的脚本假定服务器上已安装DNS角色,并且服务器上已启用PSRemoting。如果不确定如何启用PSRemoting,请参阅我以前的博客文章- 在远程计算机上运行PowerShell命令

我的NonIntegratedDNSInstall.ps1脚本有助于将非集成DNS区域安装到三台服务器上。一台充当主服务器,两台充当辅助服务器。脚本中有些信息需要在脚本运行之前进行配置,并且在脚本执行期间需要一些输入。这是脚本的分解。  

在第一部分中,声明了运行脚本的工程师的输入,并具有需要创建的非集成DNS区域的静态输入:

#This stage clears any error codes within the default error variable in 电源外壳 $error.clear() #This stage looks for the server names and IP information from the engineer $dns_srv1 = Read-Host -Prompt "Enter 域名解析  Server Member 1" $dns_srv2 = Read-Host -Prompt "Enter 域名解析  Server Member 2" $dns_srv3 = Read-Host -Prompt "Enter 域名解析  Server Member 3" $dns_ip1 = Read-Host -Prompt "Enter the IP address of 域名解析  Server Member 1" $dns_ip2 = Read-Host -Prompt "Enter the IP address of 域名解析  Server Member 2" $dns_ip3 = Read-Host -Prompt "Enter the IP address of 域名解析  Server Member 3" #Input 域名解析  non-integrated zones to be created $zones = "arbroath.com","dundee.com","glasgow.com","edinburgh.com","aberdeen.com","inverness.com","stirling.com" 
```   

The next section of the script declares the script that will be run on the remote server(s) and collects credentials for connecting to the remote server(s): 

Declare script to be run on remote server $scriptblock = {param($zones,$dns_ip1,$dns_srv2,$z,$dns_srv3) Add-DnsServerSecondaryZone -MasterServers $dns_ip1 -Name $z -ZoneFile $z".dns"} #Collects Credentials to connect to the remote server $username = Read-Host -Prompt "Enter the username for the account" read-host -Prompt "enter password" -assecurestring | convertfrom-securestring | out-file C:\cred.txt $password = get-content C:\cred.txt | convertto-securestring $credentials = new-object -typename System.Management.Automation.PSCredential -argumentlist "$username",$password This line of 电源外壳 loops round using the inputs and creates the primary copy of the non-integrated 域名解析 zones on the first server: #This stage creates the non integrated zones using the first server as the primary server $zones | foreach-object {Add-DnsServerPrimaryZone -ComputerName $dnssrv1-名称$ -DynamicUpdate无-ZoneFile $ _“。dns”}


Now that the script has created the first copy of the zones the script looks to create the zone copies on the remote servers. The script checks to see if a third server is being used or not and executes the commands accordingly. 

如果dns_srv3已填充,请执行以下If($ dnssrv3){#此阶段将第二台服务器作为名称服务器添加到$ zones上方创建的区域| foreach对象{Add-DnsServerResourceRecord -Name $ -NameServer $ dnssrv2 -NS -ZoneName $} $ zones | foreach-object {Add-DnsServerResourceRecord -Name $ _ -NameServer $ dnssrv3 -NS -ZoneName $}#创建远程会话,并在辅助服务器上创建区域的副本$ session = New-PSSession -ComputerName $ dns_srv2 -Credential $ credentials ForEach($ z在$ zones中){Invoke-Command -Session $ session -ScriptBlock $ scriptblock -Arg @(($ zones,$ dns_ip1,$ dns_srv2,$ z)}#创建远程会话并将区域的副本复制到第三台服务器上。$ session2 = New-PSSession -ComputerName $ dns_srv3 -Credential $ credentials ForEach($ z在$ zones中){Invoke-Command -Session $ session2 -ScriptBlock $ scriptblock -Arg @($ zones,$ dns_ip1,$ dns_srv2,$ z,$ dns_srv3)}#删除凭证文件Remove-Item C:\ cred.txt#关闭到第二台服务器的远程会话Remove-PSSession $ Session#关闭到第三台服务器的远程会话Remove-PSSession $ Session2#这将检查脚本是否运行有错误,并相应输出。如果($ Error [0]){写主机“脚本已完成但已完成,但有错误,请调查并重新运行脚本。” -BackgroundColor黑色-ForegroundColor红色} {写主机“脚本已成功完成。” -BackgroundColor黑色-ForegroundColor黄色}} else#如果存在dnssrv3为空,脚本将执行以下操作{#此阶段将第二台服务器作为名称服务器添加到$ zones上方创建的区域| foreach对象{Add-DnsServerResourceRecord -Name $ -NameServer $ dnssrv2 -NS -ZoneName $}#创建远程会话,并在辅助服务器上创建区域的副本$ session = New-PSSession -ComputerName $ dns_srv2 -Credential $ credentials ForEach($ z在$ zones中){Invoke-Command -Session $ session -ScriptBlock $ scriptblock -Arg @($ zones,$ dns_ip1,$ dns_srv2,$ z)}#删除凭据文件Remove-Item C:\ cred.txt#关闭远程会话Remove-PSSession $ Session#此检查脚本是否运行有错误或不,并相应地输出。如果($ Error [0]){写主机“脚本已完成但已完成,但有错误,请调查并重新运行脚本。” -BackgroundColor黑色-ForegroundColor红色} {写主机“脚本已成功完成。” -BackgroundColor黑色-ForegroundColor黄色}}

```  

该脚本的一部分是删除存储远程凭据的文件,并关闭正确创建的远程会话。  

还显示输出,以供用户显示是否成功完成。以下是在两个服务器上运行的脚本的演示: //youtu.be/ZeCMCVXr-gU 该脚本可以从Microsoft的Technet Gallery下载: //gallery.technet.microsoft.com/Configure-non-integrated-ba157b59

博客评论提供者 领英.

下一篇文章 上一篇