Error when running "get-wmiobject"

  • Marios Philippopoulos

    SSC Guru

    Points: 57030

    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]

  • Jeffrey Williams

    SSC Guru

    Points: 88603

    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
    Problems are opportunities brilliantly disguised as insurmountable obstacles.

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

  • Marios Philippopoulos

    SSC Guru

    Points: 57030

    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]

  • Jeffrey Williams

    SSC Guru

    Points: 88603

    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
    Problems are opportunities brilliantly disguised as insurmountable obstacles.

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

  • Marios Philippopoulos

    SSC Guru

    Points: 57030

    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]

  • Jeffrey Williams

    SSC Guru

    Points: 88603

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

    Jeffrey Williams
    Problems are opportunities brilliantly disguised as insurmountable obstacles.

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

  • Marios Philippopoulos

    SSC Guru

    Points: 57030

    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]

  • Jeffrey Williams

    SSC Guru

    Points: 88603

    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
    Problems are opportunities brilliantly disguised as insurmountable obstacles.

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

  • Marios Philippopoulos

    SSC Guru

    Points: 57030

    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]

  • Jeffrey Williams

    SSC Guru

    Points: 88603

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

    Jeffrey Williams
    Problems are opportunities brilliantly disguised as insurmountable obstacles.

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

  • Marios Philippopoulos

    SSC Guru

    Points: 57030

    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 11 (of 11 total)

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