syntax error

Glenn Maxwell 10,146 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 Server 2019
Windows Server 2019
A Microsoft server operating system that supports enterprise-level management updated to data storage.
3,451 questions
Windows Server 2016
Windows Server 2016
A Microsoft server operating system that supports enterprise-level management updated to data storage.
2,369 questions
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,361 questions
0 comments No comments
{count} votes

Accepted answer
  1. Limitless Technology 39,351 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 96,266 Reputation points MVP
    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