Powershell menu to perform inital server setup tasks #2

Having heavily modified this script, I thought it best to re-paste it.

This is now an 8-option menu; I’ve added the first 3 in and swapped options 4 and 5, it seemed more sensible.

Just a word of warning: I got into a bit of a state configuring the static IP address (options 3 & 4), whatever I did I was getting stuck with a private MS (169.254) address. This is because I’d somehow set the IP, then removed just the ip with Remove-NetIPAddress. If you need to use Remove-NetIPAddress, I would highly recommend using the following as New-NetIPAddress seems to refuse to run if you try and add another new gateway (makes sense, but why Remove-NetIPAddress can’t just erase everything is beyond me):

Remove-NetIPAddress -InterfaceAlias MyNetworkPort -IPAddress 1.2.3.4 -PrefixLength 24 -DefaultGateway 4.3.2.1

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

$menuText=”1) List current IP addresses”,”2) Rename a network port”,”3) Switch DHCP off on a network port”,”4) Set IP address”,”5) Set DNS servers”,”6) Rename Computer*”,”7) Join domain*”,”8) 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 {
#This just lists all the IP addresses on the system.
Clear-Host
Get-NetIPAddress -AddressFamily IPV4| More

Write-Host “Press any key to continue…”
$pressAnyKey = $host.UI.RawUI.ReadKey(“NoEcho,IncludeKeyDown”)

Clear-Host
$menuText
}
2 {
Clear-Host

#Prompts user to enter the old network adapter name, and the desired network adapter name.
$oldNicName = read-host -Prompt “Please enter the old name for the NIC port”
$newNicName = read-host -Prompt “Please enter the new name for the NIC port”

#Renames the adapter according to the variable above
Rename-NetAdapter -Name $oldNicName -NewName $newNicName | Sort-Object Name

Clear-Host
$menuText
}
3 {
#This allows you to switch off DHCP on a NIC port of your choice.
Clear-Host

#Prompts user to enter the network adapter name.
$NicName = read-host -Prompt “Please enter the name for the NIC port you want to make static”

#Renames the adapter according to the variable above
Set-NetIPInterface $NicName -DHCP Disabled

Clear-Host
$menuText
}
4 {
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 our lans, that would be /22.
$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. /22, just type 22)”
$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
}
5 {
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
}
6 {
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 computer will automatically restart in 1 minute”
Start-Sleep -Seconds 60
Restart-Computer
}
7 {
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 nmgw.ac.uk
Clear-Host
Write-Host “This computer will automatically restart in 1 minute”
Start-Sleep -Seconds 60
Restart-Computer
}
8 {Break}
}
} while ($chooseOption -ne 8)

Advertisements

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 )

Twitter picture

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

Facebook photo

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

Google+ photo

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

Connecting to %s