WMI connection check and confirmation

Hi everyone , hoping someone could offer some advice on this one.

I have a vast list of SQL servers in a .txt file.

My windows colleagues have granted WMI access for me to a number of machines , but it seems not the entire list.

Can anyone offer any advice \ scripts that I could run against the list that would output into excel that would show successful connections , and those that fail ?

Any advice or help appreciated. Any comments in the scripts would be excellent as they help me to learn what is happening in the script.

thanks all

  • Hello,

    you can simply issue any WMI query to the server and see if it fails.

    $listOfServers = 'Server01', 'Server02'

    foreach ($server in $listOfServers)
    {
    $status = ''
    try
    {
    Get-WmiObject -ComputerName $server -Class Win32_ComputerSystem -ErrorAction Stop > $null
    $status = 'Ok'
    }
    catch [Runtime.InteropServices.COMException]
    {
    $errorCode = $_.Exception.ErrorCode
    $accessDeniedErrorCode = 0x80070005
    $serverUnavailableErrorCode = 0x800706BA
    if (($errorCode -eq $serverUnavailableErrorCode) -or ($errorCode -eq $accessDeniedErrorCode))
    {
    $status = $_.Exception.Message
    }
    else
    {
    #re-throw the exception
    throw
    }
    }

    New-Object -TypeName PsObject -Property @{
    ComputerName = $server
    Status = $status
    }
    }

     

  • Thanks very much , perfect I will try to use it and understand what it is doing.