user3709009
user3709009

Reputation: 1

Powershell IF statement trying to compare two ip adresses.

Hi I am trying to compare two IP addresses. I want the script to evaluate the new ip address to the old one that is stored in a .txt file. However when I try to run this, the statement tells me it is false when actually the two ip addresses are true. Here is my code:

$a=(Invoke-WebRequest ifconfig.me/ip).Content
$b= Get-Content C:\ipadress.txt


if ($a -eq $b) 
{
Write-Host "The ip address has not changed"
}

else 
{
Write Host "The ip address has changed"
}

Any suggestions?

Upvotes: 0

Views: 4980

Answers (1)

Tim Ferrill
Tim Ferrill

Reputation: 1674

Might be worth casting them both to the [IPAddress] type. Also, since you're getting extra spaces I'd use .Trim() to clean that up.

if ([IPAddress]$a.Trim() -eq [IPAddress]$b.Trim())

Worst case this gives you an error that will help in troubleshooting.

Upvotes: 1

Related Questions