2016-06-08 60 views
-1

我有2個csv,1個來自交換機,1個來自AD。我正在查詢在AD中啓用的samaccountname,emailaddress。我正在查詢samaccountname,primarysmtpaddress其中recipienttype是'usermailbox'。根據Samaccountmatch將CSV數據解析爲另一個CSV

將這些進行比較後,根據找到的不匹配情況,吐出第3個csv。我需要將交換csv中的primarysmtpaddress解析到下一列,並僅解析基於samaccountname的地址。

此外,歡迎任何建議。第一次來這裏:)

廣告腳本

$ErrorActionPreference = 'Continue' 
$SamAccountPath = "Path\get_adusers.csv" 
$SamAccountPathE = "Path\get_adusers_edit.csv" 
$Folder = "Path\mismatch_script" 
$logfile = "Path\mismatch_script\get-user.txt" 

Try{ 
if (Test-Path $Folder) 
{} 
else 
{ 
    New-Item -Path "$Folder" -ItemType Directory -Force | Out-Null 
} 
} Catch { 
Write-Output "Could not create log folder" | Out-File $logfile -Append 

} 
Try 
{ 
    Import-Module ActiveDirectory 

} Catch { 
    Write-Output "Could not import ActiveDirectory module." | Out-File $logfile -Append 

} 

Try{ 
Get-Aduser -filter * -properties Samaccountname, emailaddress, enabled | ? { $_.enabled -eq $true } | select samaccountname, emailaddress |` 
?{$_.emailaddress -ne $null} | Sort-Object samaccountname |` 
Export-Csv -Path $SamAccountPath -NoTypeInformation -Force | Out-Null 
Get-Content $SamAccountPath | select -Skip 1 | ConvertFrom-Csv -Header 'Samaccountname','primarysmtpaddress' | Export-Csv -Path $SamAccountPathE -NoTypeInformation -Force |Out-Null 
Remove-Item $SamAccountPath -Force 

} Catch { 

Write-Output "Could not get ad users" | Out-File $logfile -Append 
}   

C:\Windows\System32\WindowsPowerShell\v1.0\powershell.exe -version 2.0 -noexit -command ". 'C:\Program Files\Microsoft\Exchange Server\V14\bin\RemoteExchange.ps1'; Connect-ExchangeServer -auto; Path\get-exuser_information.ps1" 

交易所腳本

$AliasPath = "Path\get_exusers.csv" 
$SamAccountPath = "Path\get_adusers_edit.csv" 
$MismatchPath = "Path\get_diff.csv" 
$logfile = "Path\get-user.txt" 

$mboxes = get-mailbox -Resultsize Unlimited -identity 
$samobj.Samaccountname | Where-Object {$_.recipienttype -eq "UserMailbox"} | select samaccountname, primarysmtpaddress 
$ExUserL = Import-Csv $AliasPath 
$AdUserL = Import-Csv $SamAccountPath | Where-Object {$_.samaccountname} 
$samobj = New-Object PSObject -Property @{ 'Samaccountname' = $_.samaccountname } 
$Compare = Compare-Object -ReferenceObject $ExUserL -DifferenceObject  
$AdUserL = Import-Csv $SamAccountPath | Where-Object {$_.samaccountname} 


Try { 
ForEach-Object{ 

Import-Csv $SamAccountPath | select samaccountname | Out-Null 
} 
$mboxes | Sort-Object samaccountname | Export-Csv $AliasPath -NoTypeInformation -Force | Out-Null 

} Catch { 
Write-Output "Could not get mailboxes" | Out-File $logfile -Append 
} 

Try { 

$compare | foreach { 
if ($_.sideindicator -eq '<=') 
{$_.sideindicator = '' 
$_.samaccountname = '' 
$_.primarysmtpaddress = '' 
} 

if ($_.sideindicator -eq '=>') 
{$_.sideindicator = "Mismatch"} 

} 

$compare | Export-Csv $MismatchPath -NoTypeInformation -Force | Out-Null 

} Catch { 
Write-Output "Could not compare ex and ad csvs" | Out-File $logfile -Append 
} 

Send-MailMessage -SmtpServer "server" -Attachments $MismatchPath -From "email" -to "email" -Subject "Mismatch Report" 

我改寫了一下,發現這個解決方案:

#Globals for warnings/error action 
    $WarningPreference = 'SilentlyContinue' 
    $ErrorActionPreference = 'SilentlyContinue' 

    #import activedirectory cmdlets 
    import-module -name activedirectory 

    #Collects Exchange cmdlets into PSSession and imports it into the current session. 
    $exchangeuser = "user" 
    $session = New-PSSession -ConfigurationName Microsoft.Exchange -ConnectionURI http://exchange/powershell/ -Authentication kerberos 
    import-PSSession $session -AllowClobber 

    #Declare paths 
    $MismatchPath = "Path\AD_EX_Mismatches.csv" 
    $MismatchPathE = "Path\AD_EX_MismatchesE.csv" 

    #Creates empty array 
    $errorstatus = @() 

    #Gets all mailboxes that are 'UserMailboxes' and not 'Contacts' it then pipes sam and psmtp to get-aduser. The @{n="";e={}} is creating a table and running an expression or cmdlet. 
    $gUser = Get-Mailbox -ResultSize Unlimited | Where-Object {$_.recipienttype -eq "UserMailbox"} | select samaccountname, primarysmtpaddress,@{n="ADEmail";e = {Get-ADuser $_.samaccountname -Properties emailaddress | select -ExpandProperty emailaddress}},@{n="Error";e={$errorstatus}} 

    #Foreach object (Samaccountname,primarysmtpaddress,emailaddress,error). Check if the conditions are met, if so, output the $gUser.Error to the CSV 
    $gUser | ForEach-Object { 

    if($_.ADEmail -ne $_.PrimarySmtpAddress){ 
    $_.Error = "Mismatch" 
    } 

    if($_.ADEmail -eq ' '){ 
    $_.Error = "Corrupted" 
    } 
    } 
    $gUser | Export-Csv $MismatchPath -NoTypeInformation -Force | Out-Null 

    #Finds blanks in the csv and re-exports it to a new path. 
    Import-Csv $MismatchPath | Where-Object {$_.samaccountname -and $_.primarysmtpaddress -and $_.error -notlike ""} | select * | export-csv $MismatchPathE -NoTypeInformation -Force 

    #Finds EX session and removes it 
    Get-PSSession | Remove-PSSession 

    #Send email with args 
    Send-MailMessage -SmtpServer "mailserver" -Attachments $MismatchPathE -From "emailaddress" -to "email address" -Subject "Mismatch Report" -Body "Attached is a report of mismatches of email addresses between AD and Exchange." 
+2

你問了一個碼審查或您的當前代碼有問題? – Matt

+0

我需要弄清楚如何從1 csv(「Path \ get_exusers.csv」)「primarysmtpaddress」列中獲取數據。然後將它與從(「Path \ get_exusers.csv」)和(「Path \ get_adusers.csv」)的比較對象生成的不匹配文件相關聯。最終結果是不匹配csv(Path \ get_diff.csv)。我需要從(「Path \ get_exusers.csv」)中獲取「primarysmtpaddress」列,並僅添加與不匹配csv中的「samaccountname」列相關的地址。 – mtsmith

回答

0
#Globals for warnings/error action 
$WarningPreference = 'SilentlyContinue' 
$ErrorActionPreference = 'SilentlyContinue' 

    #import activedirectory cmdlets 
import-module -name activedirectory 


#Collects Exchange cmdlets into PSSession and imports it into the current session. 
$exchangeuser = "user" 
$session = New-PSSession -ConfigurationName Microsoft.Exchange -ConnectionURI http://EX/powershell/ -Authentication kerberos 
import-PSSession $session -AllowClobber 

#Declare paths 
$MismatchPath = "E:\Utilities\Scripts\Set_DashboardData\Mismatch\1.csv" 
$MismatchPathE = "E:\Utilities\Scripts\Set_DashboardData\Mismatch\1E.csv" 
$ReportPath = "E:\Reports\Exchange\EXDashboard\" 

#Creates empty array 
$errorstatus = @() 
$Fix = @() 
#Gets all mailboxes that are 'UserMailboxes' and not 'Contacts' it then pipes sam and psmtp to get-aduser. The @{n="";e={}} is creating a table and running an expression or cmdlet. 
$gUser = Get-Mailbox -ResultSize Unlimited | Where-Object {$_.recipienttype -eq "UserMailbox"} | select samaccountname, primarysmtpaddress,@{n="ADEmail";e = {Get-ADuser $_.samaccountname -Properties emailaddress | select -ExpandProperty emailaddress}},@{n="Error";e={$errorstatus}} 

#Foreach object (Samaccountname,primarysmtpaddress,emailaddress,error). Check if the conditions are met, if so, output the $gUser.Error to the CSV 
$gUser | ForEach-Object { 

if($_.ADEmail -ne $_.PrimarySmtpAddress){ 
$_.Error = "Mismatch" 
} 

if($_.ADEmail -eq ' '){ 
$_.Error = "Corrupted" 
    } 
} 
$gUser | Export-Csv $MismatchPath -NoTypeInformation -Force | Out-Null 

#Finds blanks in the csv and re-exports it to a new path. 
Import-Csv $MismatchPath | Where-Object {$_.samaccountname -and $_.primarysmtpaddress -and $_.error -notlike ""} | export-csv $MismatchPathE -NoTypeInformation -Force 

#Deletes original file 
Remove-Item $MismatchPath -Force 

#Finds EX session and removes it 
Get-PSSession | Remove-PSSession