Server Setup PowerShell Script

In this post we will be looking at creating a simple Power Shell setup script for the initial configuration of a newly imaged server.

What the script does:

The script takes you through the process of easily imputing your chosen configurations for the settings listed below:

  • Sets IPV4 Network Adapter Settings
  • [optional] Enable Remote Desktop
  • [optional] Installs File Server feature so files on the server can be accessed remotely
  • [optional] Join Domain and Rename Server

The benefit of using this script is, it’s vastly quicker, you won’t need to hassle yourself with the OS gui and you can change all the settings from one single Powershell instance.

Also I’ve made it relatively user-friendly, whereby you can double check inputs before proceeding and in the event you’re unhappy with any of your inputs, you will have the choice to re-input them without having to restart the script.

Enjoy –

Clear
#Removes any previous password file
Remove-Item -Path C:\mysecurestring.txt -ErrorAction SilentlyContinue

#Inputs for creds/domain
Do {
$Cname = Read-Host "Enter the intended name for the Server " #Ask for source Hyper-V server with current hosts
$Domain = Read-Host "Enter your domain <contoso.com>  "
$user = Read-Host "Enter your admin account username <admin01> "
$username = "$Domain\$user"
Read-host "Enter your password (It will be stored in an encrypted file C:\mysecurestring.txt)"  -assecurestring | convertfrom-securestring | out-file C:\mysecurestring.txt
Write-host " "
Write-host "Your inputs: $Cname | $Domain | $user" 
$answer = Read-host "Are you happy with your inputs <yes/no>? " 
    
    If ($answer -eq "yes")
    {
        $success = 1 }
    Elseif ($answer -eq "no")
    {
        Clear
        Write-host "--Cleared for re-input--"
        $success = 0}
   }
    While ($success -eq 0) 

#Inputs for adapter
Do {
Write-host "-Server adapter(s) info-"
Get-NetAdapter
Write-host " "  
$Adapter = Read-Host "Enter the intended adapter  "
$IP = Read-Host "Enter intended IP for the server "
$Subnet = Read-Host "Enter subnet mast (prefix length) <24> "
$DefaultG = Read-Host "Enter the default gateway "
$DNS = Read-Host "Enter your primary DNS "
$DNS2 = Read-Host "Enter your secondary DNS "
Write-host " "
Write-host "Your inputs: $Adapter | $IP | $Subnet | $DefaultG | $DNS | $DNS2"
$answer = Read-host "Are you happy with your inputs <yes/no>? "

    If ($answer -eq "yes")
    {
        $success = 2 }
    Elseif ($answer -eq "no")
    {
        Clear
        Write-host "--Cleared for re-input--"
        Write-host "Your inputs: $Cname | $Domain | $user" 
        $success = 1}
    } 
    While ($success -eq 1) 

Write-host " "
Write-host "Your current inputs: "
Write-host "$Cname | $Domain | $user" 
Write-host "$Adapter | $IP | $Subnet | $DefaultG | $DNS | $DNS2"

$OldCName = $env:COMPUTERNAME
$password = cat C:\mysecurestring.txt | convertto-securestring
$cred = new-object -typename System.Management.Automation.PSCredential -argumentlist $username, $password

#Configuring Network adapter
Write-Host " "
Write-Host "-Configuring $Adapter settings-"
New-NetIPAddress -InterfaceAlias $Adapter -IPAddress $IP -PrefixLength $Subnet -DefaultGateway $DefaultG
Set-DnsClientServerAddress -InterfaceIndex 12 -ServerAddresses $DNS,$DNS2

Write-host " "
Write-host "-Adapter Settings Completed-"
Write-host " "

Write-host " "
$remote = Read-Host "Do you want to setup Remote Desktop <yes|no> "

if($remote -eq "yes"){

#Enable remote desktop
set-ItemProperty -Path 'HKLM:\System\CurrentControlSet\Control\Terminal Server'-name "fDenyTSConnections" -Value 0
Enable-NetFirewallRule -DisplayGroup "Remote Desktop"
netsh advfirewall firewall set rule name="Remote Desktop - User Mode (TCP-In)" new enable =Yes profile="domain,private"
netsh advfirewall firewall set rule name="Remote Desktop - User Mode (UDP-In)" new enable =Yes profile="domain,private"
set-ItemProperty -Path 'HKLM:\System\CurrentControlSet\Control\Terminal Server\WinStations\RDP-Tcp' -name "UserAuthentication" -Value 1
Write-Host "-Remote desktop setup-"
}
Write-host " "
$FileS = Read-Host "Do you want to join install the feature 'File Server' Enabling access to files on the server over the network? "
if($FileS -eq "yes"){
Install-WindowsFeature -name FS-FileServer
} 

Write-host " "
$domain = Read-Host "Do you want to join the server to the domain? "

if($domain -eq "yes"){

#Join domain/Rename
Rename-Computer -NewName $Cname
sleep 5
Add-Computer -ComputerName $OldCName -DomainName $Domain -credential $cred -force -Options JoinWithNewName,AccountCreate -restart
} 

#Removes password file
Remove-Item -Path C:\mysecurestring.txt -ErrorAction SilentlyContinue
    

 

How to get the script on a new virtual machine?

One way is to attach a USB containing the script on your chosen hyper-visor or you can attach an ISO file. Check out my guide on How to Create an ISO . For physical servers, USB will do fine 😉

 

 


Thanks for reading – feel free to follow and stay updated 🙂  View sysadminguides’s profile on Facebook View GuidesSysadmin’s profile on Twitter View 115372466162675927272’s profile on Google+

2 thoughts on “Server Setup PowerShell Script

  1. Pingback: Remove and automatically Re-add Computer from Domain using PowerShell scripts | Windows SysAdmin Hub

  2. Pingback: Updated – Server Setup Script | Windows SysAdmin Hub

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google+ photo

You are commenting using your Google+ account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s