Powershell menu to perform inital server setup tasks

I’m quite happy getting stuck into Server 2012 core, but found that sconfig.* was a bit fiddly- it did some stuff I wasn’t interested in (participating in the CEIP for example!), and made some things more convoluted (or not user-friendly, such as setting up the networking).

So I decided to try making a powershell menu of the things I need to do most often, which is setting up the server’s static IP address, DNS servers (for joining the domain) and actually joining the domain. Here’s what I’ve got working so far- it’s fairly well commented and there’s still a few bits and bobs missing (enabling remote desktop and checking for updates on internal WSUS server for starters) but it does the basics, and automatically restarts the server for any tasks that need it:

====================================================================================
Clear-Host

$menuText=”1) Set DNS servers”,”2) Set IP address”,”3) Rename Computer*”,”4) Join domain*”,”5) Disjoin domain*”,”6) Quit”

$menuText

Write-Host “”
Write-Host “* no other operations can be performed until computer has restarted.”
Write-Host “”

do
{
switch ($chooseOption=Read-Host -Prompt “Please choose your option”)
{
1 {
Clear-Host
#Lists network adapters on the system
Write-Host “Here’s a list of your network adapters:”
get-netadapter | ft
Write-Host “”
Write-Host “”

#These variables request the name of the NIC port to configure, and the primary and secondary DNS servers
#to attach that adapter.
$nicName = read-host -Prompt “Please enter the name of the NIC you want to configure”
$dnsServerAddress01 = read-host -Prompt “Please enter primary DNS IP address”
$dnsServerAddress02 = read-host -Prompt “Please enter secondary DNS IP address”

#This line carries out the DNS assignment.
Set-DNSClientServerAddress -InterfaceAlias $nicName -ServerAddresses (“$dnsServerAddress01”, “$dnsServerAddress02”)
Clear-Host
$menuText
}
2 {
Clear-Host
#Lists network adapters on the system
Write-Host “Here’s a list of your network adapters:”
get-netadapter
Write-Host “”
Write-Host “”

#These variables request the name of the NIC port to configure, and the IP address, subnet mask and default gateway
#to attach that adapter.
#Please note the subnet mask MUST be CIDR notation- e.g. for a normal 255.255.255 network, that would be /24.
$nicName = read-host -Prompt “Please enter the name of the NIC you want to configure”
$ipAddress = read-host -Prompt “Please enter the IP address you want to use”
$subnetLength = read-host -Prompt “Please enter the subnet length as CIDR (e.g. /24)”
$defaultGateway = read-host -Prompt “Please enter the default gateway”

#This line carries out the IP configuration specified above.
New-NetIPAddress -InterfaceAlias $nicName -IPAddress $ipAddress -PrefixLength $subnetLength -DefaultGateway $defaultGateway
Clear-Host
$menuText
}
3 {
Clear-Host
#Lists current computer name
Write-Host “Here’s your current computer name:”
$oldComputerName = get-content env:computername
Write-Host $oldComputerName
Write-Host “”
Write-Host “”

#This variable requests the new name for the computer
$newComputerName = read-host -Prompt “Please enter a new name for this server”

#This line carries out the rename operation.
Rename-Computer -NewName $newComputerName -ComputerName $oldComputerName
Clear-Host
Write-Host “This server will restart in 1 minute.”
Start-Sleep -Seconds 60
Restart-Computer
}
4 {
Clear-Host
#Requests domain name
$domainName = read-host -Prompt “Please enter the domain name”
Write-Host “”
Write-Host “”

#This line carries out the domain join operation.
add-computer -DomainName $domainName
Clear-Host
Write-Host “This server will restart in 1 minute.”
Start-Sleep -Seconds 60
Restart-Computer
}
5 {
Clear-Host
#This line carries out the domain disjoin operation.
Write-Host “The following command will remove the computer from the domain.”
remove-computer
Write-Host “This server will restart in 1 minute.”
Start-Sleep -Seconds 60
Restart-Computer
}
6 {
Clear-Host
Write-Host “The setup script has stopped running.”
Break
}
}
} while ($chooseOption -ne 6)
====================================================================================