I would like to start a series of blogs posts sharing PowerShell scripts to speed up our solution operations.
Today, I am going to share a script file, can select a network adapter, changes its DNS address, and then join the server To the domain you specify.
Background
===============
In my environment, I has 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 being 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 does
===============
For the Cisco C240 servers in Durham Lab, we have the use of the KVM Console to configure initial IP address. Then we'll be able to remote to the Windows hosts to operate. We want to run PowerShell remotely in all the hosts. But without joined to AD, remotely run PowerShell commands is not possible. This script would help you automatically join a in host to domain. No more Remote Desktop, click here, click There, set this, set that, and then restart Server for times!
Logic Intro
===============
First, output the current server ' s FQDN.
Then, compare the server's domain with your domain name. If They is not the same, the script would select a network adapter which IP address starts with the address of you specify.
Then, the 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.#functionOutputallnetadaptersinfo(){foreach($adapterinch(Get-netadapter| ? {$_.Status-eq"Up"})) {$fields= [Pscustomobject]@{Ip= ($adapter| get-netipconfiguration).IPv4Address.IPAddress; Name= $adapter.Name; Description= $adapter.Interfacedescription; }$fields| format-table}}functionFindtargetednetadapter($IPInitial){foreach($adapterinch(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} }returnNULL;}# #Operation starts here#$FQDN= (Get-wmiobjectWin32_ComputerSystem).Dnshostname+"."+(Get-wmiobjectWin32_ComputerSystem).DomainWrite-hostthe Server name is$FQDN."if(!(Get-wmiobjectWin32_ComputerSystem).Domain.ToString ().CompareTo ($yourDomainName)){Write-hostthe Server$FQDNIs 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-objectSystem.Management.Automation.PSCredential($username,$password)Add-computer-domainname$domain-credential$credentialRestart-computer}Write-host"No appropriate network adapter found to being used to join domain."}
At the last
=============
Works like a charm every time.
Try it, you'll love it.
[PowerShell Utils] Automatically change DNS and then Join Domain