Find duplicate entries in CSV file

Tagged: 

This topic contains 8 replies, has 4 voices, and was last updated by Profile photo of Venkata Kalyan Venkata Kalyan 1 month, 3 weeks ago.

  • Author
    Posts
  • #63402
    Profile photo of Venkata Kalyan
    Venkata Kalyan
    Participant

    Hi,
    I have a CSV file which has some duplicate entries.

    Get-VM | select Name | sort Name | Export-Csv -path "C:\Users\venkatak\Desktop\Test\Duplicate_Replication\Duplicates.csv" –NoTypeInformation
    This CSV file will have duplicate entries.

    Import-Csv 'ihsvc03_Duplicates.csv' | Group-Object -Property Name | Where-Object { $_.count -ge 2 } | Foreach-Object { $_.Group } | select Name | Export-csv -Path "C:\Users\venkatak\Desktop\Test\Duplicate_Replication\Duplicate_Found.csv" –NoTypeInformation
    This will get the information from above file & write only duplicate entries to separate file.

    Is there a way, I can send an e-mail, if I found only duplicate entries, if not found, no e-mail is necessary.

    -Kalyan

  • #63408
    Profile photo of Rob Simmers
    Rob Simmers
    Participant
    $duplicates = Import-Csv 'ihsvc03_Duplicates.csv' | 
                  Group-Object -Property Name | 
                  Where-Object { $_.Count -gt 1 }
    
    if ( $duplicates ) {
        #Send Email
    }
    
    • #63442
      Profile photo of Venkata Kalyan
      Venkata Kalyan
      Participant

      Hi Rob,
      I am looking for the duplicate names in the e-mail as-well.

      -Kalyan

  • #63409
    Profile photo of random commandline
    random commandline
    Participant
    $group = Get-VM | Select-Object Name | Group-Object -Property Name
    $result = foreach ($g in $group){If ($g.Count -gt 1) {$g.Name}}
    $send = @{
        To = 'test1.com'
        From = 'test2.com'
        Subject = 'Get-VM Duplicates'
        Body = $result
        SMTPServer = ''}
    
    Send-MailMessage @send
    
    • #63444
      Profile photo of Venkata Kalyan
      Venkata Kalyan
      Participant

      Hi Random,
      It's giving me error: cannot validate argument on parameter 'body'

      -Kalyan

  • #63450
    Profile photo of BlackBoxCoder
    BlackBoxCoder
    Participant

    Hey Venkata,

    I think the results in $Result are empty.

    You could have a look by inspecting the Variable.

    $group = Get-VM | Select-Object Name | Group-Object -Property Name
    $result = foreach ($g in $group){If ($g.Count -gt 1) {$g.Name}}
    $result
    

    Is there any data displayed?

    BR,
    BBC

    • #63457
      Profile photo of Venkata Kalyan
      Venkata Kalyan
      Participant

      Thanks. There are no data.
      One more thing, I need to add some text to body if data is there.

      body = $result ?

      -Kalyan

    • #63459
      Profile photo of Venkata Kalyan
      Venkata Kalyan
      Participant

      Hi,
      I got it.

      body – "$result – Test here"

      -Kalyan

    • #63462
      Profile photo of Venkata Kalyan
      Venkata Kalyan
      Participant

      Hi,
      But, if the data is present, I am receiving e-mail twice. I just want only one e-mail.

      $group = Get-VM | Select-Object Name | Group-Object -Property Name
      $result = foreach ($g in $group){If ($g.Count -gt 1) {$g.Name}}
      $send = @{
      To = 'test1.com'
      From = 'test2.com'
      Subject = 'Get-VM Duplicates'
      Body = $result
      SMTPServer = "}

      Send-MailMessage @send

      -Kalyan

You must be logged in to reply to this topic.