[PowerShell Utils] Automatically Change DNS and then Join Domain
I would like to start a series of blog posts sharing PowerShell scripts to speed up our solution operations.
Today, I am going to share a script file that can select a network adapter, changes its DNS address, then join the server to the domain you specify.
Background
===============
In my environment, I have 15 Windows hosts. I need to configure them from the OS installation to configure fail over cluster and then create and run VMs on them. Without scripting, manually complete related tasks would be very bored and easy to make mistakes. I started using PowerShell and it really helped ease the pain.
Now, let's take a look at the first script that I loved using.
What can this one do
===============
For Cisco C240 servers in Durham lab, we have to use KVM Console to configure initial IP address. Then we will be able to remote to the windows hosts to operate. We want to run PowerShell remotely on all 15 hosts. But without joined to AD, remotely run powershell commands are not possible. This script will help you automatically join a host to domain. No more remote desktop, click here, click there, set this, set that, and then restart server for 15 times!
Logic Intro
===============
First, output the current server's FQDN.
Then, compare the server's domain with your domain name. If they are not the same, the script will select a network adapter which IP address starts with the address you specify.
Then, change this adapter's DNS to point to your domain controller.
Then, join this server to the domain, and automatically restart the host.
Script is here
===============
PowerShell
#
#Set your variables here.
#
$yourDomainName = "midrange.sio"
$yourNetworkInitial = "10.110.70."
$yourDomainControllerIP = "10.110.70.96"
$yourDomainUserName = "Administrator"
$yourDomainUserPassword = "Password01!"
#
#Functions defined here.
#
function OutputAllNetAdaptersInfo ()
{
foreach($adapter in (get-netadapter | ? {$_.status -eq "up"}))
{
$fields= [PSCustomObject]@{
Ip= ($adapter | Get-NetIPConfiguration).IPv4Address.IPAddress;
Name= $adapter.Name;
Description= $adapter.InterfaceDescription;
}
$fields | format-table
}
}
function FindTargetedNetAdapter($IPInitial)
{
foreach($adapter in (get-netadapter | ? {$_.status -eq "up"}))
{
$fields= [PSCustomObject]@{
Ip= ($adapter | Get-NetIPConfiguration).IPv4Address.IPAddress;
Name= $adapter.Name;
Description= $adapter.InterfaceDescription;
}
if($fields.Ip.StartsWith($IPInitial))
{
return $adapter
}
}
return null;
}
#
#Operation starts here
#
$FQDN = (Get-WmiObject win32_computersystem).DNSHostName+"."+(Get-WmiObject win32_computersystem).Domain
Write-Host "Server name is $FQDN."
if(!(Get-WmiObject win32_computersystem).Domain.ToString().compareTo($yourDomainName))
{
Write-Host "Server $FQDN is already joined domain $yourDomainName"
}
else
{
OutputAllNetAdaptersInfo;
$Tgtadapter = FindTargetedNetAdapter($yourNetworkInitial);
if($Tgtadapter)
{
$Tgtadapter | Set-DnsClientServerAddress -ServerAddresses $yourDomainControllerIP
$domain = $yourDomainName
$password = $yourDomainUserPassword | ConvertTo-SecureString -asPlainText -Force
$username = "$domain\$yourDomainUserName"
$credential = New-Object System.Management.Automation.PSCredential($username,$password)
Add-Computer -DomainName $domain -Credential $credential
Restart-Computer
}
Write-Host "No appropriate network adapter found to be used to join domain."
}
At last
=============
Works like a charm every time.
Try it, you will love it.
[PowerShell Utils] Automatically Change DNS and then Join Domain的更多相关文章
- Windows 10 & change DNS
Windows 10 & change DNS https://www.windowscentral.com/how-change-your-pcs-dns-settings-windows- ...
- Create a conditional DNS forwarder on our domain.com to Amazon default DNS provider
Backgroup: I have an AWS Managed Active Directory(domain.com). I created a DHCP options set to my d ...
- powershell加win的dns服务器,解决网站负载均衡问题
用我发明的powershell填坑法,加windows的dns服务器.从调整dns服务器解析ip时间段的角度,解决网站负载均衡问题. ------------------------win2012r2 ...
- [PowerShell Utils] Create a list of virtual machines based on configuration read from a CSV file in Hyper-V
Hello everyone, this is the third post of the series. . Background =============== In my solution, ...
- [PowerShell Utils] Remotely install Hyper-V and Failover Cluster feature on a list of windows 2012 servers
Hello everyone, this is the second post of the series. . Background =============== In my environm ...
- Powershell About Active Directory Group Membership of a domain user
使用Get-User命令去寻找group membership of a domain user $((Get-ADUser Wendy -Properties *).MemberOf -split ...
- Cross join in excel --- Copy from Internet
Set up the Workbook In this example, there are two tables -- Raw Materials and Packaging -- and each ...
- deploy a ec2 and join into domain with terraform
Below is the example to convert the ps script into userdata for terraform to create instance and aut ...
- Android之通过adb shell 模拟器 error: more than one device and emulator 改ip dns
error: more than one device and emulator 如果出现上面那种情况 请关闭 ide 输入下面的 再次重新启动 模拟器 如果实际上只有一个设备或模拟器,并且查到有 ...
随机推荐
- html (第四本书第四章参考)
上机1 <!DOCTYPE html> <html> <head lang="en"> <meta charset="UTF-8 ...
- Codeforces.786B.Legacy(线段树优化建图 最短路Dijkstra)
题目链接 \(Description\) 有\(n\)个点.你有\(Q\)种项目可以选择(边都是有向边,每次给定\(t,u,v/lr,w\)): t==1,建一条\(u\to v\)的边,花费\(w\ ...
- BZOJ.1396.识别子串(后缀自动机/后缀数组 线段树)
题目链接 SAM:能成为识别子串的只有那些|right|=1的节点代表的串. 设这个节点对应原串的右端点为r[i],则如果|right[i]|=1,即\(s[\ [r_i-len_i+1,r_i-le ...
- CS1.6找金钱和人物血量
一.查找金钱数量 先搜索800 然后购买东西,再搜索剩下的钱 然后发现有两个地址,一个绿色的地址(也就是静态地址),还有一个动态地址 经过测试后,静态地址的值是对应屏幕上的值,而真正实际的金钱是那个动 ...
- [TJOI2015]线性代数
OJ题号:BZOJ3996 题目大意: 给定一个矩阵$B_{nn}$,矩阵$C_{1n}$,存在一个01矩阵$A_{1,n}$使得$d=(A\times B-c)\times A^\mathsf{T} ...
- ASP.NET Web Pages 的冲突版本问题
随着VS版本和.NET MVC版本.EF的版本的不断更新,虽然很多功能随着版本的提升而更完善,但对于旧版本开发的软件就有点悲催了,或许很多开发者都遇到类似的问题! 最近有一个项目是用.NET MVC3 ...
- HTTP协议GET和POST的区别
from http://blog.csdn.net/whuslei/article/details/6667095 权威点的说明请参考:http://www.cs.tut.fi/~jkorpela/f ...
- The main reborn ASP.NET MVC4.0: using CheckBoxListHelper and RadioBoxListHelper
The new Helpers folder in the project, to create the CheckBoxListHelper and RadioBoxListHelper class ...
- Java Calendar,Date,DateFormat,TimeZone,Locale等时间相关内容的认知和使用(4) DateFormat
本章主要介绍DateFormat. DateFormat 介绍 DateFormat 的作用是 格式化并解析“日期/时间”.实际上,它是Date的格式化工具,它能帮助我们格式化Date,进而将Date ...
- Java从网络批量读取图片并保存至本网站服务器后再插入文章中
先来看效果: 查看一下系统操作日志 删除