pedaleo
pedaleo

Reputation: 411

[System.Net.Dns]::GetHostbyAddress COMPARE strings

I have a CSV file with hostnames and their IP addresses and I've to deploy something on them but, I can only use their IP addresses.

So I need to confirm if the IP address stills matching the hostname before start the deployment.

I wrote this script but is not doing what I expected... Anyone can see where is the problem?

Thanks

$computerName = 'testName'
$computerIP = '192.168.32.148'

$var1 = [System.Net.Dns]::GetHostbyAddress("$computerIP").hostname

if ($var1 -like $computerName) {

    "$computerName IS LIKE $var1"
}else{
    "$computerName NOT LIKE $var1"
}

OUTPUT

testName NOT LIKE testName.mycompany.net

DESIRED OUTPUT

testName IS LIKE testName.mycompany.net

Upvotes: 0

Views: 777

Answers (1)

Mathias R. Jessen
Mathias R. Jessen

Reputation: 174485

-like uses exact wildcard matching, and you're not using any wildcards in your -like operation!

Try this:

if ($var1 -like "$computerName*") {
    "$computerName IS LIKE $var1"
}else{
    "$computerName NOT LIKE $var1"
}

(Notice the * after the $computerName value)

For more information about wildcard matching, check out Get-Help about_Wildcards

Upvotes: 2

Related Questions