Bookmark and Share

In this post:

 

How Can I Repair WMI Installations Remotely?

Hey, Scripting Guy! Question

Hey, Scripting Guy!

I am faced with a weird issue where I have more than 600 machines in which WMI seems to be corrupted. When I did a search on the Internet, I found that it can be repaired if we log in to individual computers, stop the WMI services, rename the repository folder, and then restart the WMI services. I would like to know if there is a script that would allow me to do this remotely. I will not be able to log in to each computer because these computers are in different countries. Do you have a script to resolve this? Some errors that I see in the event logs:

WDM call returned error: 4200
WMI issue- Impersonation failed - Access denied

Will I have to re-install WMI on all these machines? Thanks in advance for any help.

-- MK

 

 

 

 

Hey, Scripting Guy! AnswerHello MK,

 

Microsoft Scripting Guy Ed Wilson here. Many years ago, it was common to delete the WMI repository while cycling the WMI services. However, this approach is not a recommended course of action. The best thing would be to try to find out what is causing the WMI corruption. A utility such as the WMI Diagnosis Utility would be useful in that regard.

The WMI team does not recommend the approach of deleting the WMI repository because you can lose configuration information. Any management tool that uses WMI could theoretically make changes to the WMI repository; these include things such as the Microsoft System Center family of products as well as third party tools. Many desktop and server makers create custom WMI classes for their management utilities, and these things would most likely need to be re-installed.

There have been many improvements to WMI stability that could be available for you to implement via hotfixes and service packs. The WMI Diagnosis Utility (wmidiag.vbs) will assist you in tracking down what is actually going on with your computer. You might even have a DCOM problem that would not be solved by deleting the WMI repository, and therefore you could be setting yourself up for a lot of needless work.

Here is a good article from our WMI team about the errors you are seeing. The errors by themselves do not mean you need to "re-install" WMI. You may wish to see if a particular script is causing the error. It might be that you are trying to perform a privileged operation such as loading a driver without supplying the proper WMI privilege string.

If after examining your script and checking out the WMI Diagnosis Utility you are unable to find a resolution, the best course of action would be for you to open a support incident with Microsoft to track down the root cause.

 

The WMI Diag tool V.2.0 works on Windows Vista and Windows Server 2008 RTM and before. It does not work on Windows XP sp3, nor does it work on Windows 7 or Windows Server 2008 R2. See the WMIDiag.doc file included with the download for more information about the tool.

 


 

How Can I Determine CPU Usage Information with WMI? 

Hey, Scripting Guy! Question

Hey, Scripting Guy!

I have a question for you. I am trying to use WMI to generate CPU and memory usage information for processes running on a system. Obtaining the memory usage information is easy; I just work with the WorkingSetSize property of the Win32_Process WMI class. I cannot figure out, though, what property I should be looking at to try to determine CPU usage. MSDN has not been much help, unless I am just looking in the wrong place. Do you have any ideas to help me find the CPU utilization of a process?

-- JV

 

 

 

 

Hey, Scripting Guy! AnswerHello JV,

 

Win32_process does not expose that information. I wrote How Can I Identify and Delete an Instance of a Process? in which I talk about the CPU utilization of various processes. The script uses Windows PowerShell, and the Get-Process cmdlet to expose the information.

I also have a Quick-Hits Friday post that illustrates using WMI and VBScript to obtain this information.

 


 

Can I Use Windows PowerShell to Add a Path Value to a Windows XP Registry Key?

 

Hey, Scripting Guy! Question

Hey, Scripting Guy!

Help! Is it possible to use a Windows PowerShell script to append to an existing path value to a Windows XP registry key? Suppose I have an existing key value, "C:\Windows", and I want to add "C:\temp" to create a resulting value of C:\WINDOWS;C:\temp". I have looked high and low on many different Web sites with no luck. Time to ask the master!

-- RG

 

 

Where Should I Store My Windows PowerShell Functions?

Hey, Scripting Guy! Question

Hey, Scripting Guy! 

Is there a place on my computer where I can put Windows PowerShell scripts containing some of the functions I have created so that I can simply reference the functions? Alternatively, do I have to copy the functions into each script in order for each script to be able to use them?

-- DS

 

 

 

 

Hey, Scripting Guy! AnswerHello DS,

In Windows PowerShell 2.0, use a module to store your functions. You can also dot source a script containing functions. We will be talking about modules the week of January 18. Dot sourcing of scripts was discussed in the Hey, Scripting Guy! Why Would I Even Want to Use Functions in My Windows PowerShell Scripts? post.  


 

Help Me Write a Funtion That Accepts and Returns an Array of Strings Based on Ping Response

Hey, Scripting Guy! Question

Hey, Scripting Guy! 

How would I write a function that accepts an array of strings containing the names of computers and returns an array of strings that excludes any computers that did not respond to a ping? I currently have the following code, which does the details of pinging the list of computers, but I don’t know how to build a new array of strings as a result, and I don’t know how to return the new array. Can you help me? Here is my code:

 

function Remove-DeadHosts {
    param( [string[]]$computers )

    foreach( $computer in $computers ) {
        $ping = new-object System.Net.NetworkInformation.Ping
        $Reply = $ping.send($computer)
        if ($Reply.status –eq “Success”)
        {
            "Online"
        }
        else
        {
        "Offline"
        }
        $Reply = ""
    }
}

 

The reason I am writing this function is that I am working on a number of other functions that do things such as WMI query remote computers for a list of installed software, etc. When I attempt to query a machine that is either not on or is protected by a firewall, the script gets angry and throws an exception. I do not know how to handle these exceptions so I try to avoid them by scrubbing my list of computers. Is there a better way to be doing all of this?

 

-- SD

 

  

Hey, Scripting Guy! AnswerHello SD,

Sorry to hear about your angry script. I would do something like the ModifiedRemove-DeadHosts.ps1 script seen here.

ModifiedRemove-DeadHosts.ps1

# -----------------------------------
# modifiedRemove-DeadHosts.ps1 script
# ed wilson, msft, 12/17/2000
# based upon DS script
# QHF
# ------------------------------------
function Remove-DeadHosts {
    param( [string[]]$computers )

    foreach( $computer in $computers ) {
        $ping = new-object System.Net.NetworkInformation.Ping
        $Reply = $ping.send($computer)
        if ($Reply.status –eq “Success”)
        {
            [string[]]$online += $computer
        } #end if
        else
        {
          [string[]]$offline += $computer + ","
        } #end else
      
    } #end foreach
     $online
     $offLine
} #end Remove-Deadhosts
$rtn = Remove-DeadHosts -computers "192.168.1.100","192.168.1.101", "192.168.1.112"
"Online hosts: `r`n$($rtn[0])`r`n"
"Offline hosts: `r`n$($rtn[1])"

 

 

Help Me Troubleshoot This Script I'm Creating with the ADSI Scriptomatic

Hey, Scripting Guy! Question

Hey, Scripting Guy! 

I am using the ADSI Scriptomatic and am having some issues. I have created a test user named “Jim Shoe.” This user is seen in the following image.

Image of user Jim Shoe

I have changed the script in the ADSI Scriptomatic to reflect this, as seen in the following image.

Image of user Jim Shoe in ADSI Scriptomatic

The problem is that I keep getting the error seen in the following image.

Image of error CF is receiving

Am I missing something?

-- CF

 

 

Hey, Scripting Guy! AnswerHello CF,

I hate it when that happens. It looks as if your script is correct, as far as I can tell. I am going to guess that your user name needs to be something like this:

Cn=jim shoe, ou=informationTechnology …

You need to confirm the exact name of your user object. To do this, create a Microsoft Management Console (MMC) and add the adsiedit snapin to it. Then select the jim shoe user object and view the DistinguishedName of your user object. Be sure that your script is trying to connect to that exact name.

Why are we doing this? Because your error indicates there is no such object on the server, your adsi binding string cannot find your user object. To help you see what your binding string is doing, you may want to use wscript.echo to display your connection string.  

Well, this concludes another edition of Quick-Hits Friday. It also concludes another exciting week on the Script Center. Join us next week as we delve into the mysteries of…well, we will let that remain a mystery for now.

If you want to know exactly what we will be covering on Monday, follow us on Twitter or Facebook. If you have any questions, send e-mail to us at scripter@microsoft.com or post your questions on the Official Scripting Guys Forum. See you on Monday. Until then, have an awesome weekend.

 

Ed Wilson and Craig Liebendorfer, Scripting Guys