none
PowerShell hashtable duplicate entries RRS feed

  • Question

  • Hi ,

    Need some help to eliminate duplicate entries In a hashtable .

     Name                           Value                                                                                                                        
    ----                           -----                                                                                                                        
    USERID                         08/03/2018                                                                                                                   
    USERID                         09/03/2018                                                                                                                   
    USERID                         10/03/2018                                                                                                                   
    USERID                         13/03/2018                                                                                                                   
    ADM_IBM                        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 .



    • Edited by Subatra Mahendran Saturday, November 17, 2018 5:03 AM
    • Moved by Bill_Stewart Monday, January 7, 2019 8:07 PM Unanswerable drive-by question
    Saturday, November 17, 2018 5:02 AM

All replies

  • It is impossible to have duplicate keys in a hash table.  What you have posted is not a hash table. 

    Without the code there is not really any way to understand what you are trying to do.


    \_(ツ)_/


    • Edited by jrv Saturday, November 17, 2018 8:28 AM
    Saturday, November 17, 2018 8:26 AM
  • $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)" } } } }



    Output : 

    Name      Value

    USERID   08/03/2018
    USERID   09/03/2018
    USERID   10/03/2018
    USERID   13/03/2018
    ADM        23/03/2018



    Saturday, November 17, 2018 9:10 AM
  • Please format and post code correctly using the code posting tool provided.


    \_(ツ)_/

    Saturday, November 17, 2018 9:35 AM
  • $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)"
    }
    }
    }
    }
    
    
    


    Saturday, November 17, 2018 9:49 AM
  • Please format your code with correct indenting so it is readable.  Edit the original post and post  the correct code with proper formatting.

    The second code post is no where near the same as the first.

    Here is a good place to learn how to post in a technical forum:  How to ask questions in a technical forum

    You can learn how to format and write code in this article: PowerShell Style Guidelines

    You can learn how to use PowerShell here: Microsoft Virtual Academy - Getting Started with Microsoft PowerShell


    \_(ツ)_/


    • Edited by jrv Saturday, November 17, 2018 9:56 AM
    Saturday, November 17, 2018 9:56 AM
  • Hi  

    Sorry for the format  I posted . The second posted code is the right now .

    Please provide your suggestions .

    Just started coding in powershell and posting in technical forums . Will improve :)

    Thanks for the help .

    Saturday, November 17, 2018 10:00 AM
  • You still have not formatted your code so that it is readable and makes sense.  Read the article on style to learn how to format code.

    From what little I am willing to try to decode it appears that you have no understanding or experience with scripting and have just pasted a lot of things together blindly. 

    If you fix the format you will likely see some of your mistakes.  We cannot redesign you code because we don't so custom coding and we do not have access to your files.

    Please read the links I posted  to understand how to post and how to write code.


    \_(ツ)_/

    Saturday, November 17, 2018 10:04 AM
  • $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)"
    }
    }
    }
    }
    Output : 

    Name      Value

    USERID   08/03/2018
    USERID   09/03/2018
    USERID   10/03/2018
    USERID   13/03/2018
    ADM        23/03/2018



    Saturday, November 17, 2018 10:28 AM
  • Please take some time to read the links I posted and also take some time to learn the forum. 

    Here is how to indent code correctly so it is readable.

    $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)"
                }
            }
        }
    }

    Also you are outputting many things.  You need to learn enough about PowerShell to know how to get the output you want.  We cannot give you personal lessons in a forum.

    Looking at your code it is impossible to understand what you are trying to do.


    \_(ツ)_/


    • Edited by jrv Saturday, November 17, 2018 1:49 PM
    Saturday, November 17, 2018 1:49 PM
  • Start with this and look at the output.  The success of this is if your file actually works like your code thinks it does.

    $username=Get-Content .\u.txt
    Get-Content .\lastlogon.txt |
        select-string -pattern 'logged off' |
        ForEach-Object{
            $splitinfo = $_ -split 'Login ID: '
            $dateinfo = $splitinfo[0] -split '       '
            $finaldateinfo = $dateinfo[2] -split ' '
            $userinfo = $splitinfo[1] -split ' '
            [pscustomobject]@{
                User = $userinfo
                FinalDate = $finaldateinfo
            }
        }
    


    \_(ツ)_/

    Saturday, November 17, 2018 2:08 PM
  • I can't make much sense of the code, but your hash table does not have duplicates. Instead I think you keep recreating the hash table for each entry in lastLogon.txt. The logic of the code is wrong.

    Richard Mueller - MVP Enterprise Mobility (Identity and Access)

    Saturday, November 17, 2018 2:13 PM
  • I think it is important to format the code so it is clear where loops begin and end. the last code posted by the OP should be formatted similar to:

    $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)"
                }
            }
        }
    }

    This makes it clear that the hash table is recreated for each line in lastLogon.txt. And as noted, several things are output besides the has table.


    Richard Mueller - MVP Enterprise Mobility (Identity and Access)


    Saturday, November 17, 2018 2:19 PM
  • Here is how to parse a text file into objects and how to then manipulate those objects in PowerShell.

    This gets the text output from a CMD call to the old "dir" command which produces text output.

    cmd /c dir | 
        Select-String '<DIR>' |
        ForEach-Object{
            $parts = $_ -split '\s+'
            [pscustomobject]@{
                Filename = $parts[4]
                Date = $parts[0]
            }
        } |
        

    Now we can add code to filter the data or alter it in any way we want.

    cmd /c dir | 
        Select-String '<DIR>' |
        ForEach-Object{
            $parts = $_ -split '\s+'
            [pscustomobject]@{
                Filename = $parts[4]
                Date = $parts[0]
            }
        } |
        Where-Object{ $_.Date -lt [datetime]::Today.AddMonths(-2)}


    \_(ツ)_/

    Saturday, November 17, 2018 2:23 PM
  • Hello!

    If you get the data into a PSObject, you can do like this:

    $CSV = @(
        'Name;Value'
        'USERID;08/03/2018'
        'USERID;09/03/2018'
        'USERID;10/03/2018'
        'USERID;13/03/2018'
        'ADM;23/03/2018'
    )
    
    $ObjectGroupedByName = ConvertFrom-Csv $CSV -Delimiter ';' | select Name, @{Expression={(Get-Date $_.Value)};Label='Date'} | Group-Object -Property Name
    
    $ToKeep = @()
    $ObjectGroupedByName | % {
        $ToKeep += $_.Group | sort Date | select -Last 1
    }
    
    $ToKeep | ft *


    Regards,
    Erlend


    Saturday, November 17, 2018 7:18 PM