[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 输入下面的 再次重新启动 模拟器 如果实际上只有一个设备或模拟器,并且查到有 ...
随机推荐
- Outdated Kotlin Runtime
你的 kotlin 运行时版本 在 1.1.2库中 是 1.1.2 然而插件版本是 1.1.4 . 运行时库 应该被更新,避免兼容问题. Outdated Kotlin Runtime Your ve ...
- BZOJ.2125.最短路(仙人掌 最短路Dijkstra)
题目链接 多次询问求仙人掌上两点间的最短路径. 如果是在树上,那么求LCA就可以了. 先做着,看看能不能把它弄成树. 把仙人掌看作一个图(实际上就是),求一遍根节点到每个点的最短路dis[i]. 对于 ...
- [HDU5714]拍照
[HDU5714]拍照 题目大意: 河上有\(n(n\le10^4)\)个船只,小明希望把尽可能多的船只都完整地拍到一张照片中. 小明位于河的边上,并且可以在河边的任意位置进行拍照,照相机的视野恰好为 ...
- SmartSVN has inconsistent newlines解决方法
SmartSVN has inconsistent newlines解决方法 点击 Project–>Setting,选择Working copy下的EOL-style,将Default EOL ...
- 【转载】C语言 构建参数个数不固定函数
深入浅出可变参数函数的使用技巧本文主要介绍可变参数的函数使用,然后分析它的原理,程序员自己如何对它们实现和封装,最后是可能会出现的问题和避免措施. VA函数(variable argument fun ...
- HDU 4737 A Bit Fun (2013成都网络赛)
A Bit Fun Time Limit: 5000/2500 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)Total S ...
- stm32f103串口实现映射功能
在实际开发中,常常遇到串口的默认输出IO口被其它模块占用了,所以我们要用到串口IO口映射功能.是指将原来实现功能的IO口映射到其它指定IO口,其它不变.详细操作例如以下: 先贴出默认下的串口初始化设置 ...
- 78-WS2812-Library (STM32F4)
78-WS2812-Library (STM32F4) //-------------------------------------------------------------- // File ...
- 关于STM32 SPI NSS的讨论
NSS分为内部引脚和外部引脚. NSS外部引脚可以作为输入信号或者输出信号, 输入信号一般用作硬件方式从机的片选, 而输出信号一般用于主SPI去片选与之相连的从SPI. NSS从设备选择有两种模式: ...
- UVA 303 Pipe
点击打开链接 题意: 求光线能达到的最大横坐标 注意光线可以和管道重合 也可以经过转折点 解法: 枚举每种光线是否能通过每个转折点的截面(线段)即可 //大白p263 #include <cma ...