1

所以我写了一些代码来获取文本文件的输入,并对我填充的另一个数据库运行一些 sql 检查:

$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

我得到的返回值是:

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

这是正确的,但它也缺少更多。例如,如果我从 SQL 管理工作室中获取单个样本并运行查询,我会得到:

我将每个列表中的“测试”一词填充为....测试。

Select Names From Objects WHERE Names='test'

Names

test

但我没有看到对上述代码输出的测试。我通过查询带有 SQL 管理工作室的数据库手动验证了大约 5 或 6 个。

任何帮助深表感谢。

4

1 回答 1

1

将文件内容与数据库中的完整名称列表进行比较:

$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

是否Compare-Object显示差异?

于 2013-06-03T08:58:42.957 回答