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

时间: 2025-01-05 06:57:22

[PowerShell Utils] Automatically Change DNS and then Join Domain的相关文章

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

【Powershell】【DNS】通过Powershell远程注册静态DNS

代码通过调用DNS服务器的wmi端口远程注册静态DNS记录 $dnsServerName="08dc01.mok.com" $containerName="mok.com" $dns =[WmiClass]"\\08dc01.mok.com\root\MicrosoftDNS:MicrosoftDNS_ResourceRecord" $dns.CreateInstanceFromTextRepresentation($dnsServerName,

Powershell About Active Directory Group Membership of a domain user

使用Get-User命令去寻找group membership of a domain user $((Get-ADUser Wendy -Properties *).MemberOf -split (“,”) | Select-String -SimpleMatch “CN=”) -replace “CN=”,”” 扩展1?:获取在群组Wendy和群组Gaga中的所有用户 Get-ADUser -Filter * -SearchScope Subtree -SearchBase "dc=xx,

Powershell AWS 自动化管理 (9) - Route 53

最后再看看如何用PowerShell 管理AWS的DNS服务. Route 53的启用很简单,可以在AWS上注册新的域名 也可以在其他网站注册新的域名再迁徙过来.豆子在GoDaddy上已经有一个域名 beanxyz.com了,管理界面挪到Route 53 很简单,在Route 53新建一个HostedZone beanxyz.com, 他会自动生成对应的NS 记录,在我的GoDaddy里面把NS记录指向AWS的NS就行了. 手动配置很简单,如下所示,我创建了一个blog.beanxyz.com

Powershell AWS 自动化管理 (12) - 创建一个高可用的WordPress博客(下)

这个是PowerShell创建AWS高可用博客的第三部分,我们来看看后半截工作是怎么完成的. 创建EC2-S3的Role,这个Role是分配给EC2虚拟机的,这样他们创建之后自动就有权限访问S3的内容. 创建VPC网络 创建VPC的2个子网,位于不同的AZ 创建Internet网关 配置路由表 创建并配置EC2的Security Group,确保80和22端口可用 创建高可用的MariaDB数据库 配置数据库的Security Group,确保3306端口可用 创建S3 Bucket 并配置Po

改变查看修改安卓手机的DNS

This post is a continuation to my previous post on "[How to] Change DNS Server on Android phones?". The earlier post was about changing the DNS Server when you are connected using WiFi. For a 3G connection, a different set of properties needs to

DNS Configuration for the SCAN used with Oracle RAC Database 11g Release 2

This article provides the basic DNS configuration steps necessary to use the Single Client Access Name (SCAN) introduced in Oracle 11g Release 2 RAC. Please consider the following caveats before following the instructions here: I'm not an network guy

[python网络编程]DNS服务器

在上一篇中,使用scrapy修改源IP发送请求的最后我们提到由于hosts文件不支持正则,会导致我们的随机域名DNS查询失败.使用DNS代理服务器可以解决这个问题, 下面是我用gevent写的小工具,很简单.我们只拦截匹配的A记录,然后发送DNS Response,如果不匹配,那么我们服务器就是一个DNS代理,转发请求. # -*- coding=utf-8 -*- import struct from cStringIO import StringIO from collections imp

部署正向解析DNS脚本

#!/bin/bash #This is DNS server script#Make KingApple Li#tel 1501032**** #change hostname and network sed -i '2c HOSTNAME=ns.duanyufei.org' /etc/sysconfig/network sed -i '$a DNS1=192.168.10.10' /etc/sysconfig/network-scripts/ifcfg-eth0 /etc/init.d/ne