1

So I have some code I wrote to take the input form a text file and run some sql checks against another database I populated:

$volOutput = gc C:\Users\<user>\Desktop\mutant.txt 

foreach ($m in $volOutput) { 

  $check = $m.split()[-1] | select -Unique

  foreach ($c in $check) {
    #$c - this lists all of them so the foreach is working...

    # Build the connection and search the db for $c names.
    $conn = New-Object System.Data.SqlClient.SqlConnection
    $conn.ConnectionString = "Server=(localdb)\mutex; Database=MutexObjects"
    $conn.Open()
    $db = $conn.CreateCommand()
    $db.CommandText = "select Names from Objects WHERE Names='$c'"
    $db.ExecuteScalar()
    $conn.Close()    

  } # Foreach Check

} # First foreach

The return values I get are:

PS C:\> B:\Programming\powershell\parse_vol.ps1
ZonesCounterMutex
ZoneAttributeCacheCounterMutex
ZonesCacheCounterMutex
ZoneAttributeCacheCounterMutex
ZonesLockedCacheCounterMutex
ZonesCounterMutex
ZoneAttributeCacheCounterMutex
ZonesCacheCounterMutex
ZoneAttributeCacheCounterMutex
ZonesLockedCacheCounterMutex

Which is correct, but it's also missing a lot more. If I take individual samples and run queries from within SQL management studio for example, I get:

I populated the word "test" in each list as a....test.

Select Names From Objects WHERE Names='test'

Names

test

But I don't see test on the output from the above code. There are about 5 or 6 more than it's missing that I have validated manually by querying the db w/in SQL management studio.

Any help is much appreciated.

1 Answer 1

1

Compare the file content against the full list of Names from the database:

$filecontent = Get-Content "C:\Users\<user>\Desktop\mutant.txt" `
          | % { $_.split()[-1] } `
          | select -Unique

$conn = New-Object System.Data.SqlClient.SqlConnection
$conn.ConnectionString = "Server=(localdb)\mutex; Database=MutexObjects"
$conn.Open()
$dbwrite = $conn.CreateCommand()
$dbwrite.CommandText = "SELECT Names FROM Objects"

$reader = $dbwrite.ExecuteReader([System.Data.CommandBehavior]::CloseConnection)
$dbcontent = while ( $reader.Read() ) { $reader[0] }

$conn.Close()    

Compare-Object $filecontent $dbcontent

Does Compare-Object show differences?

Sign up to request clarification or add additional context in comments.

3 Comments

Yes, a lot for both <= and = (more to the $filecontent <=)
The names with a <= side indicator are not present in the database, so it's unsurprising that they're not listed by your script.
So I actually went back and looked. I was missing some, and I think this was working all along... In either case, I like your response and gave you credit.

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.