VMware Cloud Community
hum_mike
Contributor
Contributor
Jump to solution

Comparing a parameter in a function for output

Here is my issue:

I have a function that I pass a parameter too which is actually an IP address. I need to inside the function, compare that parameter to output the portgroup. Here is my function: No matter what parameter I pass, I keep getting this as the output "No portgroup exist"

Function Portgroup{

param($Prod_IP)

If ($Prod_IP -like "10.10.")

{

$Portgroup = "portgroup 10.10.0.0"

}

Elseif ($Prod_IP -like "10.11.")

{

$Portgroup = "portgroup 10.11.0.0"

}

Elseif ($Prod_IP -clike "10.12.")

{

$Portgroup = "portgroup 10.12.0.0"

}

Else

{

$Portgroup = "No portgroup exist"

}

$Portgroup

}

0 Kudos
1 Solution

Accepted Solutions
AntonVZhbankov
Immortal
Immortal
Jump to solution

If ($Prod_IP -like "10.10.*")

Or

If ($Prod_IP.StartsWith("10.10."))


---

MCITP: SA+VA, VCP 3/4, VMware vExpert

http://blog.vadmin.ru

EMCCAe, HPE ASE, MCITP: SA+VA, VCP 3/4/5, VMware vExpert XO (14 stars)
VMUG Russia Leader
http://t.me/beerpanda

View solution in original post

0 Kudos
2 Replies
AntonVZhbankov
Immortal
Immortal
Jump to solution

If ($Prod_IP -like "10.10.*")

Or

If ($Prod_IP.StartsWith("10.10."))


---

MCITP: SA+VA, VCP 3/4, VMware vExpert

http://blog.vadmin.ru

EMCCAe, HPE ASE, MCITP: SA+VA, VCP 3/4/5, VMware vExpert XO (14 stars)
VMUG Russia Leader
http://t.me/beerpanda
0 Kudos
LucD
Leadership
Leadership
Jump to solution

With the -like operator you have to add an asterisk as the wildcard.

In the position of the asterisks can be any character any number of times.

Something like this

function Portgroup{
param($Prod_IP)

if ($Prod_IP -like "10.10.*")
{
$Portgroup = "portgroup 10.10.0.0"
}
elseif ($Prod_IP -like "10.11.*")
{
$Portgroup = "portgroup 10.11.0.0"
}
elseif ($Prod_IP -clike "10.12.*")
{
$Portgroup = "portgroup 10.12.0.0"
}
else
{
$Portgroup = "No portgroup exist"
}
$Portgroup
} 

PS: why do you use the -clike operator instead of -like ?

____________

Blog: LucD notes

Twitter: lucd22


Blog: lucd.info  Twitter: @LucD22  Co-author PowerCLI Reference