1

I'm trying to start my Powershell portfolio with and easy script that calculates Network ID or BroadcastID depending on input from the user. That being said, I can only get my if statement to run. I am unsure what I am missing here. I have tried searching this as I know I cannot be the only one to have this issue, but I am unable to find it. Any education on my error would be appreciated as it seems like a basic flaw.

Thanks!

#prompt for IP and subnet mask
$ip = Read-Host -Prompt "Enter your IP address"
$mask = Read-Host - Prompt "Enter your subnet mask"
[String]$UserDecision = Read-Host -Prompt "Enter N if you would like to calculate your Network ID or B if you would like to calculate your Broadcast address."
    
$splitmask=$mask.split(".")
    
$wildcard="$(255 - $splitmask[0]).$(255 - $splitmask[1]).$(255 - $splitmask[2]).$(255 - $splitmask[3])"
    
# ip and mask variable to ip addresses
    
$ip = [ipaddress] $ip
    
$mask = [ipaddress] $mask
    
#determine networkID
    
function CalculateNetID {

    $networkID = [ipaddress] ($ip.Address -band $mask.Address)
    
    #print NetworkID to console
    echo "The Network id is $($networkID.IPAddressToString)"
}  
    
function CalculateBroadcastID {
    
    $networkID = [ipaddress] ($ip.Address -band $mask.Address)

    #convert wildcard to IP addresses
    $wildcard= [ipaddress] $wildcard    
    
    $broadcast = [ipaddress] $($wildcard.Address -bor $NetworkID.Address)
    
    #print broadcast to console
    echo "The Broadcast id is $broadcast"
}
    
if ($UserDecision -eq "N" -or "n"){
    CalculateNetID
}    
elseif($UserDecision -eq "B" -or "b"){
    CalculateBroadcastID
}
else{
    echo "Please retry and enter the character associated with the ID you would like to calculate"
}
3
  • 2
    Honestly, I'd recommend using a switch statement in here for cleaner output. You also have to evaluate the $UserDecision variable again in your if condition. Probably best if you also use a -Like "b*" which is case insensitive or, you can just lowercase it using .tolower() method. Commented Mar 21, 2021 at 17:55
  • 2
    Thumbs up for @AbrahamZinala comment, I added the switch statement on edit. Commented Mar 21, 2021 at 18:06
  • 1
    @AbrahamZinala That is great tip. Thanks! Commented Mar 21, 2021 at 20:34

1 Answer 1

4

Here is your problem:

$false -or 'n' # => True

As you can see, this is what's happening in your first if statement, the correct syntax for your condition should be:

if($UserDecision -eq "N" -or $UserDecision -eq "n") {

Same thing applies for the elseif.

Note that, PowerShell comparison operators are not case-sensitive by default, so the -or conditions could be just removed:

if($UserDecision -eq "n") {

You might also want to consider using a switch statement instead of chained if, elseif, else:

switch($UserDecision) {
    N { CalculateNetID }
    B { CalculateBroadcastID }
    Default {
        "Please retry and enter the character associated with the ID you would like to calculate"
    }
}
Sign up to request clarification or add additional context in comments.

5 Comments

you may want to add a note that -eq is case insensitive ... so there is no need to test for both the upper and lower case letters. [grin]
@Lee_Dailey I think I already did, maybe I explained it wrong?
arg! [blush] i managed to [somehow] misread your post. "never mind ..."
Perfect. I really appreciate the help. I realize Powershell is not case sensitive but I did not realize I could do it this way. I appreciate it!
Yup, glad to help :)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.