syntax error

Glenn Maxwell 12,871 Reputation points
2022-08-11T05:37:29.787+00:00

Hi all

i am executing the below syntax and i am getting below error. experts guide me

$name = "chrome.exe"  
$processHandle = (Get-CimInstance Win32_Process -Filter "Name = '$name'").ProcessId  
$Threads = Get-CimInstance -Class Win32_Thread -Filter "ProcessHandle = $processHandle"  
$threads | Select-Object priority, thread*, User*Time, kernel*Time |  
Out-GridView -Title "The $name process has $($threads.count) threads"  

Get-CimInstance : Invalid query
At line:3 char:12

  • $Threads = Get-CimInstance -Class Win32_Thread -Filter "ProcessHandle ...
  • ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
  • CategoryInfo : InvalidArgument: (:) [Get-CimInstance], CimException
  • FullyQualifiedErrorId : HRESULT 0x80041017,Microsoft.Management.Infrastructure.CimCmdlets.GetCimInstanceCommand
Windows for business Windows Server User experience PowerShell
Windows for business Windows Server User experience Other
0 comments No comments
{count} votes

Accepted answer
  1. Limitless Technology 39,916 Reputation points
    2022-08-11T15:39:14.827+00:00

    Hello

    The columns returned by the WMI class are incomplete or unexpected. This error can indicate that the query returned a null value. Therefore the script can't continue / will throw the 0x80041017. It is possible that the Win32_Thread cannot be found or accessed on the server causing a null result.

    In most cases, could be also due to an output with multiple objects (various handles) so the system can't assign the variable to one specifically, then it complains about the Invalid Query.

    Please try to add a recursion on the result, such as:

    $Threads = $processHandle | Foreach-Object{
    Get-CimInstance -Class Win32_Thread -Filter "ProcessHandle = $_"
    }

    ---------------------------------------------------------------------------------------------------------------------------------------------------------

    --If the reply is helpful, please Upvote and Accept as answer--

    0 comments No comments

1 additional answer

Sort by: Most helpful
  1. Andreas Baumgarten 123.4K Reputation points MVP Volunteer Moderator
    2022-08-11T07:12:41.79+00:00

    Hi @Glenn Maxwell ,

    chrome.exe starts multiple processes. So $processHandle is an array with ProcessIDs .
    This is the reason why line 3 gives you an error.

    Please try this if it is helpful for you:

    $name = "chrome.exe"  
    $processHandles = (Get-CimInstance Win32_Process -Filter "Name = '$name'").ProcessId  
    foreach ($processHandle in $processHandles){  
    $threads = Get-CimInstance -Class Win32_Thread -Filter "ProcessHandle = $processHandle"  
    $threads | Select-Object priority, thread*, User*Time, kernel*Time |  
     Out-GridView -Title "The $name process has $($threads.count) threads"}  
    

    ----------

    (If the reply was helpful please don't forget to upvote and/or accept as answer, thank you)

    Regards
    Andreas Baumgarten

    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.