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的更多相关文章

  1. Windows 10 & change DNS

    Windows 10 & change DNS https://www.windowscentral.com/how-change-your-pcs-dns-settings-windows- ...

  2. 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 ...

  3. powershell加win的dns服务器,解决网站负载均衡问题

    用我发明的powershell填坑法,加windows的dns服务器.从调整dns服务器解析ip时间段的角度,解决网站负载均衡问题. ------------------------win2012r2 ...

  4. [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, ...

  5. [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 ...

  6. Powershell About Active Directory Group Membership of a domain user

    使用Get-User命令去寻找group membership of a domain user $((Get-ADUser Wendy -Properties *).MemberOf -split ...

  7. Cross join in excel --- Copy from Internet

    Set up the Workbook In this example, there are two tables -- Raw Materials and Packaging -- and each ...

  8. 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 ...

  9. Android之通过adb shell 模拟器 error: more than one device and emulator 改ip dns

    error: more than one device and emulator 如果出现上面那种情况 请关闭  ide 输入下面的  再次重新启动 模拟器 如果实际上只有一个设备或模拟器,并且查到有 ...

随机推荐

  1. Xamarin iOS教程之申请付费开发者账号下载证书

    Xamarin iOS教程之申请付费开发者账号下载证书 Xamarin iOS使用真机测试应用程序 在讲解iOS Simulator时,已经提到了虽然iOS Simulator可以模仿真实的设备,但是 ...

  2. StringBuilder的实现与技巧ZZ

      在上一篇进一步了解String 中,发现了string的不便之处,而string的替代解决方案就是StringBuilder的使用..它的使用也很简单System.Text.StringBuild ...

  3. BZOJ.1031.[JSOI2007]字符加密(后缀数组)

    题目链接 环可以拆成链:对字符串排序能想到后缀数组. 完了.输出时忽略长度不足n的串,输出s[sa[i]+n-1],即排名为i的字符串的末尾. //4140kb 744ms #include < ...

  4. JavaScript 的装饰器:它们是什么及如何使用

    请访问我的独立博客地址:https://imsense.site/2017/06/js-decorator/ 装饰器的流行应该感谢在Angular 2+中使用,在Angular中,装饰器因TypeSc ...

  5. BZOJ4280 : [ONTAK2015]Stumilowy sad

    线段树每个区间维护上下界以及要整体增加的标记即可,时间复杂度$O(m\log n)$. #include<cstdio> #define inf 1500000000 int n,m,op ...

  6. 何时调用C++复制构造函数和拷贝构造函数(转)

    1. 何时调用复制构造函数 复制构造函数用于将一个对象复制到新创建的对象中.也就是说,它用于初始化过程中,而不是常规的赋值过程中.类的复制构造函数原型通常如下: class_name(const cl ...

  7. C#Winform将WebBowser控件替换为Chrome内核

    摘要 由于最近要做一个浏览器式的软件,其中有不少地方需要使用到jQuery和BootStrap,但是在C#中,默认的WebBrowser控件默认使用的是IE的core,而低版本的IE在JS加载上总是容 ...

  8. linux后台开发核心技术

    3. 常用STL的使用 3.1. string (1)string类的实现(使用strlen.strcpy.strcat.strcmp等,注意判NULL). (2)C++字符串和C字符串的转换:dat ...

  9. 线程池大小设置,CPU的核心数、线程数的关系和区别,同步与堵塞完全是两码事

    线程池应该设置多少线程合适,怎么样估算出来.最近接触到一些相关资料,现作如下总结. 最开始接触线程池的时候,没有想到就仅仅是设置一个线程池的大小居然还有这么多的学问,汗颜啊. 首先,需要考虑到线程池所 ...

  10. EM算法与混合高斯模型

    非常早就想看看EM算法,这个算法在HMM(隐马尔科夫模型)得到非常好的应用.这个算法公式太多就手写了这部分主体部分. 好的參考博客:最大似然预计到EM,讲了详细样例通熟易懂. JerryLead博客非 ...