[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 输入下面的 再次重新启动 模拟器 如果实际上只有一个设备或模拟器,并且查到有 ...
随机推荐
- 使用Synchronized关键字同步类方法
要想解决“脏数据”的问题,最简单的方法就是使用synchronized关键字来使run方法同步,代码如下: public synchronized void run() { } 从上面的代码可以看出, ...
- Redis和数据库 数据同步问题
Redis和数据库同步问题 缓存充当数据库 比如说Session这种访问非常频繁的数据,就适合采用这种方案:当然了,既然没有涉及到数据库,那么也就不会存在一致性问题: 缓存充当数据库热点缓存 读操作 ...
- pygame系列_小球完全弹性碰撞游戏_源码下载
之前做了一个基于python的tkinter的小球完全碰撞游戏: python开发_tkinter_小球完全弹性碰撞游戏_源码下载 今天利用业余时间,写了一个功能要强大一些的小球完全碰撞游戏: 游戏名 ...
- sql 计算两个经纬度点之间的距离
这里用到的算法和地球半径等数据均来自网络,此文只作整理记录. 地球半径值采用赤道半径 6378137.0米,这是1980年的国际标准数据. //存储过程 CREATE FUNCTION [f_GetD ...
- 使用CefSharp在.Net程序中嵌入Chrome浏览器(八)——Cookie
CEF中的Cookie是通过CookieManager来管理的,可以用它来设置发送的Cookie. 发送Cookie 发送Cookie的一个基本示例如下: var cookieManager = _c ...
- cocos2d-x学习资源汇总
http://blog.csdn.net/akof1314 http://blog.csdn.net/bill_man/ http://blog.csdn.net/fylz1125/ MoonWa ...
- Ubuntu使用安装或者卸载软件!!!
安装软件: 1.在应用商店里面下载安装 2.在终端sudo apt-get install 软件名 3.使用ppa:加入一个ppa源:sudo add-apt-repository ppa:user/ ...
- 多个RS-485设备怎么连接到一台电脑上?可以设置地址,有协议
计算机都是RS232的,所以要给计算机串口配一个232/485转换器,然后给所有485设备设置一个地址. 计算机用查询方式,根据设备地址查询指定设备. 比如,计算机发送5个自节 01 03 04 FF ...
- WCF Routing服务,负载均衡
WCF4.0支持路由机制,通过RoutingService实现请求分发.拦截处理. 一.应用场景 1.暴露一个endpoint在外网,其余服务部署于内网: 2.请求分发,能对服务做负载功能: 二.WC ...
- 查询死锁的Session SQL 语句
/* Formatted on 2018/3/14 23:34:49 (QP5 v5.256.13226.35538) */ SELECT dob.object_name table_name , l ...