[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 输入下面的 再次重新启动 模拟器 如果实际上只有一个设备或模拟器,并且查到有 ...
随机推荐
- 【翻译】 What is class diagram(什么是类图)?
[翻译] What is class diagram(什么是类图)? 写在翻译之前 这是一篇关于UML的英文博客的翻译,是我们的老师在教授我们UML类图的时候推荐给我们的,为了学习UML顺便学习英语, ...
- 用mac的chrome浏览器调试Android手机的网页
一.参考链接 read chrome remote debugging documentation 调出开发者选项 二.设置android 在安卓4.2及更新的版本中,默认情况下,[开发者选项]是隐藏 ...
- codevs 3022 西天收费站
题目描述 Description 唐僧师徒四人终于发现西天就在眼前,但猴子突然发现前面有n个收费站(如来佛太可恶),在每个收费站用不同的方式要交的钱不同,输入每个收费站的每种方法收的钱,输出最少花的钱 ...
- C++ 队列(queue)堆栈(stack)实现基础
Queue 在C++中只要#include<queue>即可使用队列类,其中在面试或笔试中常用的成员函数如下(按照最常用到不常用的顺序) 1. push 2. pop 3. size 4. ...
- hdu 4407 容斥原理
题意: 1 //一组数据 3 3 //数字为1-3,3次运算 2 2 3 //将2号位变成3 1 1 3 4 //计算1-3号位上与4互质的数的和 1 2 3 6 好题,需要重复练习 #include ...
- 快速排序的C++实现
版权声明:本文为博主原创文章,未经博主允许不得转载. 快速排序的C++实现 int Partition(int a[], int low, int high) { int x = a[high];// ...
- SLAM(一)----学习资料整理
转自:http://www.cnblogs.com/wenhust/ 书籍: 1.必读经典 Thrun S, Burgard W, Fox D. <Probabilistic robotics& ...
- 解决 PermGen space Tomcat内存设置(转)
在使用Java程序从数据库中查询大量的数据或是应用服务器(如tomcat.jboss,weblogic)加载jar包时会出现java.lang.OutOfMemoryError异常.这主要是由于应用服 ...
- 使用CefSharp在.Net程序中嵌入Chrome浏览器(八)——Cookie
CEF中的Cookie是通过CookieManager来管理的,可以用它来设置发送的Cookie. 发送Cookie 发送Cookie的一个基本示例如下: var cookieManager = _c ...
- [Deepin 15] 安装 Java 8、Git、Firefox、SafeEyes(定时提醒休息工具)
一.安装 JDK 8 1.到官网,用 迅雷下载 安装包 (jdk-8u131-linux-x64.tar.gz) 2.解压放到目录 /opt/software/jdk 3.配置环境变量 sudo vi ...