Overblog
Editer l'article Suivre ce blog Administration + Créer mon blog

Create AD Powershell Script

Publié le par damcuvelier

A simple script to create a AD in powershell:

 

# .\CreateDom.ps1 -DomainName MyDomain -AdminPassword P@ssw0rd!

Param($DomainName,$AdminPassword)

if(!$DomainName){Write-Output "Value for Domain Name (DomainName) is mandatory"; Exit}
if(!$AdminPassword){Write-Output "Value for Administrator Password (AdminPassword) is mandatory"; Exit}

$ErrorActionPreference = "Continue"

 
#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
 
(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

 

Commenter cet article