0

I am retrieving the hosts file on a server with 5 DNS entries:

C:\Windows\System32\drivers\etc\hosts

Mine looks like this after the comments:

127.0.0.1 infspcpd8tx8e.rtmphost.com

127.0.0.1 infspkbpef39p.rtmphost.com

127.0.0.1 infspo99vn3ti.rtmphost.com

127.0.0.1 infspqx6l10wu.rtmphost.com

127.0.0.1 infspvdkqjhkj.rtmphost.com

In my hosts file I see them as 5 lines on top of eachother, but when I paste it here it has a space inbetween. This is the same when I use get-content on that file, but I wouldn't expect that to stop me.

So I have an array that is gotten like so: $ACCOUNTS = Get-ChildItem "D:\cyst\accounts\" | select name

I then try to see if there are duplicate entries in the hosts file by checking the $accounts variable against the array I got containing the hosts file.

    foreach ($rtmp in $ACCOUNTS) { 
        $HostsFile = Get-Content C:\Windows\System32\drivers\etc\hosts | ForEach-Object {[System.Convert]::ToString($_)}
        #$rt[string]$data = $HostsFile
        [string]$rtmpfull = $rtmp.name + ".rtmphost.com"

            if ($HostsFile -contains $rtmpfull) { Write-Host "Host found in hosts file moving on..." } 

                else { echo "wrong"

                }
        }

It never matches and always returns false, I can't match anything.. please help - is it a type issue? I've googled this for DAYS but now i'm desperate and posting here.

zdan
  • 28,667
  • 7
  • 60
  • 71
Matt Wall
  • 475
  • 2
  • 7
  • 15

3 Answers3

2

I think you can probably speed that up by just dispensing with the foreach.

(Get-Content C:\Windows\System32\drivers\etc\hosts) -match [regex]::escape($rtmpfull) 

Should match the entire hosts file at once.

mjolinor
  • 66,130
  • 7
  • 114
  • 135
0

This test:

if ($HostsFile -contains $rtmpfull)

is looking for $rtmpfull to match an entire line stored in $HostsFile. You want to check for a partial match like so;

if ($HostsFile | Foreach {$_ -match $rtmpfull})

BTW you can simplify this:

$HostsFile = Get-Content C:\Windows\System32\drivers\etc\hosts | ForEach-Object {[System.Convert]::ToString($_)}

to:

$HostsFile = Get-Content C:\Windows\System32\drivers\etc\hosts

By default, Get-Content will give you an array of strings where each element of the array corresponds to a line in the file.

Keith Hill
  • 194,368
  • 42
  • 353
  • 369
  • Thank you, but I am still having some problems here.. so.. I get the hosts file like so: $HostsFile = Get-Content C:\Windows\System32\drivers\etc\hosts It contains the same list as above. I then do $hostsfile -contains $rtmpfull $rtmpfull is "127.0.0.1`tinfspvdkqjhkj" I even try to match a tab or the word or a few words and nothing, even the whole line. If I do $hostsfile -contains '127.0.0.1' and it gets TRUE I do that but other words found in the line and I get FALSE Oy vey hehe – Matt Wall Jan 26 '12 at 00:26
  • if ($HostsFile | Foreach {$_ -match $rtmpfull}) That saved my ass. I wish I could make it less performance hungry but it WORKS THANK YOU! – Matt Wall Jan 26 '12 at 00:32
  • What's the value of `$rtmpfull` on the false positives? Since this is now doing a regex match, you have to watch out for metacharacters. – Keith Hill Jan 26 '12 at 01:21
0
$ACCOUNTS = Get-ChildItem "D:\cyst\accounts\" 

foreach ($rtmp in $ACCOUNTS){ 
    $found=$FALSE
    foreach ($line in (gc C:\Windows\System32\drivers\etc\hosts)){
        if(($line -match $rtmp) -and ($found -eq $TRUE)){
            echo "$($matches[0]) is a duplicate"
        }
        if (($line -match $rtmp) -and ($found -eq $FALSE)){
            echo "Found $($matches[0]) in host file..."
            $found=$TRUE
        }
    }
}

Not elegant, but it will do the job.

SpellingD
  • 2,533
  • 1
  • 17
  • 13