Error when running "get-wmiobject"

  • When running the following I get the error below:

    get-wmiobject -ComputerName XXXXX -query "select * from Win32_Service where Name='serviceName1'"

    Get-WmiObject : Critical error

    At line:1 char:14

    + get-wmiobject <<<< -ComputerName XXXXX -query "select * from Win32_Service where Name='serviceName1'"

    I can ping the computer in question, so I don't know what is wrong. Is there a way to find out? The message is very vague.

    __________________________________________________________________________________
    SQL Server 2016 Columnstore Index Enhancements - System Views for Disk-Based Tables[/url]
    Persisting SQL Server Index-Usage Statistics with MERGE[/url]
    Turbocharge Your Database Maintenance With Service Broker: Part 2[/url]

  • The only thing I can suggest is remote into that system and verify that you can access WMI. I have seen things like this when WMI was not working on the server.

    Jeffrey Williams
    “We are all faced with a series of great opportunities brilliantly disguised as impossible situations.”

    ― Charles R. Swindoll

    How to post questions to get better answers faster
    Managing Transaction Logs

  • Jeffrey Williams (1/7/2009)


    The only thing I can suggest is remote into that system and verify that you can access WMI. I have seen things like this when WMI was not working on the server.

    I connected locally on that server and ran the following vb script. It executed fine:

    msgbox("before")

    sComputerName = "XXXXXX"

    Set objWMIService = GetObject ("winmgmts:" _

    & "{impersonationLevel=impersonate}!\\" _

    & sComputerName & "\root\cimv2")

    msgbox("after")

    __________________________________________________________________________________
    SQL Server 2016 Columnstore Index Enhancements - System Views for Disk-Based Tables[/url]
    Persisting SQL Server Index-Usage Statistics with MERGE[/url]
    Turbocharge Your Database Maintenance With Service Broker: Part 2[/url]

  • Can you try the following and see what happens?

    $computer = 'name of computer';

    Get-WmiObject -ComputerName $computer Win32_Service | Where-Object {$_.Name -like 'MSSQL$*'} | Select Name, StartMode, State, Status;

    Jeffrey Williams
    “We are all faced with a series of great opportunities brilliantly disguised as impossible situations.”

    ― Charles R. Swindoll

    How to post questions to get better answers faster
    Managing Transaction Logs

  • Jeffrey Williams (1/8/2009)


    Can you try the following and see what happens?

    $computer = 'name of computer';

    Get-WmiObject -ComputerName $computer Win32_Service | Where-Object {$_.Name -like 'MSSQL$*'} | Select Name, StartMode, State, Status;

    That ran fine.

    I think the reason for the error is that I was missing the single quotes around the computer name.

    This one runs fine:

    get-wmiobject -ComputerName 'myComputerName' -query "select * from Win32_Service where Name='myServiceName'"

    __________________________________________________________________________________
    SQL Server 2016 Columnstore Index Enhancements - System Views for Disk-Based Tables[/url]
    Persisting SQL Server Index-Usage Statistics with MERGE[/url]
    Turbocharge Your Database Maintenance With Service Broker: Part 2[/url]

  • Interesting - you probably have a character in the computer name that is causing a problem.

    Jeffrey Williams
    “We are all faced with a series of great opportunities brilliantly disguised as impossible situations.”

    ― Charles R. Swindoll

    How to post questions to get better answers faster
    Managing Transaction Logs

  • Jeffrey Williams (1/8/2009)


    Interesting - you probably have a character in the computer name that is causing a problem.

    Actually, there was nothing wrong with the original code. Even taking the quotes out, it now works. I just ran the exact same line of code as yesterday, and I get no error. There are only alphanumeric characters in the name.

    It must be something flaky with our network....

    I wish there was a way to trap the specific error powershell/WMI is throwing...

    __________________________________________________________________________________
    SQL Server 2016 Columnstore Index Enhancements - System Views for Disk-Based Tables[/url]
    Persisting SQL Server Index-Usage Statistics with MERGE[/url]
    Turbocharge Your Database Maintenance With Service Broker: Part 2[/url]

  • You can trap for the errors and display the error messages. Example:

    Trap [Microsoft.SqlServer.Management.Common.ExecutionFailureException] {

    Write-Output $("Exception: $($_.Exception.Message)");

    Write-Output $("Base Exception: $($_.Exception.GetBaseException().Message)");

    return;

    }

    In the above, I am trapping for a specific error - but, you can trap at a more generic level as needed.

    Jeffrey Williams
    “We are all faced with a series of great opportunities brilliantly disguised as impossible situations.”

    ― Charles R. Swindoll

    How to post questions to get better answers faster
    Managing Transaction Logs

  • Jeffrey Williams (1/8/2009)


    You can trap for the errors and display the error messages. Example:

    Trap [Microsoft.SqlServer.Management.Common.ExecutionFailureException] {

    Write-Output $("Exception: $($_.Exception.Message)");

    Write-Output $("Base Exception: $($_.Exception.GetBaseException().Message)");

    return;

    }

    In the above, I am trapping for a specific error - but, you can trap at a more generic level as needed.

    Great info, thank you!

    I've been meaning to beef up my scripts with more error handling capabilities.

    __________________________________________________________________________________
    SQL Server 2016 Columnstore Index Enhancements - System Views for Disk-Based Tables[/url]
    Persisting SQL Server Index-Usage Statistics with MERGE[/url]
    Turbocharge Your Database Maintenance With Service Broker: Part 2[/url]

  • Just wait till v2 - which includes full Try/Catch functionality.

    Jeffrey Williams
    “We are all faced with a series of great opportunities brilliantly disguised as impossible situations.”

    ― Charles R. Swindoll

    How to post questions to get better answers faster
    Managing Transaction Logs

  • Jeffrey Williams (1/8/2009)


    Just wait till v2 - which includes full Try/Catch functionality.

    That's cool.

    What tool do you use to work with powershell scripts?

    __________________________________________________________________________________
    SQL Server 2016 Columnstore Index Enhancements - System Views for Disk-Based Tables[/url]
    Persisting SQL Server Index-Usage Statistics with MERGE[/url]
    Turbocharge Your Database Maintenance With Service Broker: Part 2[/url]

Viewing 11 posts - 1 through 10 (of 10 total)

You must be logged in to reply to this topic. Login to reply