Skip to content

Commit

Permalink
Administrator check, auto install sqlserver module, unencrypted optio…
Browse files Browse the repository at this point in the history
…nnal for WID,

Check if the script as been ran with Administrator's privilege
auto install sqlserver module if not available (with question)
unencrypted optionnal for WID (Windows Internal Database is still unencrypted if running locally, allowing it
  • Loading branch information
tunisiano187 authored Feb 16, 2024
1 parent d3ffecf commit 85fe513
Showing 1 changed file with 33 additions and 7 deletions.
40 changes: 33 additions & 7 deletions Optimize-WsusServer.ps1
Original file line number Diff line number Diff line change
@@ -1,5 +1,3 @@
#Requires -modules SqlServer

<#
.SYNOPSIS
Comprehensive Windows Server Update Services (WSUS) configuration and optimization script.
Expand Down Expand Up @@ -41,9 +39,9 @@ Declines all updates that have been approved and are superseded by other updates
Creates a scheduled task to run the OptimizeDatabase function weekly.
.NOTES
Version: 1.2.1
Author: Austin Warren
Creation Date: 2020/07/31
Version: 1.2.2
Author: Fabian
Last update Date: 2024/02/16
.EXAMPLE
Optimize-WsusServer.ps1 -FirstRun
Expand Down Expand Up @@ -81,6 +79,34 @@ param (
[switch]
$DeclineSupersededUpdates
)

#Check if script is running with Administrator priveliges
Write-host "Checking to make sure you have Local Admin rights" -foreground yellow
If (-NOT ([Security.Principal.WindowsPrincipal] [Security.Principal.WindowsIdentity]::GetCurrent()).IsInRole([Security.Principal.WindowsBuiltInRole] "Administrator"))
{
Write-Warning "Please run this script as an Administrator!"
If (!($psISE)){"Press any key to continue…";[void][System.Console]::ReadKey($true)}
Exit 1
}

#Check if the sqlmodule is installed for invoke-sqlcmd
$Module=Get-Module -Name SqlServer -ListAvailable
if($Module.count -eq 0)
{
Write-Host "SqlServer Powershell module is not available" -ForegroundColor yellow
$Confirm= Read-Host Do you want to install this module? [Y] Yes [N] No
if($Confirm -match "[yY]")
{
Write-host "Installing SqlServer PowerShell module..."
Install-Module SqlServer -Repository PSGallery -Scope CurrentUser -AllowClobber -Force
}
else
{
Write-Host "SqlServer PowerShell module is required to run this script. Please install module using Install-Module SqlServer cmdlet."
Exit
}
}

#----------------------------------------------------------[Declarations]----------------------------------------------------------

# Recommended IIS settings: https://www.reddit.com/r/sysadmin/comments/996xul/getting_2016_updates_to_work_on_wsus/
Expand Down Expand Up @@ -437,11 +463,11 @@ function Optimize-WsusDatabase {

Write-Host "Creating custom indexes in WSUS index if they don't already exist. This will speed up future database optimizations."
#Create custom indexes in the database if they don't already exist
Invoke-Sqlcmd -query $createCustomIndexesSQLQuery -ServerInstance $serverInstance -QueryTimeout 120
Invoke-Sqlcmd -query $createCustomIndexesSQLQuery -ServerInstance $serverInstance -QueryTimeout 120 -Encrypt Optional

Write-Host "Running WSUS SQL database maintenence script. This can take an extremely long time on the first run."
#Run the WSUS SQL database maintenance script
Invoke-Sqlcmd -query $wsusDBMaintenanceSQLQuery -ServerInstance $serverInstance -QueryTimeout 40000
Invoke-Sqlcmd -query $wsusDBMaintenanceSQLQuery -ServerInstance $serverInstance -QueryTimeout 40000 -Encrypt Optional
}

function New-WsusMaintainenceTask($interval) {
Expand Down

0 comments on commit 85fe513

Please sign in to comment.