Search code examples
powershellwindows-8wmiwindows-scripting

WMI Scripting Windows 8 error


I have a problem with WMI in a machine with a Windows 8 Home Edition. I need to catch the CPU usage and the ProcessID by process. I've tried so many ways:

  • ShellExecute in Delphi
  • A *.bat with the code
  • A *.vbs
  • A *.vbs executed by a *.bat
  • A *.ps1
  • A *.ps1 executed by a *.bat

(Maybe some of these ways can be really stupid, but I've tried anyway)

In the User-Click it works perfectally, but by a standalone applicantion it doesn't work. I have opened the Security on WMIMGMT.msi of some folders and the execution policy (in PowerShell) now is UNRESTRICTED.

This is the code on the *.ps1 file:

$ErrorActionPreference = "SilentlyContinue"
Stop-Transcript | Out-Null
$ErrorActionPreference = "Continue"
$perflist = (get-wmiobject Win32_PerfFormattedData_PerfProc_Process)
foreach ($p in $peflist) {
  "" + $p.IDProcess + ";" + $p.PercentProcessTime
}

This is the code on *.bat

powershell -ExcetutionPolicy Unrestricted -File "C:\Somefolder\PP.ps1" > C:\SomeFolder\output.txt

All I got is this output:

get-wmiobject : Invalid query "select * from Win32_Win32_PerfFormattedData_PerfProc_Process"
In C:\Somefolder\PP.ps1:4 character: 14
+ $perflist = (get-wmiobject Win32_PerfFormattedData_PerfProc_Process)
+          ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
     + CategoryInfo         : InvalidArgument: (:) [Get-WmiObject], ManagementException
     + FullyQualifiedErroID : GetWMIManagementException,Microsoft.PowerShell.Commands.GetWmiObjectCommand

Edit1: The code copied as asked:

  • *.ps1:

    $ErrorActionPreference="SilentlyContinue" 
    Stop-Transcript | out-null 
    $ErrorActionPreference = "Continue"
    $perflist = (get-wmiobject Win32_PerfFormattedData_PerfProc_Process) 
    foreach ($p in $perflist) {
      "" + $p.IDProcess + ";" + $p.PercentProcessorTime
    } 
    
  • *.bat

    powershell -executionPolicy unrestricted -file "C:\MonitorPerformance\test.ps1" > C:\MonitorPerformance\output1.txt
    
  • Output: (Consulta inválida = Invalid Query; No = in; caractere = character)

    get-wmiobject : Consulta inv lida "select * from Win32_PerfFormattedData_PerfProc_Process"
    No C:\MonitorPerformance\test.ps1:4 caractere:14
    + $perflist = (get-wmiobject Win32_PerfFormattedData_PerfProc_Process)
    +              ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
        + CategoryInfo          : InvalidArgument: (:) [Get-WmiObject], ManagementException
        + FullyQualifiedErrorId : GetWMIManagementException,Microsoft.PowerShell.Commands.GetWmiObjectCommand
    

Solution

  • Windows 8 does not performs WMI actions perfectly when you use x32 applications, I made a dummy x64 application who executes the *.bat and it finally works. Thanks for all the help.