[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 输入下面的 再次重新启动 模拟器 如果实际上只有一个设备或模拟器,并且查到有 ...
随机推荐
- 网页图表Highcharts实践教程之标签组与载入动画
网页图表Highcharts实践教程之标签组与载入动画 Highcharts标签组 在图表的大部分元素都提供了标签功能.但很多时候,我们需要额外说明一些信息.这个时候借助原有的图表元素的标签功能就不是 ...
- BZOJ.2007.[NOI2010]海拔(最小割 对偶图最短路)
题目链接 想一下能猜出,最优解中海拔只有0和1,且海拔相同的点都在且只在1个连通块中. 这就是个平面图最小割.也可以转必须转对偶图最短路,不然只能T到90分了..边的方向看着定就行. 不能忽略回去的边 ...
- python基础-UDP、进程、进程池、paramike模块
1 基于UDP套接字1.1 介绍 udp是无连接的,是数据报协议,先启动哪端都不会报错 udp服务端 import socket sk = socket() #创建一个服务器的套接字 sk.bind( ...
- OSX下面用ffmpeg抓取桌面以及摄像头推流进行直播
参考博客 http://blog.chinaunix.net/uid-11344913-id-4665455.html 在osx系统下通过ffmpeg查看设备 ffmpeg -f avfoundati ...
- Delphi中StrToDateTime函数TFormatSettings参数的使用
var FSetting : TFormatSettings; DateTime1: tDateTime; begin FSetting := TFormatSettings.Cr ...
- Android Service总结04 之被绑定的服务 -- Bound Service
Android Service总结04 之被绑定的服务 -- Bound Service 版本 版本说明 发布时间 发布人 V1.0 添加了Service的介绍和示例 2013-03-17 Skywa ...
- Android 实现页面跳转并传递参数教程
首先我们来看一下实现的功能: 第二,我们看一下实现这个功能,总共会接触到哪些文件和代码. 1.实现本功能总共涉及如下6个文件 2.实现本功能,总共涉及如下6个文件中的如下代码: (1) 效果: ...
- 解决克隆 centos虚拟机后修改克隆后的机器的ip、mac、uuid失败的问题
解决办法: So here's how we fix it: Remove the kernel's networking interface rules file so that it ca ...
- Golang 使用Map构建Set类型的实现方法
前言 本篇主要给大家讲述了如何利用Go语言的语法特性实现Set类型的数据结构,分享出来供大家参考学习,话不多说了,来一起看看详细的介绍吧. 需求 对于Set类型的数据结构,其实本质上跟List没什么多 ...
- Universal-Image-Loader解析(二)——DisplayImageOptions的详细配置与简单的图片加载
在使用这个框架的时候,我们必须要配置一个DisplayImageOptions对象来作为ImageLoader.getInstance().displayImage()中的参数,所以很有必要讲解这个对 ...