Author Posts

February 2, 2017 at 6:08 pm

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

February 2, 2017 at 6:25 pm

$duplicates = Import-Csv 'ihsvc03_Duplicates.csv' | 
              Group-Object -Property Name | 
              Where-Object { $_.Count -gt 1 }

if ( $duplicates ) {
    #Send Email
}

February 2, 2017 at 6:58 pm

$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

February 3, 2017 at 5:43 am

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

-Kalyan

February 3, 2017 at 5:46 am

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

-Kalyan

February 3, 2017 at 8:32 am

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

February 3, 2017 at 10:38 am

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

body = $result ?

-Kalyan

February 3, 2017 at 11:23 am

Hi,
I got it.

body – "$result – Test here"

-Kalyan

February 3, 2017 at 11:25 am

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