PowerShell Script Error

  • Hello I have the following powershell script which checks is the MS SQL Server Service is running:

    $servers=get-content "c:\servers.txt"

    foreach($server in $servers)

    {

    $body=get-wmiobject win32_service -computername $server |

    select name,state |

    where {($_.name -like "MSSQLSERVER" ) `

    -and $_.state -match "Stopped"} |

    Out-String

    if ($body.Length -gt 0)

    {

    $smtp = new-object Net.Mail.SmtpClient("notincluded")

    $subject="****HIGH IMPORTANCE SQL SERVER IS DOWN ON" + $server + "****"

    $smtp.Send("notincluded@notincluded.co.uk", "notincluded@notincluded.co.uk", $subject, $body)

    "message sent"

    }

    }

    When running through the powershell interface I get the following error.

    + $body=get-wmiobject <<<< win32_service -computername $server |
    Get-WmiObject : Invalid namespace

    Can anyone help.

  • anyone able to help ?

  • It's probably failing on named instances. I've run into that when trying to do checks across all instances. The OS level checks choke on that. I'm sure there's probably a way to check for a "\" or something and lop off the end but I haven't gotten that far with mine. You could change it to try to connect to the instance and if you can't alert such as...

    $con.Open()

    if($con.state -eq “Open”)

    {do something}

    else

    {alert that instance is down}

  • See if this helps.

    http://blogs.msdn.com/powershell/archive/2007/09/24/get-wmihelp-search-wmihelp.aspx

    I haven't started to use powershell yet, but this looks like it might be useful to get some help.

    Greg E

  • Powershell Get-WmiObject : Invalid namespace

    Try this:

    $namespace = "root\CIMV2"

    Get-WmiObject -class Win32_Service -computername $computer -namespace $namespace

    Also; $servers=get-content "c:\servers.txt" <---- Make sure there are no leading or trailing spaces or tabs in your server names. Those cause all kinds of errors.

    And get your hands on this:

    ScriptOMatic Powershell Version 1.0

    Ed Wilson, MrEd Software, 6 March 2008

  • I had exactly this issue with a similar script. In my case there was a space after some server names in the input file; they are not ignored when wmi tries to connect.

  • To get rid of trailing or leading spaces try this:

    $a = $a.Trim()

    Or in this case:

    $server = $server.Trim()

  • I use this to check services and start, i dont have speech marks in get-content path

    # Setup trap to catch exceptions

    trap [Exception]

    {

    write-error $("TRAPPED: " + $_.Exception.Message);

    }

    # read computers from text file

    $computers = Get-Content C:\scripts\sqlcheck\Computers.txt;

    $start = $true;

    # Setup the Service array with the service names we want to check are running

    $serviceArray = 'morello msstaging updatelinks','morello msstaging signoff','morello msstaging lockmanager','morello msstaging mae','morello msstaging syncd','SQLSERVERAgent', 'MSSQLSERVER';

    # Powershell knows it's an array so working with it is simple

    foreach($computer in $computers)

    {

    Write-Host "Checking $computer";

    $objWMIService = Get-WmiObject -Class win32_service -computer $computer

    foreach($service in $objWMIService)

    {

    # Check each service specicfied in the $serviceArray

    foreach($srv in $serviceArray)

    {

    if($service.name -eq $srv)

    {

    Write-Host "$srv is present on $computer.";

    if($service.state -eq "running")

    {

    Write-Host "$srv is running on $computer";

    }

    else

    {

    Write-Host "$srv is not running on $computer";

    # If $start is true the script will attempt to start the service if it is stopped

    if($start -eq $true)

    {

    # Attempt to start the current service on the current computer

    $serviceInstance = (Get-WmiObject -computer $computer Win32_Service -Filter "Name='$srv'");

    $name = $serviceInstance.Name;

    Write-Host "Attempting to start $name on $computer."

    $serviceInstance.StartService() | Out-Null;

    # Refresh the object instance so we get new data

    $serviceInstance = (Get-WmiObject -computer $computer Win32_Service -Filter "Name='$srv'");

    $state = $serviceInstance.State;

    Write-Host "$name is ""$state"" on $computer.";

    }

    }

    }

    }

    }

    }

Viewing 8 posts - 1 through 7 (of 7 total)

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