Showing posts with label errors. Show all posts
Showing posts with label errors. Show all posts

Wednesday, September 2, 2015

PowerShell – Hanging Get-WmiObject

 

I ran into something that I didn’t know was an issue with Get-WmiObject. I have been using Get-WmiObject for quite sometime so this was a shock to me. I had written a script to grab the description from servers and dump them to a gridview.

I wanted to run this on 1400 servers. Meaning know it was going to take awhile. When I ended up running it, it took too long, actually waaaay to long. The reason? Some of the Get-WmiObject calls were hanging for what ever reason. No problem I thought lets assign the timeout argument to the Get-WmiObject? Where is the timeout argument? ??? After a bit of searching and reading I learned there is no way to set a timeout to the Get-WmiObject commandlet.

After some more reading I learned that the best way to handing this is to setup the Get-WmiObject as a Job. The job can handle the Get-WmiObject process and if it takes longer than you think it should, kill the job and move on.

I don’t have the old way I was running the script but this is the new way.. I tried to comment it as much as I could.

 

    # try this and catch it if it fails

    try {

        # Set the try to stop if it error out

        $ErrorActionPreference = 'Stop'

       

              

        # start the Job and assign it to a variable.. 

 

        # -scriptBlock -- assign the -scriptblock argument to show what to run.. this is where the

        #     get-wmiobject is run. This is encapsualted in {}

 

        # param  -- set the param to pass along the server name from the -ArgumentList to the

        #    Get-WmiObject commandlet

 

        # -ArgumentList pass in the server information.

 

        # | Wait-Job  -Timeout 2 - This tells the job to wait for 2 seconds.

 

        $job = start-job -scriptBlock {param ([string]$server) Get-WmiObject -Class Win32_OperatingSystem -ComputerName $server } -ArgumentList $server | Wait-Job  -Timeout 2

 

        # This dumps out the job information to a variable to use later

        $results = $job | Receive-Job | select Description

       

        # If the Job state is not completed then it was and error, so write out an error for the

        #   try/catch to catch.

        if ($job.state -ne "Completed") {Write-Error "Error"}

       

        # This is the where we put the variables into an array.

        $List += [pscustomobject]@{

        'ComputerName' = $server

        'Description'  = $results.Description

        }

    }

 

    catch {

        #catch if there is an error and if there is write that there was one in the lsit array..

       $List += [pscustomobject]@{

       'ComputerName'         = $server

       'Description' = "Error"

 

       }

    }

 

 

Here is the full script.

 

 

 

#get the list of servers to scan from serverlist.txt

$serverlist = "C:\scripts\computers.txt"

$List = @()

$i = 0

$fileCount = 0

 

#get the count of servers for updates to the user.

 

$fileCount = (Get-Content $serverlist | Measure-Object).Count

 

 

#Loop the server list.

foreach ($server in Get-Content $serverlist) {

 

#increment the number of records proccessed

$i++

 

#Write to the screen whats happening.

Write-Progress -activity "Connecting to server $server" -status "Scanning: $i of $($fileCount)" -percentComplete (($i / $fileCount)  * 100)

 

 

 

 

 

 

 

    # try this and catch it if it fails

    try {

        # Set the try to stop if it error out

        $ErrorActionPreference = 'Stop'

       

              

        # start the Job and assign it to a variable.. 

        # -scriptBlock -- assign the -scriptblock argument to show what to run.. this is where the get-wmiobject is run. This is encapsualted in {}

        # param  -- set the param to pass along the server name from the -ArgumentList to the Get-WmiObject commandlet

        # -ArgumentList pass in the server information.

        # | Wait-Job  -Timeout 2 - This tells the job to wait for 2 seconds.

 

        $job = start-job -scriptBlock {param ([string]$server) Get-WmiObject -Class Win32_OperatingSystem -ComputerName $server } -ArgumentList $server | Wait-Job  -Timeout 2

 

        # This dumps out the job information to a variable to use later

        $results = $job | Receive-Job | select Description

       

        # If the Job state is not completed then it was and error, so write out an error for the try/catch to catch.

        if ($job.state -ne "Completed") {Write-Error "Error"}

       

        # This is the where we put the variables into an array.

        $List += [pscustomobject]@{

        'ComputerName' = $server

        'Description'  = $results.Description

        }

    }

 

    catch {

        #catch if there is an error and if there is write that there was one in the lsit array..

       $List += [pscustomobject]@{

       'ComputerName'         = $server

       'Description' = "Error"

 

       }

    }

 

 

}

 

$List  | Out-GridView

 

 

 

Friday, December 13, 2013

Error 0x80070002 occurred while verifying known folder

ISSUE:

Server is getting a general Warning message speradically a lot. Its a Warning so not a big deal but it does raise concern.
Here is the error:

- System
  - Provider
   [ Name]  Microsoft-Windows-KnownFolders
   [ Guid]  {8939299F-2315-4C5C-9B91-ABB86AA0627D}
   EventID 1002
   Version 0
   Level 3
   Task 0
   Opcode 0
   Keywords 0x8000000000000000
  - TimeCreated
   [ SystemTime]  2013-12-13T05:30:22.504632600Z
   EventRecordID 6590
   Correlation
  - Execution
   [ ProcessID]  10080
   [ ThreadID]  5392
   Channel Microsoft-Windows-Known Folders API Service
   Computer **********************
  - Security
   [ UserID]  S-1-5-18
- EventData
  hrError 0x80070003
  FolderId {B97D20BB-F46A-4C97-BA10-5E3608430854}
  Path C:\Windows\system32\config\systemprofile\AppData\Roaming\Microsoft\Windows\Start Menu\Programs\Startup 

So yeah a lot of these are happening with various folders.

RESOLUTION:

I was doing some reading and found out that this specific error had to do with some folders missing in a default profile area. One of the readings fixes was, make the missing folders. What a great idea.

There are several easy ways to make the folders:

CMD
Use can use the MD, make directory option.

MD "C:\Windows\system32\config\systemprofile\AppData\Roaming\Microsoft\Windows\Start Menu\Programs"

Windows Explorer
Just go to the folder area and make the missing folder. Make sure you make the full path if more than one folder is missing.

Copy the Default profile on the system.
Every system has a profile that it uses as a template for making a new user profile. So I copied it and pasted it in the missing directory. Note: I did not verify that ALL the missing folders where there. I figured since its a warning, I will wait out the log file and see any othere missing folders show up.


Update: welp that didn't work.. Still getting Error

0x80070002 occurred while verifying known folder {b4bfcc3a-db2c-424c-b029-7fe99a87c641} with path 'C:\Windows\system32\config\systemprofile\Desktop'.

and it does exist.

So i did some more digging and ran across this post, http://tqaforum.net/discussion/37146/known-folders-errors-in-event-viewer/p4,  about how there are 2 system folders in windows 64 bit.. I totally forgot this. So I copied the folders to the SysWOW64. I am thinking this will fix the issue. We shall see.