Skip to content

Commit

Permalink
Major fix: the script should now not run the cleanup process twice an…
Browse files Browse the repository at this point in the history
…d report if the service isn't running before starting.
  • Loading branch information
Mike Galvin committed Apr 23, 2019
1 parent 085b14c commit 57b93dc
Showing 1 changed file with 32 additions and 12 deletions.
44 changes: 32 additions & 12 deletions Wsus-Maintenance.ps1
@@ -1,6 +1,6 @@
<#PSScriptInfo
.VERSION 1.6
.VERSION 1.7
.GUID 56dc6e4a-4f05-414c-9419-c575f17f581f
Expand Down Expand Up @@ -122,27 +122,47 @@ If ($LogPath)
Add-Content -Path $Log -Value ""
}

## Logging
If ($LogPath)
Function WsusMaintCmd
{
Add-Content -Path $Log -Value "$(Get-Date -Format G) WSUS maintenance routine starting..."
Add-Content -Path $Log -Value ""
Get-WsusServer -Name $WsusServer -PortNumber $WsusPort | Invoke-WsusServerCleanup -CleanupObsoleteComputers -CleanupObsoleteUpdates -CleanupUnneededContentFiles -CompressUpdates -DeclineExpiredUpdates -DeclineSupersededUpdates
}

## Get the WSUS server configured and perform the maintainence operations
$WsusMaintCmd = Get-WsusServer | Invoke-WsusServerCleanup -CleanupObsoleteComputers -CleanupObsoleteUpdates -CleanupUnneededContentFiles -CompressUpdates -DeclineExpiredUpdates -DeclineSupersededUpdates
## Get the WSUS service information
$SvcName = "WsusService"
$GetSvc = Get-Service -Name $SvcName

Get-WsusServer -Name $WsusServer -PortNumber $WsusPort

## Logging
## Logging enabled
If ($LogPath)
{
$WsusMaintCmd | Out-File -Append $Log -Encoding ASCII
## Check the WSUS service status
If ($GetSvc.Status -eq "Running")
{
Add-Content -Path $Log -Value "WSUS maintenance routine starting..."
Write-Host "WSUS maintenance routine starting..."
WsusMaintCmd | Out-File -Append $Log -Encoding ASCII
}

Else
{
Add-Content -Path $Log -Value "Error: WSUS Service is not running!"
Write-Host "Error: WSUS Service is not running!"
}
}

## Logging not enabled
Else
{
$WsusMaintCmd
## Check the WSUS service status
If ($GetSvc.Status -eq "Running")
{
Write-Host "WSUS maintenance routine starting..."
WsusMaintCmd
}

Else
{
Write-Host "Error: WSUS Service is not running!"
}
}

## If log was configured stop the log
Expand Down

0 comments on commit 57b93dc

Please sign in to comment.