powershell - PowerShell-每个-重复

标签 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()

最佳答案

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 

关于powershell - PowerShell-每个-重复,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31922555/

相关文章:

powershell - 如何在 PowerShell 中获取以 "ABC_"开头的 Active Directory 组名称列表?

javascript - 将 JavaScript 用于数组和循环

java - 使用 for 循环从任意大小的列表中获取多个 WebElement

php - 循环使用 php 在 mysql 数据库中插入行

powershell - TFS Build和Powershell:如何访问预定义的构建变量

在文本框中输入时的 PowerShell 调用函数

c# - 更新标签时,程序在For循环上挂起

javascript - 我对 For 循环的理解正确吗?我缺少什么? - JavaScript

excel - PowerShell - 将数据粘贴到 Excel 中

powershell - 使用Powershell脚本发送SNMP陷阱