Issue with the -and operator

This topic contains 0 replies, has 1 voice, and was last updated by Profile photo of Forums Archives Forums Archives 5 years, 5 months ago.

  • Author
    Posts
  • #5572

    by segwar at 2013-02-20 02:16:08

    Hi All,

    I am trying to write a monitor script using powershell . The script will run in two servers which are a part of an MS cluster. The requirement is that the script whould patrol both the servers (it will be scheduled on both) and if the F: drive (cluster drive) is mapped and a specific service is stopped on one of the servers it should start the service. The code that I have written is as follows. This is a dignostic version of the script and I plan to change the command section of the loop. I wanted to check with the forum members if I was missing something.


    if (get-psdrive | where-object {$_.root -eq "F:\"}) -and (get-service "WINDOWS Service NAME" | where-object {$_.status -eq "stopped"}) {write-host "F mapped and service down"} else {"f not mapped and service down"}

    The error I am getting looks to be a syntax error.

    Thanks
    Segwar

    by AlexBrassington at 2013-02-20 04:06:14

    Yup. Syntax indeed.
    #You were missing some surrounding brackets
    #The IF operator requires ()
    if ((get-psdrive | where-object {$_.root -eq "F:\"}) -and (get-service "WINDOWS Service NAME" | where-object {$_.status -eq "stopped"}))
    {
    write-host "F mapped and service down"
    }
    else
    {
    "f not mapped and service down"
    }

    #Nicer version
    $fDriveExists = get-psdrive | where-object {$_.root -eq "F:\"}
    $serviceStopped = get-service "WINDOWS Service NAME" | where-object {$_.status -eq "stopped"}

    if ($fDriveExists -and $serviceStopped)
    {
    write-host "F mapped and service down"
    }
    else
    {
    "f not mapped and service down"
    }

    You were missing some standard braces for the if statement.

    Edit: The logic is also a bit wrong. The Else statement means that either F is not mapped or the service is still running.

    by segwar at 2013-02-20 19:40:43

    Thanks a Ton Alex!! God Bless

    by segwar at 2013-02-20 19:50:14

    About the logic, The commands in the if and else segments are debugging codes. Actually the command in the if segment will be to start the service and the command in the else would be to exit. Hope that explains.

    Thanks again

You must be logged in to reply to this topic.