1

I'm stumped. I'm running the below code on a 2012 R2 box while I develop it. All this piece does is get the hostname, grabs the number off the end, runs a function to see if it's an odd or even, then sets the storage location based on that.

For some reason after the If statement returns the value the script just stops running like the script has ended As you can see I've added write-debug "message 3" and it simply does not even register. Anyone know of any PS gotchas for a scenario like this? Or is it I've made some mistake somewhere. Server is running WMF 4.0.

function check-oddOrEven($number)
{
    If([bool]!($number%2))
    {
       $OddEvnResult = "Even"
       return $OddEvnResult
    }
    Else
    {
       $OddEvnResult = "Odd"
       return $OddEvnResult
    }
}

Write-Debug "message1" -debug

$oddStrgPath = "C:\ClusterStorage\Volume1"
$evnStrgPath = "C:\ClusterStorage\Volume2"

$hostname = $env:computername
#$hostname = "testN02"
$OddEvnSplit = $hostname.split('N')[1]

Write-Debug "message2" -debug

$OddEvnResult = check-oddOrEven $OddEvnSplit
if ($OddEvnResult -eq "Odd")
{
    write-host "Odd number in hostname detected (1,3,5..etc). Setting storage path to" $oddStrgPath
    #set-vmhost -VirtualHardDiskPath $oddStrgPath -VirtualMachinePath $oddStrgPath
    $OEresult= $oddStrgPath
    return $OEresult
}
else
{
    write-host "Even number in hostname detected (2,4,6..etc). Setting storage path to" $evnStrgPath
    #set-vmhost -VirtualHardDiskPath $evnStrgPath -VirtualMachinePath $oddStrgPath
    $OEresult= $evnStrgPath
    return $OEresult
}

Write-Debug "message3" -debug

I've tried write-host and write-output with no success either. Here is the output from the console:

DEBUG: message1
DEBUG: message2
Even number in hostname detected (1,3,5..etc). Setting storage path to C:\ClusterStorage\Volume2
C:\ClusterStorage\Volume2
  • Hello, just tried to reproduce your issue. Remove the 'return' statement at the end of your 'if' and 'else' blocks, and it will work. Now I leave it up to you to understand the behaviour ;) – Ob1lan Jan 31 '17 at 6:56
3

Please read this post from StackOverflow about the 'return' statement. Here is the summary :

Return: This will return to the previous call point. If you call this command from a script (outside any functions) it will return to the shell. If you call this command from the shell it will return to the shell (which is the previous call point for a single command ran from the shell). If you call this command from a function it will return to where ever the function was called from.

Execution of any commands after the call point that it is returned to will continue from that point. If a script is called from the shell and it contains the Return command outside any functions then when it returns to the shell there are no more commands to run thus making a Return used in this way essentially the same as Exit.

So, you have to remove the return statement from your 'if' and 'else', leaving only the variable to display its content.

Exemple :

    if ($OddEvnResult -eq "Odd")
{
    write-host "Odd number in hostname detected (1,3,5..etc). Setting storage path to" $oddStrgPath
    #set-vmhost -VirtualHardDiskPath $oddStrgPath -VirtualMachinePath $oddStrgPath
    $OEresult= $oddStrgPath
    $OEresult
}
else
{
    write-host "Even number in hostname detected (2,4,6..etc). Setting storage path to" $evnStrgPath
    #set-vmhost -VirtualHardDiskPath $evnStrgPath -VirtualMachinePath $oddStrgPath
    $OEresult= $evnStrgPath
    $OEresult
}

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.