我有2个csv,1个来自交换,1个来自AD。我正在查询samaccountname,emailaddress,在AD中启用。我正在查询samaccountname,primarysmtpaddress,其中recipienttype是' usermailbox'。
在比较这些之后,它会根据发现的不匹配情况吐出第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."
答案 0 :(得分: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