1

我有一个使用 kpscript 从 keepass 数据库中检索信息的命令。此命令可能会检索 1 行,多行没有,但最后,它总是返回以下输出:

OK: Operation completed successfully.

如果 StartsWith("OK:"): 示例:使用 PowerShell 单独处理每一行并退出循环的最佳方法是什么:

KPScript -c:GetEntryString $PASSHOME\$PASSFILE -pw:$PASS -Field:UserName $SEARCH

Administrator
OK: Operation completed successfully.

按照我尝试过的方法没有成功

$UNAME = KPScript -c:GetEntryString $PASSHOME\$PASSFILE -pw:$PASS -Field:UserName $SEARCH 
IF(! $UNAME){ 
    write-host "UNAME1=$UNAME" 
    write-host "ERROR UNAME" -foreground "red" 
    exit 
}

while (! $UNAME) { KPScript -c:GetEntryString $PASSHOME\$PASSFILE -pw:$PASS -Field:UserName $SEARCH }

也失败了

    $UNAME=KPScript -c:GetEntryString $PASSHOME\$PASSFILE -pw:$PASS -Field:UserName $SEARCH
    write-host "UNAME=$UNAME"

结果:

    UNAME=Administrator OK: Operation completed successfully.

关键是要有: UNAME=administrator UNAME=OK:操作成功完成。所以每一行都可以单独处理

也许这可以通过数组来完成?

也失败了

    $UNAME=@(KPScript -c:GetEntryString $PASSHOME\$PASSFILE -pw:$PASS -Field:UserName $SEARCH)
    write-host "UNAME=$UNAME"

输出是一样的:

    UNAME=Administrator OK: Operation completed successfully.
4

2 回答 2

2

这是你想要做的吗?

#Sample of kpscript output
#$output = "Administrator","OK: Operation completed successfully.","Should have stopped already"

$output = KPScript -c:GetEntryString $PASSHOME\$PASSFILE -pw:$PASS -Field:UserName $SEARCH

foreach ($line in $output) {
    $UNAME = $line
    Write-Host "UNAME=$UNAME"
    if($line.StartsWith("OK:")) { break }
}

输出(使用样本输出):

UNAME=Administrator
UNAME=OK: Operation completed successfully.
于 2016-03-04T20:36:22.957 回答
0

老话题,但也许它对某人有帮助,这就是我解决它的方法

$information 将包含一个包含有用信息的数组

$RawEntiries = & $kpscript -c:GetEntryString $PathKeepass -pw:$pscred.GetNetworkCredential().password -Field:$Field -ref-Title:"$title" -FailIfNoEntry -FailIfNotExists

If ($RawEntiries -notlike "E:*")
{
    $information = $RawEntiries[0..($RawEntiries.IndexOf("OK: Operation completed successfully.")-1)]
    $Information
} 
于 2020-10-14T12:11:18.440 回答