Skip to content

Instantly share code, notes, and snippets.

@tanfwc
Last active December 17, 2024 08:41
Show Gist options
  • Save tanfwc/db05d526475066b9360edac6978d8f81 to your computer and use it in GitHub Desktop.
Save tanfwc/db05d526475066b9360edac6978d8f81 to your computer and use it in GitHub Desktop.
MSP - Quick Group Policy Checks
#
# Invoke-Expression (New-Object Net.WebClient).DownloadString('https://gist.githubusercontent.com/tanfwc/db05d526475066b9360edac6978d8f81/raw/quick_gpo_checks.ps1')
# Function to check network connectivity to the domain controller
function Test-NetworkConnectivity {
Write-Host "`nTesting network connectivity..." -ForegroundColor Cyan
$DomainController = (nltest /dsgetdc:$env:USERDNSDOMAIN | Select-String "DC:").ToString().Split(":")[1].Trim().TrimStart("\")
$PingResult = Test-Connection -ComputerName $DomainController -Count 2 -Quiet
if ($PingResult) {
Write-Host "Network connectivity to Domain Controller is OK." -ForegroundColor Green
} else {
Write-Host "Network connectivity to Domain Controller FAILED." -ForegroundColor Red
}
}
# Function to test DNS resolution
function Test-DNSResolution {
Write-Host "`nTesting DNS Resolution..." -ForegroundColor Cyan
$DNSStatus = Resolve-DnsName $env:USERDNSDOMAIN -Type A -ErrorAction SilentlyContinue
if ($DNSStatus) {
Write-Host "DNS resolution for $env:USERDNSDOMAIN successful." -ForegroundColor Green
} else {
Write-Host "DNS resolution for $env:USERDNSDOMAIN failed." -ForegroundColor Red
}
}
# Function to check the Group Policy Client service
function Test-GPClientService {
Write-Host "`nChecking Group Policy Client service..." -ForegroundColor Cyan
$GPService = Get-Service -Name gpsvc
if ($GPService.Status -eq 'Running') {
Write-Host "Group Policy Client service is running." -ForegroundColor Green
} else {
Write-Host "Group Policy Client service is not running. Attempting to start it..." -ForegroundColor Yellow
Start-Service -Name gpsvc
}
}
# Function to check for errors in the Event Log related to Group Policy
function Check-EventLogForGPErrors {
Write-Host "`nChecking Event Log for Group Policy Errors..." -ForegroundColor Cyan
$GPErrorEvents = Get-WinEvent -LogName "Application" | Where-Object {
$_.Message -like "*Group Policy*" -or $_.Message -like "*gpupdate*"
} | Select-Object -First 20
if ($GPErrorEvents) {
$GPErrorEvents | Format-Table TimeCreated, Message -AutoSize
} else {
Write-Host "No Group Policy errors found in Event Log." -ForegroundColor Green
}
}
# Main Debug Script
Write-Host "=== Debugging gpupdate Hang ===`n" -ForegroundColor Yellow
Test-NetworkConnectivity
Test-DNSResolution
Test-GPClientService
Check-EventLogForGPErrors
Write-Host "`n=== Debugging Completed ===" -ForegroundColor Yellow
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment