开发者

How to search with multiple keys in a hash table in powershell

I'm writing a Powershell script and in it I'm using a hash table to store information about database checks. The table has 5 keys (host, check, last execution time, last rep, status) and I want to search in my table for values where:

$s = $table where $host -eq $hostname -and check -eq $check

Does anyone have any idea how this is done? And if it makes any difference, the script cannot rely on .NET framework higher than 2.0

I´m new to Powershell and scripting in general so this might be very obvious but I still can't seem to find an answer on Google. Also if someone knows a good reference page for Powershell scripting I would really appreciate a link.

Gísli

EDIT: Don't see how it matters but here is a function I use to create a hash table:

function read_saved_state{
    $state = @{}
    $logpos = @{}
    $last_log_rotate =开发者_开发技巧 0
    foreach($s in Get-Content $saved_state_file){
        $x = $s.split('|')
        if($x[0] -eq 'check'){
            $state.host = $x[1]
            $state.check = $x[2]
            $state.lastexec = $x[3]
            $state.lastrep = $x[4]
            $state.status = $x[5]
        }
        elseif($x[0] -eq 'lastrotate'){
            $last_log_rotate = $x[1]
        }
        elseif($x[0] -eq 'log'){
            $logpos.lastpos = $x[3]
        }
    }

$saved_state_file has one line for each check run and can also have a line for last log rotate and last log position. There can be as many as 12 checks for one host.

I'm trying to extract a particular check, run at a particular host, and changing the lastexec_time, last_rep and status. return $state,$logpos,$last_log_rotate }


Assuming you have an array or list of hashtables (not entirely clear from the question), your syntax is pretty close:

$s = $tables | where {($_.host -eq $hostname) -and ($_.check -eq $check)}
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜