WMI issues

MilesLindsay-4339 0 Reputation points
2025-01-09T00:29:23.8+00:00

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%)"

Windows Server PowerShell
Windows Server PowerShell
Windows Server: A family of Microsoft server operating systems that support enterprise-level management, data storage, applications, and communications.PowerShell: A family of Microsoft task automation and configuration management frameworks consisting of a command-line shell and associated scripting language.
5,598 questions
0 comments No comments
{count} votes

2 answers

Sort by: Most helpful
  1. MotoX80 34,946 Reputation points
    2025-01-09T16:34:17.9633333+00:00

    What error does "Get-WmiObject -Class Win32_OperatingSystem" give you? Is there a corresponding WMI error entry in the System or Application event log when you run that?

    Can you try something for me?

    On this forum, a common question goes like this: some user is having some problem with Windows and another user replies and says to run sfc and/or dism to see if that does anything.

    So what I tried to do was to bundle all of those common suggestions into a single Powershell script that a user could run. I called it IsWindowsHealthy.ps1

    https://zcusa.951200.xyz/en-us/answers/questions/118183/how-to-fix-error-0x80070543-in-windows-10

    I've tested it on my Win11 laptop and Win10 VM's using Powershell 5.1. Basically what I'm looking for now is "sick" pcs' to see if it detects any issues. I added in a call to "winmgmt.exe /verifyrepository" to see if it detects any WMI problems.

    I no longer have access to a WSUS or SCCM environment, so I would like to know if the Get-WUHistory call works or not.

    If you can, could you run that on one of your "problem" machines and see what it does?


  2. MilesLindsay-4339 0 Reputation points
    2025-01-12T02:10:06.0933333+00:00

    I should add the WMI issues came about from an internal work document. in which when you right click you would

    Right‐click MY COMPUTER (on the desktop or start menu) and select [ Manage ] to open

    the COMPUTER MANAGEMENT window. On a Windows machine, right click on

    Start, then select Computer Management. But the fix provided did not work for Window systems with the condition described above.

    User's image

    Fix-

    net stop ccmexec

    net stop bits

    sc config winmgmt start= disabled

    taskkill /IM CCMExec.exe /F

    net stop winmgmt /y

    net stop "tanium client"

    rd /q /s C:\Windows\system32\wbem\repository

    rd /q /s C:\Windows\softwaredistribution\Download

    md C:\Windows\softwaredistribution\Download

    rd /q /s C:\Windows\ccmcache

    md C:\Windows\ccmcache

    rd /q /s C:\Windows\ccm\temp\

    md C:\Windows\ccm\temp\

    rd /q /s C:\Windows\CCM\Staging\

    md C:\Windows\CCM\Staging\

    rd /q /s C:\Windows\CcmTemp\

    sc config winmgmt start= auto

    net start iphlpsvc

    net start wscsvc

    net start winmgmt

    sc config ccmexec start= delayed‐auto

    mofcomp "c:\program files\microsoft policy platform\extendedstatus.mof"

    net start ccmexec

    0 comments No comments

Your answer

Answers can be marked as Accepted Answers by the question author, which helps users to know the answer solved the author's problem.