PowerShell
Edit|Remove
$ErrorActionPreference = "Continue" 
$domainName = $args[0] 
$AdminPassword = $args[1] 
 
#Write Provision Log 
MD C:\Temp 
$MyLog ="C:\Temp\ProvisionLog.txt" 
(Get-Date).ToString() + (" Parameters: ["+$args.Count.ToString()+"] Domain = $domainName Password = $password"|Out-File $MyLog -Append 
(Get-Date).ToString() + " Parameters:[" +($args.Count.ToString())+"]" |Out-File $MyLog -Append 
(Get-Date).ToString() + (" Domain: $domainname"|Out-File $MyLog -Append 
(Get-Date).ToString() + (" Password: $AdminPassword"|Out-File $MyLog -Append 
 
(Get-Date).ToString() + (" Statically setting Parameters"|Out-File $MyLog -Append 
if (!$domainName) {$domainName = "ContosoAzure.com"if (!$AdminPassword) {$AdminPassword = "Passw0rd!"} 
 
# remark the following lines if you want to use parameter values 
$domainName = "ContosoAzure.com" 
$AdminPassword = "Passw0rd1" 
 
(Get-Date).ToString() + " Firewall Rules" |Out-File $MyLog -Append 
# Firewall Rules 
New-NetFirewallRule -Name 'Allow_Ping' -DisplayName 'Allow Ping' -Protocol 'ICMPv4' -IcmpType '8' -Enabled 'True' -Profile 'Any' -Action 'Allow' -Profile 'Any'  
 
(Get-Date).ToString() + " Set-DNSClient" |Out-File $MyLog -Append 
Set-DnsClient ` 
    -InterfaceAlias "Ethernet*" ` 
    -ConnectionSpecificSuffix $domainName 
 
(Get-Date).ToString() + " Install-WindowsFeatures" |Out-File $MyLog -Append 
Install-WindowsFeature ` 
    -Name AD-Domain-Services ` 
    -IncludeManagementTools 
 
(Get-Date).ToString() + " ConvertTo-SecureString" |Out-File $MyLog -Append 
$securePassword = ConvertTo-SecureString $AdminPassword ` 
    -AsPlainText ` 
    -Force 
 
(Get-Date).ToString() + " Install-ADDSForest" |Out-File $MyLog -Append 
Install-ADDSForest ` 
    -DomainName $domainName ` 
    -SafeModeAdministratorPassword $securePassword ` 
    -Force 
 
(Get-Date).ToString() + " Finished" |Out-File $MyLog -Append 
(Get-Date).ToString() + " Restart-Computer" |Out-File $MyLog -Append 
Restart-Computer  
On a clean Windows Server machine, script will install Active Driectory Domain Services, install and configure the forest and domain.  Default values are ConstosoAzure.com for the domain but it is very easy to change by changing a couple lines of code or remarking them out and passing parameter values. Also enables Ping in the firewall and sets the DNS client sufix for the domain. Script is documented by logging what it is doing :)
NOTE: script writes passwords to plain text file so you may want to remark lines out or delete log file when done.
Script is designed to be run to create a demo environment using Azure Custom Script on the creation of a virtual machine.

Brought to you by ITProGuru Dan Stolts. (Microsoft –Chief Technology Strategist)

Blog: http://ITProGuru.com

Script Home: http://ITProGuru.com/Scripts

Accompanying Video soon on Channel 9 at https://channel9.msdn.com/Series/GuruPowerShell

 

Special ASK: Please use the Copy Button to Copy the code
Please Rate this article.  If it was helpful, flag 4 or 5 stars which are the positive values