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)

Weird Windows 8 Update error #2

Hmm. Still haven’t got to the bottom of this.

I decided to start updating in “blocks” from the GUI on the basis that I would quickly enough figure out which block of updates was failing on my host machine. So… starting with 49 updates, I installed several batches (starting with .Net) and all went fine until I was down to 15 updates. I then selected the bottom 8 (the last 15 are all just “Update for Windows 8” updates) and this broke my host. So I reverted (using system restore- this is important! Create as many restore points as you need to revert back to…), then tried a different random 5 and these worked. From these last 10 I then tried 2 more, but these broke my machine. My PC then went really crazy and I struggled to get it back, but now- instead of having the same 10 left, I know have 14 updates and I don’t think they’re all part of the original 15 I was down to. More to follow…

EDIT 30/MAY/2013 @ 10:14- Just tried install KB 2768703 and it failed, 100%. So there’s something up with this update, even if it’s not the one causing the update process to fail and revert back.

Weird Windows 8 Update error

So far I’ve only noticed this on Windows 8 Enterprise, so can’t comment as to whether this would affect any other flavour of W8. However, I decided to use my own PowerShell script (see previous post!) to update my W8Ent host. It seemed to be going fine but I kept on installing updates assuming that the reboot detection was working- which I’m pretty sure wasn’t  :)) Anyway, I went off to lunch and left my PC rebooting… and came back, only to find that the updates configuration was failing. I had to resort to powering off at the power switch, and eventually- after hammering F8 repeatedly- was able to do a system restore and it’s back to normal now (incidentally, the system restore process reported itself as having failed but it didn’t).

I haven’t had much luck identifying which particular update crashed it yet, but so far I think it’s KB2768703. There have been a lot of WER log entries relating to this update, it’s a critical update (matching the system restore title) and the timing is right. I’m going to restart doing updates, this time rebooting after each batch of updates…

EDIT 17-MAY-2013@16:34 : I’m pretty sure it is now KB2768703. This is the only KB installer that seems to crop up in the WER errors (application log), however I have noticed that Bing Desktop also generates WER errors so it may be that that is clashing. Anyway, I’m going to avoid installing the above update for now (using the Windows Update client so I can de-select it).

Powershell menu to perform WSUS tasks

After my previous post, WSUS is actually a bit more complicated than I’d thought so I’ve developed an entire, stand-alone WSUS script because that previous one was getting quite big.

A couple of notes: the location of the log files aren’t really important, when I started with the script it was just listing the updates inside the PowerShell windows which wasn’t very visually friendly. So I stuck them in a text file to make it easier to read. The other thing is, it doesn’t seem to run properly immediately after a reboot- I don’t know if this is just my Server Core 2012 VM being a bit sluggish, but it just does nothing if you run it within 5 or 10 minutes of rebooting. But it does work, and I’ve kept an eye on our WSUS Server and it now has all relevant updates installed.

Oh great. All the formatting has gone as well. Sorry about that, it was neatly tabbed before…

EDIT 17-MAY-2013: After some problems with my main W8 host, I’ve modified the script below to remove the option not to reboot (which I’m not sure worked anyway). So now the script automatically reboots after running a batch of updates. I’ve left the code in for interest, but would strongly advise removing the lines highlighted in red below.

======================================================================================================================================================================================
#Split in to three main sections: first of all just searches for updates, next option is to install, then final option is to display installed updates

#Equivalent to DOS CLS- just wipes the console screen
Clear-Host

#Sets the menu up- this is a 4-option menu
$menuText=”1) Display new (not yet installed) updates in Notepad”,”2) Download and install new updates”,”3) Display installed updates in Notepad”,”4) Quit”

#Displays the menu options
$menuText

do
{
#Starts the menu system- asks for user to make a choice
switch ($chooseOption=Read-Host -Prompt “Please choose your option”)
{
1 {
#Deletes the old log file- this stops the log file displaying false (too many) updates
Remove-Item C:\Support\new_WSUS_Patches.log
#Defines update criteria
$newUpdateCriteria = “IsInstalled=0 and Type=’Software'”
#Search for new updates.
$newUpdateSearcher = New-Object -ComObject Microsoft.Update.Searcher
$newSearchResults = $newUpdateSearcher.Search($newUpdateCriteria)
#Catches 0 updates error, gracefully exists when there are no new updates
If ($newSearchResults.updates.Count -eq 0) {
Clear-Host
Write-Host “I couldn’t find any updates…”
Start-Sleep 5
}
Else {
#This just performs the search, outputs the results to a text file and then opens the text file
$newSearchResults.Updates | ForEach-Object {Out-File -FilePath C:\Support\new_WSUS_Patches.log -Append -InputObject $_.Title }
Invoke-Item C:\Support\new_WSUS_Patches.log
}
cls
$menuText
}
2 {
#Define update criteria.
$newUpdateCriteria = “IsInstalled=0 and Type=’Software'”
#Search for new updates.
$newUpdateSearcher = New-Object -ComObject Microsoft.Update.Searcher
$newSearchResults = $newUpdateSearcher.Search($newUpdateCriteria)
If ($newSearchResults.updates.Count -eq 0) {
Clear-Host
Write-Host “I couldn’t find any updates…”
Start-Sleep 5
cls
$menuText
}
Else {
#Download updates.
$Session = New-Object -ComObject Microsoft.Update.Session
$Downloader = $Session.CreateUpdateDownloader()
$Downloader.Updates = $newSearchResults.Updates
$Downloader.Download()

#Install updates.
$Installer = New-Object -ComObject Microsoft.Update.Installer
$Installer.Updates = $newSearchResults.Updates
$installUpdates = $Installer.Install()
#Reboot if required by updates.
If ($Result.rebootRequired) {
Clear-Host
Write-Host “This computer needs to be restarted so will do so in 1 minute”
Start-Sleep 60
Restart-Computer
}
Else {
Clear-Host
Write-Host “Reboot NOT required!”
Start-Sleep 5
cls
$menuText
}
}
}
3 {
#Deletes the old log file
Remove-Item C:\Support\installed_WSUS_Patches.log
#Define update criteria.
$installedUpdateCriteria = “IsInstalled=1 and Type=’Software'”
#Search for installed updates.
$installedUpdateSearcher = New-Object -ComObject Microsoft.Update.Searcher
$installedSearchResults = $installedUpdateSearcher.Search($installedUpdateCriteria)
$installedSearchResults.Updates | ForEach-Object {Out-File -FilePath C:\Support\installed_WSUS_Patches.log -Append -InputObject $_.Title }
Invoke-Item C:\Support\installed_WSUS_Patches.log
cls
$menuText
}
4 {Break}
}
#As soon as the user preses 4, the script exits
} while ($chooseOption -ne 4)