1

I have a command to retrieve info from a keepass database using the kpscript. This command may retrieve 1 line, multiple line on none, but, in the end, it always returns this output:

OK: Operation completed successfully.

What is the best way to handle each line individually with PowerShell and exit the loop if StartsWith("OK:"): Example:

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

Administrator
OK: Operation completed successfully.

Following approaches I have tried unsuccessfully

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

and

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

Also unsuccessful

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

The result:

    UNAME=Administrator OK: Operation completed successfully.

The point is to have: UNAME=administrator UNAME=OK: Operation completed successfully. So every line can be treated individualy

Maybe this be accomplished with an array?

Also unsuccessful

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

Output is the same:

    UNAME=Administrator OK: Operation completed successfully.
Pedro Sales
  • 513
  • 2
  • 8
  • 24
  • Have you tried anything? You already have the jist of an idea. Are you familiar with loops on PowerShell and the pipeline? – Matt Mar 04 '16 at 15:25
  • I have in fact tried many things: while, for, foreach, but I can't seen to get the result I want, that's why I'm asking for help. What I need in the end is to treat each line individually depending on the data – Pedro Sales Mar 04 '16 at 15:28
  • That is awesome. I appreciate your effort. Can you show it here? – Matt Mar 04 '16 at 15:30
  • since I made so many test and none of them were working, i don't have the tests anymore, sorry ... – Pedro Sales Mar 04 '16 at 15:32
  • Of all the test you cant recreate just one of them? This reads like a code writing request which is not what SO is for. If you show a smiddgen of effort it can go a long way here. – Matt Mar 04 '16 at 15:35
  • 1
    I'll recreate them again, no problem :) – Pedro Sales Mar 04 '16 at 15:37
  • First I tried with no loop like this $UNAME=KPScript -c:GetEntryString $PASSHOME\$PASSFILE -pw:$PASS -Field:UserName $SEARCH IF (! $UNAME) { write-host "UNAME1=$UNAME" write-host "ERROR UNAME" -foreground "red" exit } but the variable got both lines in it – Pedro Sales Mar 04 '16 at 15:40
  • Then I tryed with while like this while (! $UNAME) { KPScript -c:GetEntryString $PASSHOME\$PASSFILE -pw:$PASS -Field:UserName $SEARCH } but I got an infinite loop – Pedro Sales Mar 04 '16 at 15:42
  • I also tried the foreach, but I don't think that's the right way – Pedro Sales Mar 04 '16 at 15:45
  • Do you want to display the lines until ok is reached? – Matt Mar 04 '16 at 15:47
  • My first post shows exactly the output for a single command. Would you like other kind of example? – Pedro Sales Mar 04 '16 at 15:48
  • `if($UNAME -match "OK: Operation completed successfully"){"found"}`? – Matt Mar 04 '16 at 15:56
  • I tried if($UNAME.StartsWith("OK:")) {"found"} but $UNAME gets the value and the ok like, all together. I need to separate the lines – Pedro Sales Mar 04 '16 at 16:00

2 Answers2

2

Is this what you're trying to do?

#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 }
}

Ouptut (using the sample-output):

UNAME=Administrator
UNAME=OK: Operation completed successfully.
Frode F.
  • 52,376
  • 9
  • 98
  • 114
  • I used a foreach-loop here because I seem to remember that `break` didn't stop the pipeline in `Foreach-Object` (you needed to throw a `PipelineStoppedException`), but in Win10 using both PS5.0 and PS2.0 it works like it should with `1..10 | Foreach-object { if ($_ -eq 5) { break }; $_ }`? Anyone know why? – Frode F. Mar 04 '16 at 20:50
  • Thank you Frode, that is what I want in fact, but I'm getting an error: You cannot call a method on a null-valued expression. At Z:\CSP\deploys\aplicacional\teste.ps1:26 char:24 + if($line.StartsWith <<<< ("OK:")) { break } + CategoryInfo : InvalidOperation: (Start + FullyQualifiedErrorId : InvokeMethodOnNull. Seams like nothing is getting to the $line variable – Pedro Sales Mar 07 '16 at 09:26
  • check `$output` does it contain anything? – Frode F. Mar 07 '16 at 10:15
  • My bad, it works, thank you. If you can help me with another question, in case UNAME can have several values, I wanted to put them all in an array. How can I do this? – Pedro Sales Mar 07 '16 at 10:39
  • `$uname = @()` outside the loop and `$uname += $line` inside – Frode F. Mar 07 '16 at 10:44
  • 1
    I'm opening a new question for this because it's different from the original one – Pedro Sales Mar 07 '16 at 11:46
0

old topic but maybe it helps someone, this is how i solved it Use the paramters -FailIfNoEntry -FailIfNotExists this will give you a line starting with E: *** message when the field is empty or the entry is not found

$information will contain an array with the usefull 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
}