PowerShell-适用于每个-重复

PowerShell-适用于每个-重复,powershell,for-loop,Powershell,For Loop,我有一个带有foreach循环的PowerShell脚本。其中的写入主机总是在屏幕上写入副本。我添加了一个“发送邮件”,它会根据每个记录发送重复的电子邮件。我做错了什么 Import-module ActiveDirectory $web = get-SPWeb "http://inside.nov.com/sales" $list = $web.Lists["Shared Documents"] $now = get-date; $SearchBase = "OU=Sales,DC=NOV,

我有一个带有
foreach
循环的PowerShell脚本。其中的写入主机总是在屏幕上写入副本。我添加了一个“发送邮件”,它会根据每个记录发送重复的电子邮件。我做错了什么

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()
导入模块ActiveDirectory
$web=获取SPWeb“http://inside.nov.com/sales"
$list=$web.Lists[“共享文档”]
$now=获取日期;
$SearchBase=“OU=Sales,DC=NOV,DC=com”
$LdapServer=“AD748.NOV.COM”
$From=”intranet@NOV.com"
$To=“Shawna_Richardson@NOV.com"
$Cc=“待定”
$Subject=“测试Powershell电子邮件”
$Body=“Active Directory更新”
$SMTPServer=“smtp74.NOV.com”
$CAML=
西北
'
$query=新对象Microsoft.SharePoint.SPQuery
$query.RowLimit=10
$query.query=$CAML
$listitems=$list.GetItems($query)
$count=$listitems.count
写入主机$count
函数Get-Data()
{
如果($count-通用电气1)
{
foreach($listitems中的项目)
{
$NOVEmpID=$item[“NOVEmpID”]
如果($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
如果($ADExpDate){
$ADExpDate=[DateTime]::Parse($ADExpDate).toSortDateString()
}
$SPEndDate=[DateTime]::解析($item[“SPExpDate”])。ToSortDateString()
$data=@{
“Emp ID”=$item[“NOVEmpID”]
“销售名称”=$item[“员工名称”]
“SP结束日期”=$item[“SPExpDate”]
“广告结束日期”=$ADExpDate
“AD帐户状态”=$userAccountControl
}
新对象PSObject-属性$data
如果($ADExpDate)
{
如果($ADExpDate-gt$SPEndDate)
{
写入主机$item[“NOVEmpID”]$item[“EmployeeName”]$item[“SPExpDate”]$ADExpDate
$item[“NewDate”]=$ADExpDate
$item.SystemUpdate()
$list.update()
}
如果($ADExpDate-lt$SPEndDate)
{
$subject=$item[“EmployeeName”]+“-”+$NOVEmpID+“日期大于广告日期”
$Body=$item[“EmployeeName”]+“-”+$NOVEmpID+“-”+$item[“SPExpDate”]+“-”+$ADExpDate
发送邮件-从$From-到$to-Subject$Subject-Body$Body-SmtpServer$SmtpServer
}
}
其他的
{
$subject=$item[“EmployeeName”]+“-”+$NOVEmpID+“广告日期为空”
$Body=$item[“EmployeeName”]+“-”+$NOVEmpID+“-”+$item[“SPExpDate”]+“-”+$ADExpDate
发送邮件-从$From-到$to-Subject$Subject-Body$Body-SmtpServer$SmtpServer
}
}
}
}
}
$csvFileForFacilities=“$(获取日期).ToString('MM-dd-yyyy')).csv”
写入主机$csvFileForFacilities
获取数据|导出Csv-NoTypeInformation-路径“G:\\Result\\Export.Csv”
从文件$csvFileForFacilities中获取数据
$web.dispose()

内部的所有
获取数据
都会发生两次-,因为您调用
获取数据
两次

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 

请用您在电子邮件中看到的内容以及您希望看到的内容更新您的示例。实际上,这是一段比较复杂的代码,我不确定哪一行写的是重复的。它发送了两次电子邮件,并从写入主机代码中写入了两次到屏幕。因为您调用了两次get-ddate。@jisaak假设应该读取
get-data
,但发现得很好。谢谢。这起作用了。我完全忘了我调用函数两次。