none
Powershell if/else statement

    Question

  • Hi,

    I hope this is the correct forum for my question. I am trying to write a script that tells me what the status of a hyper-v integration service is on a VM and then disable the service if it is true. Here is my script so far. I get as far as the if statement but it doesn't go to the else statement.

    Currently timesync is set to true.

    $timesync = get-vmintegrationservice -vmname <VNname> -name "Time Synchronization"
    $status = $timesync.enabled
    if ($status -eq "False") {write-host "time service disabled"}
    Else {write-host "time is enabled"}

    My end script I want it to actually do something like this:

    $timesync = get-vmintegrationservice -vmname <VMname> -name "Time Synchronization"
    $status = $timesync.enabled
    if ($status -eq "False") {write-host "time service disabled"}
    ELSE {disable-vmintegrationservice -vmname <VMName>"Time Synchronization"}

    Any help with where I am going wrong is greatly appreciated.

    Thanks,
    Georgi


    Georgi

    Tuesday, January 29, 2013 8:31 PM

Answers

  • Try using If ($status -eq $false)

    If you find that my post has answered your question, please mark it as the answer. If you find my post to be helpful in anyway, please click vote as helpful.

    • Marked as answer by Gmeyer Tuesday, January 29, 2013 8:43 PM
    Tuesday, January 29, 2013 8:33 PM
  • testing a boolean variable or expression explicity for $false is postentially confusing, and as redundant as testing for $true. Instead of this:

    if ($status -eq $False) {write-host "time service disabled"}
    Else {write-host "time is enabled"}

    I would recommend this:

    if ($status) {
       write-host "time is enabled"
    } Else {
       write-host "time service disabled"
    }

    If the flow of control is such that it would be more logical for the "false" code block to appear first, I'd change it to this:

    if (-not $status) {
       write-host "time service disabled"
    } Else {
       write-host "time is enabled"
    }

    Al Dunbar -- remember to 'mark or propose as answer' or 'vote as helpful' as appropriate.

    Wednesday, January 30, 2013 4:46 AM

All replies

  • Try using If ($status -eq $false)

    If you find that my post has answered your question, please mark it as the answer. If you find my post to be helpful in anyway, please click vote as helpful.

    • Marked as answer by Gmeyer Tuesday, January 29, 2013 8:43 PM
    Tuesday, January 29, 2013 8:33 PM
  • Since $status appears to be a boolean, have you tried testing for $false instead of "False"?

    [string](0..33|%{[char][int](46+("686552495351636652556262185355647068516270555358646562655775 0645570").substring(($_*2),2))})-replace " "

    Tuesday, January 29, 2013 8:34 PM
  • Thank you! That worked. I did not think of using $false instead of "False". I am still in the learning stages of powershell. And now running my second script with the correct statement works.

    Thanks!

    Georgi


    Georgi

    Tuesday, January 29, 2013 8:43 PM
  • testing a boolean variable or expression explicity for $false is postentially confusing, and as redundant as testing for $true. Instead of this:

    if ($status -eq $False) {write-host "time service disabled"}
    Else {write-host "time is enabled"}

    I would recommend this:

    if ($status) {
       write-host "time is enabled"
    } Else {
       write-host "time service disabled"
    }

    If the flow of control is such that it would be more logical for the "false" code block to appear first, I'd change it to this:

    if (-not $status) {
       write-host "time service disabled"
    } Else {
       write-host "time is enabled"
    }

    Al Dunbar -- remember to 'mark or propose as answer' or 'vote as helpful' as appropriate.

    Wednesday, January 30, 2013 4:46 AM
  • in an IF statement, the condition that appears within the parentheses is an expression having the value $true or the value $false. If a variable already has a boolean value, it is redundant to compare it to one of the two boolean constants. it also prevents the casting of non-boolean non-zero values to true:

    PS C:\Users\Al>
    PS C:\Users\Al> $A = 333
    PS C:\Users\Al> if ($A) {'YAY'}
    YAY
    PS C:\Users\Al> if (-not $A) {'YAY'}
    PS C:\Users\Al> if ($A -eq $true) {'YAY'}
    PS C:\Users\Al> if ($A -eq $false) {'YAY'}
    PS C:\Users\Al>

    This redundancy causes confusion, especially in the negative, as "IF ($A -EQ $FALSE)" could be read as: if it is true that $A is false.


    Al Dunbar -- remember to 'mark or propose as answer' or 'vote as helpful' as appropriate.

    Wednesday, January 30, 2013 5:01 AM
  • So if let's say in my case if I asked for that status of a virtual machine based on "enabled" and that gives me a status of True or False, I should not use $true or $false but -not $status or just $status. So $status would be true and -not $status would be false.


    Georgi

    Thursday, January 31, 2013 10:00 PM
  • I was OK with your code as it was written to check for the explicit value of $false.
     
    That way it asserts to me that we are using Boolean values.
     
    Get the code working first, then decide whether you want to refactor for style reasons.
     
    $x -eq $false  -->  -not $x  or  !$x
    $x -eq $true   -->  $x
     
    I've even seen code written as !!$x to convert a value to a Boolean value.
     
     
    Thursday, January 31, 2013 10:31 PM
  • that depends on who you are asking for the status and how they are returning it.

    If the documentation for the cmdlet or whatever states that it returns a boolean true or false value, then it would be appropriate to use one of these two forms:

        IF ($result) ...
        IF (-not $result) ...

    If it returns a string that is either set to "true" or "false", you might be tempted to think you'd need a string comparison, however Powershell does a fair bit of automatic casting. Consider this example of the process:

    PS C:\>
    PS C:\> $A = $true
    PS C:\> $B = -1
    PS C:\> $C = 'True'
    PS C:\> $A
    True
    PS C:\> $B
    -1
    PS C:\> $C
    True
    PS C:\> if ($A) {"A IS TRUE"}
    A IS TRUE
    PS C:\> if ($B) {"B IS TRUE"}
    B IS TRUE
    PS C:\> if ($C) {"C IS TRUE"}
    C IS TRUE
    PS C:\>
    PS C:\> $A|gm
       TypeName: System.Boolean
    Name        MemberType Definition
    ----        ---------- ----------
    CompareTo   Method     int CompareTo(System.Object obj), int CompareTo(bool value)
    Equals      Method     bool Equals(System.Object obj), bool Equals(bool obj)
    GetHashCode Method     int GetHashCode()
    GetType     Method     type GetType()
    GetTypeCode Method     System.TypeCode GetTypeCode()
    ToString    Method     string ToString(), string ToString(System.IFormatProvider provider)
    PS C:\> $B|gm
       TypeName: System.Int32
    Name        MemberType Definition
    ----        ---------- ----------
    CompareTo   Method     int CompareTo(System.Object value), int CompareTo(int value)
    Equals      Method     bool Equals(System.Object obj), bool Equals(int obj)
    GetHashCode Method     int GetHashCode()
    GetType     Method     type GetType()
    GetTypeCode Method     System.TypeCode GetTypeCode()
    ToString    Method     string ToString(), string ToString(string format), string ToString(System.IFormatProvider pro..
    PS C:\> $C|gm
       TypeName: System.String
    Name             MemberType            Definition
    ----             ----------            ----------
    Clone            Method                System.Object Clone()
    CompareTo        Method                int CompareTo(System.Object value), int CompareTo(string strB)
    Contains         Method                bool Contains(string value)
    CopyTo           Method                System.Void CopyTo(int sourceIndex, char[] destination, int destinationIndex,..
    EndsWith         Method                bool EndsWith(string value), bool EndsWith(string value, System.StringCompari..


    Al Dunbar -- remember to 'mark or propose as answer' or 'vote as helpful' as appropriate.

    Thursday, January 31, 2013 11:08 PM
  • I've been wrapping my head around this for a while the better part of 2 days now as my If/Else statements were always going to the first option regardless of the return of a function.

    Thank you, sincerely.

    Zach

    Sunday, December 15, 2013 3:17 PM