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

posted on   中道学友  阅读(350)  评论(0编辑  收藏  举报

编辑推荐:
· AI与.NET技术实操系列:向量存储与相似性搜索在 .NET 中的实现
· 基于Microsoft.Extensions.AI核心库实现RAG应用
· Linux系列:如何用heaptrack跟踪.NET程序的非托管内存泄露
· 开发者必知的日志记录最佳实践
· SQL Server 2025 AI相关能力初探
阅读排行:
· 震惊!C++程序真的从main开始吗?99%的程序员都答错了
· 【硬核科普】Trae如何「偷看」你的代码?零基础破解AI编程运行原理
· 单元测试从入门到精通
· 上周热点回顾(3.3-3.9)
· winform 绘制太阳,地球,月球 运作规律
历史上的今天:
2009-11-24 SharePoint基础之七- SharePoint基础架构与ASP.NET的集成
2009-11-24 SharePoint基础之六- SharePoint基础架构中涉及的ASP.NET架构
2009-11-24 [翻译]走近.NET AppDomain
2009-11-24 SharePoint基础之五- SharePoint基础架构中涉及的IIS初步

导航

< 2025年3月 >
23 24 25 26 27 28 1
2 3 4 5 6 7 8
9 10 11 12 13 14 15
16 17 18 19 20 21 22
23 24 25 26 27 28 29
30 31 1 2 3 4 5

技术追求准确,态度积极向上

点击右上角即可分享
微信分享提示