The business where I work has several computers in our environment which have not getting cumulative updates in months and there are not items in the ccmcache folder. I wrote a very basic script to which checks if the WMI is "working" on a number of hosts. If WMI is not working for those computers, I re-register the MOF. and the .dll. I've have been doing this the last couple of months and most of start getting their updated. But our SCCM admin (who does not use powershell) insisted I will be doing damage by doing this "at will". 1. I am not doing it at will 2. I am only re-registering, not resetting 3. SCCM starts working and I can see the Cumulative updates installed. He says look at CCMSetup.log and don't do the mof re-register unless the log file indicates. Is there some where I can show that for those systems which show blank WMI properties that this simple remediation works or maybe he correct and I need to find a different approach? Thank you.
# Stop the WMI service
net stop winmgmt
# Re-register DLL files
cmd.exe /c "for %i in (%windir%\system32\wbem*.dll) do regsvr32 /s %i"
# Re-register EXE files
#cmd.exe /c "for %i in (%windir%\system32\wbem*.exe) do %i /RegServer"
# Compile MOF and MFL files
cmd.exe /c "for %i in (%windir%\system32\wbem*.mof %windir%\system32\wbem*.mfl) do mofcomp %i"
# Start the WMI service again
net start winmgmt
# Define the script location
$scriptLocation = $MyInvocation.MyCommand.Path
$scriptLocation = Split-Path -Path $scriptLocation -Parent
# Define the locations of the input files
$hnamesLocation = "$scriptLocation\hnames.txt"
$csvhnamesLocation = "$scriptLocation\hostnames.csv"
# Import the CSV file and filter for the current computer
$CSVFile = Import-CSV $csvhnamesLocation -Header @("computer","os") | Where-Object { $_.computer -like $env:COMPUTERNAME }
Write-Host $CSVFile
# Read the host names from the text file
$names = Get-Content $hnamesLocation
# Get the current date and time
$curDate = Get-Date -Format "yyyyMMdd HHmmss"
# Set the report save location and title
$outputLocation = "$scriptLocation\WMIStatus - $curDate.csv"
Set-Content -Path $outputLocation -Value "Computer,DNS,WMI Status"
# Get the number of computers in hosts.txt
$numOfEntries = $names.Count
$count = 0
$onlineCount = 0
# Initialize counters for various statuses
$offlineCount = 0
$questionableCount = 0
$dnsFailureCount = 0
$goodWMIFailureCount = 0
$goodNoWMIcount = 0
$questionableWMIFailureCount = 0
$questionableNoWMIcount = 0
$dnsFailureWMIFailureCount = 0
$dnsFailureNoWMIcount = 0
# Function to split an IP address string into an array
function Split-IPAddress {
param (
[string]$addressString
)
return $addressString -split '\s+'
}
# Main part of Script
# Loop through each name in the $names array
ForEach($n in $names) {
$count++
# Display the current count and the name being processed
Write-Host -NoNewline "$count of $numOfEntries | $n | "
# Check if the computer is online
If (Test-Connection -BufferSize 32 -Count 1 -ComputerName $n -Quiet) {
# Resolve the DNS name to get the IP address
$usdaIP = (Resolve-DnsName $n).IPAddress
Write-Host $usdaIP
# Join the IP addresses into a single string
$usdaIPString = $usdaIP -join ", "
# Check if the IP address string contains spaces (indicating multiple addresses)
if ($usdaIPString -match ' ') {
# Split the IP address string into an array
$splitAddresses = Split-IPAddress -addressString $usdaIPString
# Extract the IPv4 address
$usdaIP = "IPv4 Address: $($splitAddresses[1])"
# Match the IPv4 address format
if ($usdaIP -match "\d{1,3}(.\d{1,3}){3}") {
$usdaIP = $matches[0]
}
} else {
$usdaIP = $usdaIPString
}
try {
# Perform a reverse DNS lookup
$dnsResult = Resolve-DnsName $usdaIP -errorAction Stop
Write-Host $dnsResult
if ($dnsResult) {
# Extract the hostname and split it to get the first part
$reverseLookup = ($dnsResult.NameHost -split '.')[0]
Write-Host $reverseLookup
# Check if the reverse lookup matches the computer name
If ($reverseLookup -eq $n) {
$onlineCount++
Write-Host -NoNewLine " Online " -BackgroundColor Green -ForegroundColor Black
Write-Host -NoNewline " DNS Good " -BackgroundColor Green -ForegroundColor Black
# Use Invoke-Command to check WMI on the remote computer
$WMIStatus = Invoke-Command -ComputerName $n -ScriptBlock {
Get-WmiObject -Class Win32_OperatingSystem
}
if ($WMIStatus) {
Write-Host -NoNewline " WMI Status Good`n" -BackgroundColor Green -ForegroundColor Black
Add-Content -Path $outputLocation -Value "$n,Good,WMI Good"
} else {
Write-Host -NoNewline " WMI Failure`n" -BackgroundColor Red -ForegroundColor Black
Add-Content -Path $outputLocation -Value "$n,Good,WMI Failure"
$goodWMIFailureCount++
}
} else {
Write-Host -NoNewLine " Online " -BackgroundColor Green -ForegroundColor Black
Write-Host -NoNewLine " DNS Questionable " -BackgroundColor Yellow -ForegroundColor Black
Write-Host " Expected: $n, Found: $reverseLookup"
# Use Invoke-Command to check WMI on the remote computer
$WMIStatus = Invoke-Command -ComputerName $n -ScriptBlock {
Get-WmiObject -Class Win32_OperatingSystem
}
if ($WMIStatus) {
Write-Host -NoNewline " WMI Status Good`n" -BackgroundColor Green -ForegroundColor Black
Add-Content -Path $outputLocation -Value "$n,Questionable,WMI Good"
} else {
Write-Host -NoNewline " WMI Failure`n" -BackgroundColor Red -ForegroundColor Black
Add-Content -Path $outputLocation -Value "$n,Questionable,WMI Failure"
$questionableWMIFailureCount++
}
}
}
} catch {
# Handle DNS lookup failure
Write-Host -NoNewLine " Online " -BackgroundColor Green -ForegroundColor Black
Write-Host -NoNewLine " DNS Lookup Failed " -BackgroundColor Red -ForegroundColor Black
# Use Invoke-Command to check WMI on the remote computer
$WMIStatus = Invoke-Command -ComputerName $n -ScriptBlock {
Get-WmiObject -Class Win32_OperatingSystem
}
if ($WMIStatus) {
Write-Host -NoNewline " WMI Status Good`n" -BackgroundColor Green -ForegroundColor Black
Add-Content -Path $outputLocation -Value "$n,LookupFailed,WMI Good"
} else {
Write-Host -NoNewline " WMI Failure`n" -BackgroundColor Red -ForegroundColor Black
Add-Content -Path $outputLocation -Value "$n,LookupFailed,WMI Failure"
$dnsFailureWMIFailureCount++
}
}
} else {
# Handle offline computers
Write-Host "Offline" -BackgroundColor Red -ForegroundColor Black
Add-Content -Path $outputLocation -Value "$n,Offline,Unknown"
$offlineCount++
}
}
# Calculate percentages
$percentGoodWMIFailure = [math]::Round(($goodWMIFailureCount / $numOfEntries) * 100, 2)
$percentQuestionableWMIFailure = [math]::Round(($questionableWMIFailureCount / $numOfEntries) * 100, 2)
$percentDnsFailureWMIFailure = [math]::Round(($dnsFailureWMIFailureCount / $numOfEntries) * 100, 2)
# Display the summary
Write-Host "Total computers processed: $count"
Write-Host "Total online: $onlineCount"
Write-Host "Total offline: $offlineCount"
Write-Host "Good WMI Failure: $goodWMIFailureCount ($percentGoodWMIFailure%)"
Write-Host "Questionable WMI Failure: $questionableWMIFailureCount ($percentQuestionableWMIFailure%)"
Write-Host "DNS Failure WMI Failure: $dnsFailureWMIFailureCount ($percentDnsFailureWMIFailure%)"
Write-Host "Total online computers: $onlineCount"
# Log additional details to the output file
Add-Content -Path $outputLocation -Value "Number of good computers with WMI failure: $goodWMIFailureCount ($percentGoodWMIFailure%)"
Add-Content -Path $outputLocation -Value "Number of questionable computers with WMI failure: $questionableWMIFailureCount ($percentQuestionableWMIFailure%)"
Add-Content -Path $outputLocation -Value "Number of DNS failure computers with WMI failure: $dnsFailureWMIFailureCount ($percentDnsFailureWMIFailure%)"