0

I need some help to eliminate duplicate entries in a hashtable.

Code:

$username=Get-Content ".\u.txt"
#$username
$fileread=Get-Content ".\lastlogon.txt"
$lastinfo=$fileread|select-string -pattern "logged off" -encoding ASCII
foreach($i in $lastinfo){
    $splitinfo=$i -split("Login ID: ")
    $dateinfo=$splitinfo[0] -split("       ")
    $finaldateinfo=$dateinfo[2] -split(" ")
    #$finaldateinfo[0]
    $userinfo=$splitinfo[1] -split(" ")
    #$userinfo[0]
    $hashinfo= @{$userinfo[0]=$finaldateinfo[0]}
    #$hashinfo
    foreach($h in $hashinfo.GetEnumerator()){
        foreach($a in $username){
            if($hashinfo.ContainsKey($a)){
                "$($hashinfo.keys):$($hashinfo.Values)"
            }
        }
    }
}

Result:

Name      Value
----     -----
USERID 08/03/2018
USERID 09/03/2018
USERID 10/03/2018
USERID 13/03/2018
ADM 23/03/2018

The hashtable is like this.

I need only the last entry of USERID to be kept and eliminate all the other values of USERID.

4
  • 1
    To what do you refer with last entry, the latest date? With several same Names it can't be a hash table key. Commented Nov 17, 2018 at 9:46
  • yes latest date Commented Nov 17, 2018 at 13:58
  • 1
    You are getting array of small hashtables. You'd need to define a hashtable at the beginning and then feel it out. You probably can make it a bit more efficient - triple foreach seemed to be excessive. Maybe post a sample of your input files Commented Nov 17, 2018 at 17:49
  • Please find the input file : u.txt USERID ADM ADM_TM Lastlogon.txt USERID loggedoff 08/03/2018 USERID loggedoff 13/03/2018 Commented Nov 18, 2018 at 10:44

2 Answers 2

1

I'm a little rusty in Powershell, but I believe you can create a dictionary (hash table) like this:

$LastAccessByUser= @{}

Assuming your source data is already ordered, simple iterate over it. I don't know your variable names, so I'll just provide an example assuming you have a list by the name $list containing Items.

foreach ($item in $list.Items)
{
    $LastAccessByUser[$item.Name] = $item.Value
}

Apologies if it doesn't quite match your scenario, but you should be able to adjust based on what you have.

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

Comments

1

This can't be a hashtable. As far as I know there can't be any duplicate keys (Name) in a hashtable as that's the purpose of the hashtable - to return values based on unique keys. Please recheck.

If this is an array then,

$lastmatch = $arr | where { $_.Name -match "USERID" } | select -Last 1

Add the above result to a hashtable like,

$ht.Add($lastmatch.Name,$lastmatch.Value)

1 Comment

The post isn't asking for multiple keys, but to store the last value encountered for each key.

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.