0

I getting the following error:

gwmi : Invalid class "WmiMonitorID"
At line:1 char:1
+ gwmi WmiMonitorID -Namespace root\wmi | ForEach-Object {($_.UserFrie ...
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
     + CategoryInfo          : InvalidType: (:) [Get-WmiObject], ManagementException
     + FullyQualifiedErrorId : GetWMIManagementException,Microsoft.PowerShell.Commands.GetWmiObjectCommand

while trying to grab my monitor's Serial ID using:

gwmi WmiMonitorID -Namespace root\wmi | ForEach-Object {
    ($_.UserFriendlyName -notmatch 0 | foreach {[char]$_}) -join "";
    ($_.SerialNumberID -notmatch 0 | foreach {[char]$_}) -join ""
}

I used this command about 2 months back and it worked flawlessly. (Was not even running PowerShell in admin)

code screenshot

Now with or without admin I get the error mentioned above. (also tried x64 and x86 both with/without admin)

error screenshot

Am I overlooking something?

Ansgar Wiechers
  • 193,178
  • 25
  • 254
  • 328
newcool
  • 319
  • 2
  • 20
  • May be your WMI is corrupt .Try these steps https://www.virtuallyboring.com/microsoft-wmi-invalid-class-error-0x80041010/ – TheGameiswar Jul 17 '19 at 02:57
  • @TheGameiswar My `WMI Control (Local)` looks like the good one in the screenshot – newcool Jul 17 '19 at 03:01
  • 1
    The second screenshot looks as if there is an additional space in the class. You can see it in `Invalid Class " WmiMonitorID"` so it might be reading the whitespace. – Drew Jul 17 '19 at 03:26
  • @Drew Looks like you are right, but now there is a new error. https://i.imgur.com/jsRYonB.png – newcool Jul 17 '19 at 03:44
  • 1
    Now it looks like it is running each line independently instead of it being one larger expression. – Drew Jul 17 '19 at 03:53
  • @Drew Looks like you're right again. It was adding random spaces from wherever I c&p from. I guess this is solved. – newcool Jul 17 '19 at 05:54

1 Answers1

0

Thank you very much @Drew for pointing out the whitespace.

So it seems like whatever I was c&p the code from was adding extra spaces to the code which would make it end up looking like

gwmi(Space)(Space)WmiMonitorID(Space)(Space)-Namespace

or add extra spaces at the end

... -join ""}(Space)(Space)

The solution would be to copy it in a .txt or paste bin and make sure the extra spaces are not there before copying back to PowerShell.

newcool
  • 319
  • 2
  • 20