search
Categories
Sponsors
VirtualMetric Hyper-V Monitoring, Hyper-V Reporting
Archive
Blogroll

Badges
MCSE
Community

Cozumpark Bilisim Portali
How to avoid WMI query hangs in PowerShell
Posted in Windows Powershell | 2 Comments | 7,624 views | 15/06/2015 12:28

Some WMI queries may be effected from system performance, so queries may run very long times. In some cases, I see that wmi query hangs, so PowerShell script is not able to continue. In that cases, we should use timeout parameter.

Since CIM is the new method for us, we should go with CIM. For example this is our WMI query:

Get-WmiObject -Class "Win32_PerfFormattedData_PerfProc_Process"

If you run this on PowerShell, you will notice that it will takes for a while. So lets run same query with CIM:

Get-CimInstance -Class "Win32_PerfFormattedData_PerfProc_Process" -OperationTimeoutSec 15

As you see, CIM and WMI query is almost same. But additionally I’ve added OperationTimeoutSec parameters to avoid from hangs. That gives us ability to cancel query if it takes more than 15 seconds. You can change it anytime you want.


Comments (2)

scott

January 2nd, 2020
10:28:12

YOU ROCK!


Nadim J

January 20th, 2021
15:16:40

Unfortunately CIM relies on WinRM. So if that’s not enabled in large environments, you are still stuck



Leave a Reply