PowerShell - 每个 - 重复

时间:2015-08-10 14:43:32

标签: powershell for-loop

我有一个带有foreach循环的PowerShell脚本。其中的Write-Host总是在屏幕上写入副本。我添加了一封发送邮件,每条记录都会发送重复的电子邮件。我在这里做错了什么?

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 = "intranet@NOV.com"
$To = "Shawna_Richardson@NOV.com"
$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()

1 个答案:

答案 0 :(得分:3)

Get-Data内的所有内容都会发生两次 - ,因为您拨打Get-Data两次

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

调用一次并将输出存储在变量中,然后重复使用:

$Data = Get-Data 
$Data | Export-Csv -NoTypeInformation -Path "G:\\Result\\export.csv"
$Data | Out-File $csvFileForFacilities