Skip to content

Instantly share code, notes, and snippets.

@hitsumabushi
Last active September 4, 2015 12:25
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save hitsumabushi/77bfe1c938c6f7f2961c to your computer and use it in GitHub Desktop.
Save hitsumabushi/77bfe1c938c6f7f2961c to your computer and use it in GitHub Desktop.
Windows update
Param([string]$interfaceName, [string]$ip, [int]$prefixLength, [string]$gateway, [string[]]$dnsServers, [string]$serverName, [string]$ntpServer)
# Utility functions
function SetIPAddress ([string]$interfaceName, [string]$ip, [int]$prefixLength, [string]$gateway){
Get-NetAdapter $interface | New-NetIPAddress -IPAddress $ip -PrefixLength $prefixlength -DefaultGateway $gateway
}
function SetDNS ([string]$interface, [string[]]$dnsServers){
Get-NetAdapter $interface | Set-DnsClientServerAddress -ServerAddresses $dnsServers
}
function DisableIPv6 ([string]$interface) {
Disable-NetAdapterBinding -InterfaceAlias $interface -ComponentID ms_tcpip6
}
function RenameServer ([string] $serverName, [switch] $restart){
if ($restart) {
Rename-Computer -NewName $serverName -Force -Restart
} else {
Rename-Computer -NewName $serverName -Force
}
}
function EnableAutoWindowsUpdate {
net stop wuauserv
cscript c:\Windows\System32\SCregEdit.wsf /AU 1
net start wuauserv
Set-Service wuauserv -startuptype "Automatic"
}
# Invoke-WebRequest -Uri "https://gist.githubusercontent.com/hitsumabushi/77bfe1c938c6f7f2961c/raw/a90b2f2e93cb92796b34c6fe7d4f102fb603257f/gistfile1.ps1" -OutFile C:\update_windows.ps1
function RunWindowsUpdate ([switch] $restart){
Write-Host "Start Windows Update"
$UpdateCollection = New-Object -ComObject Microsoft.Update.UpdateColl
$Searcher = New-Object -ComObject Microsoft.Update.Searcher
$Session = New-Object -ComObject Microsoft.Update.Session
$Searcher = New-Object -ComObject Microsoft.Update.Searcher
$results = $searcher.search("Type='software' AND IsInstalled = 0 AND IsHidden = 0 AND AutoSelectOnWebSites = 1")
# Install Update
if ($results.Updates.Count -eq 0) {
Write-Host "No Update"
# no updates.
} else {
# setup update collection
foreach ($update in $results.Updates){
$UpdateCollection.Add($update) | out-null
}
# download update items
Write-Host "Download update items"
$Downloader = $Session.CreateUpdateDownloader()
$Downloader.Updates = $UpdateCollection
$Downloader.Download()
# install update items
Write-Host "Install update items"
$Installer = New-Object -ComObject Microsoft.Update.Installer
$Installer.Updates = $UpdateCollection
$InstallationResult = $Installer.Install()
# Check Result
if ($InstallationResult.ResultCode -eq 2){
Write-Host "Update Success"
} else {
Write-Host "Some updates could not installed"
}
if ($InstallationResult.RebootRequired){
# reboot
if ($restart) {
Write-Host "Restart Computer"
Restart-Computer
}
}
Write-Host "Finish Windows Update."
}
}
function EnableWinRM {
winrm qc -force
Enable-PSRmoting -force
}
function EnableRemoteDesktop {
# Enable RDP
set-ItemProperty -Path 'HKLM:\System\CurrentControlSet\Control\Terminal Server' -name "fDenyTSConnections" -Value 0
# Enable secure RDP auth
set-ItemProperty -Path 'HKLM:\System\CurrentControlSet\Control\Terminal Server\WinStations\RDP-Tcp' -name "UserAuthentication" -Value 1
# FW Setting
New-NetFirewallRule -DisplayName "Remote Desktop" -Action "Allow" -Direction "Inbound" -Enabled True -Group "Remote Desktop" -LocalPort "3389" -Protocol "TCP"
New-NetFirewallRule -DisplayName "Remote Desktop" -Action "Allow" -Direction "Inbound" -Enabled True -Group "Remote Desktop" -LocalPort "3389" -Protocol "UDP"
}
function SetNTPServer ([string]$ntpServer){
Set-Service -Status Running -StartupType Automatic W32Time
w32tm /config /manualpeerlist:$ntpServer /syncfromflags:manual /reliable:yes /update
}
SetIPAddress $interfaceName $ip $prefixLength $gateway
SetDNS $interfaceName $dnsServers
DisableIPv6 $interfaceName
RenameServer $serverName
EnableAutoWindowsUpdate
RunWindowsUpdate
EnableWinRM
EnableRemoteDesktop
SetNTPServer $ntpServer
Restart-Computer
# Warning for using undefined variable
Set-PSDebug -strict
$UpdateCollection = New-Object -ComObject Microsoft.Update.UpdateColl
$Searcher = New-Object -ComObject Microsoft.Update.Searcher
$Session = New-Object -ComObject Microsoft.Update.Session
$Searcher = New-Object -ComObject Microsoft.Update.Searcher
$results = $searcher.search("Type='software' AND IsInstalled = 0 AND IsHidden = 0 AND AutoSelectOnWebSites = 1")
# For Check: print update contents
#$Results.Updates | ForEach-Object { $_.Identity.UpdateID ; "`t" + $_.Title }
# Install Update
if ($results.Updates.Count -eq 0) {
Write-Host "There are no updates."
} else {
# setup update collection
foreach ($update in $results.Updates){
$UpdateCollection.Add($update) | out-null
}
# download update items
Write-Host "Download update items..."
$Downloader = $Session.CreateUpdateDownloader()
$Downloader.Updates = $UpdateCollection
$Downloader.Download()
# install update items
Write-Host "Install items..."
$Installer = New-Object -ComObject Microsoft.Update.Installer
$Installer.Updates = $UpdateCollection
$InstallationResult = $Installer.Install()
# Check Result
if ($InstallationResult.ResultCode -eq 2){
Write-Host "Success!"
} else {
Write-Host "Some updates could not installed!!"
}
if ($InstallationResult.RebootRequired){
Write-Host "Reboot System."
Restart-Computer
}
Write-Host "Finish."
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment