1

I have a PowerShell script with a foreach loop. The Write-Host within this always writes duplicate on the screen. I added a send-mail and it sends duplicate email per record. What am I doing wrong here?

Import-module ActiveDirectory

$web = get-SPWeb "http://inside.nov.com/sales"
$list = $web.Lists["Shared Documents"]
$now = get-date;
$SearchBase = "OU=Sales,DC=NOV,DC=com"
$LdapServer = "AD748.NOV.COM"
$From = "[email protected]"
$To = "[email protected]"
$Cc = "TBD"
$Subject = "Testing Powershell email"
$Body = "Active Directory updates"
$SMTPServer = "smtp74.NOV.com"
$CAML = '
   <Where>
     <Eq>
        <FieldRef Name="Region" />
        <Value Type="Choice">Northwest</Value>
     </Eq>
   </Where>
'
$query = new-object Microsoft.SharePoint.SPQuery
$query.RowLimit = 10
$query.Query = $CAML
$listitems = $list.GetItems($query)
$count = $listitems.count
Write-Host $count

function Get-Data()
{
    if ($count -ge 1)
    {
        foreach($item in $listitems)
        {
            $NOVEmpID = $item["NOVEmpID"]

            if($item["NOVEmpID"] -ne $null)
            {
                $LDAPResult = Get-ADUser -SearchBase $searchbase -SearchScope 'subtree' -Server $ldapserver -filter "NOVEmpID -like '*$NOVEmpID*'" -Properties * | Select -Property AccountExpirationDate, userAccountControl
                $pair1, $pair2 = $LDAPResult -replace '^@{|}$' -split '; '
                $null, $ADExpDate = $pair1 -split '=', 2
                $null, $userAccountControl = $pair2 -split '=', 2
                if($ADExpDate){
                    $ADExpDate = [DateTime]::Parse($ADExpDate).ToShortDateString()
                }
                $SPEndDate = [DateTime]::Parse($item["SPExpDate"]).ToShortDateString()

                $data = @{
                            "Emp ID" = $item["NOVEmpID"]
                            "Sales Name" = $item["EmployeeName"]
                            "SP End Date" = $item["SPExpDate"]
                            "AD End Date" = $ADExpDate
                            "AD Account Status" = $userAccountControl
                         }
                New-Object PSObject -Property $data
                if($ADExpDate)
                {
                    if($ADExpDate -gt $SPEndDate)
                    {
                        write-host $item["NOVEmpID"] $item["EmployeeName"]    $item["SPExpDate"] $ADExpDate
                        $item["NewDate"] = $ADExpDate
                        $item.SystemUpdate()
                        $list.update()
                    }
                    if($ADExpDate -lt $SPEndDate)
                    {
                        $subject = $item["EmployeeName"] + " - " + $NOVEmpID + " date is greater than AD Date"
                        $Body = $item["EmployeeName"] + " - " + $NOVEmpID + " - "  + $item["SPExpDate"] + " - " + $ADExpDate
                        Send-MailMessage -From $From -to $To -Subject $subject -Body $Body -SmtpServer $SMTPServer
                    }
                }
                else
                {
                    $subject = $item["EmployeeName"] + " - " + $NOVEmpID + " AD Date is empty"
                    $Body = $item["EmployeeName"] + " - " + $NOVEmpID + " - "  + $item["SPExpDate"] + " - " + $ADExpDate
                    Send-MailMessage -From $From -to $To -Subject $subject -Body $Body -SmtpServer $SMTPServer
                }
            }
        }
    }
}
$csvFileForFacilities = "$((Get-Date).ToString('MM-dd-yyyy')).csv"
Write-Host $csvFileForFacilities
Get-Data | Export-Csv -NoTypeInformation -Path "G:\\Result\\export.csv"
Get-Data | Out-File $csvFileForFacilities
$web.dispose()
4
  • Please update your example with what you're seeing in the e-mail, and what you're looking to see instead. As is, this is somewhat complex code, and I'm not sure which line is writing the duplicates. Commented Aug 10, 2015 at 14:53
  • it's sending email twice and writing to screen twice from write-host code. Commented Aug 10, 2015 at 14:57
  • 2
    because you are calling get-ddate twice. Commented Aug 10, 2015 at 15:00
  • @jisaak assume that should read get-data - but well spotted. Commented Aug 10, 2015 at 15:05

1 Answer 1

3

Everything inside Get-Data happens twice - because you call Get-Data twice:

get-data | Export-Csv -NoTypeInformation -Path "G:\\Result\\export.csv"
get-data | Out-File $csvFileForFacilities 

Call it once and store the output in a variable, and then re-use that:

$Data = Get-Data 
$Data | Export-Csv -NoTypeInformation -Path "G:\\Result\\export.csv"
$Data | Out-File $csvFileForFacilities 
Sign up to request clarification or add additional context in comments.

1 Comment

Thanks. this worked. I totally forgot that I was calling the function twice.

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.