Exchange 2010 Powershell脚本攻略(五)

CheckInvalidRecipients

Param(

[string] $OrganizationalUnit,

[string] $ResultSize = "Unlimited",

[string] $Filter,

[string] $DomainController,

[switch] $FixErrors,

[switch] $RemoveInvalidProxies,

[switch] $ShowInvalidProxies,

[switch] $OutputObjects

)

# Catch any random input and output the help text

if ($args) {

@"

Usage: CheckInvalidRecipients.ps1 [-OrganizationalUnit ] [-ResultSize ]

[-Filter ] [-DomainController ] [-FixErrors] [-RemoveInvalidProxies]

[-ShowInvalidProxies] [-OutputObjects]

This script is designed to return information on invalid recipient objects and possible

attemtpt to fix them.

-OrganizationalUnit: The OU the script will run against. The default is to run against

the current session scope.

-ResultSize: The maximum number of recipients of each type to return.

The four types are: User, Contact, Group, and DynamicDistributionGroup.

The default will return all recipients in the current scope.

-Filter: The filter that should used to retrieve recipients.

-DomainController: The domain controller the script should run against. The default is to

run against a well-connected domain controller in the current scope.

-FixErrors: Script will attempt to fix recipient errors it encounters.

-RemoveInvalidProxies: Script will attempt to remove invalid email addresses it encounters.

(-RemoveInvalidProxies must be specified with -FixErrors)

-ShowInvalidProxies: Script will display invalid email addresses it encounters.

-OutputObjects: Script will output any objects it processes to the pipeline.

NOTE: This script may re-read recipient data if it is necessary to complete the desired operation.

To improve performance, we will not re-read the data unless one of the following parameters

is specified: -FixErrors, -ShowInvalidProxies

This means piping objects from the Get-Recipient task will only work correctly if one of these

parameters is specified. (This is because the output from Get-Recipient never returns

validation errors)

Currently, the script can fix the following errors:

1. Primary SMTP Address Problems: If a recipient has multiple SMTP addresses listed as primary or

the primary SMTP is invalid, the script will try to set the

WindowsEmailAddress as the primary SMTP address, since that is

the address Exchange 2003 would have recognized as the primary

(although E12 does not).

2. External Email Address Problems: If a recipient has an external email address, but that address

is missing from the EmailAddresses collection, the script will

attempt to add it to the EmailAddresses.

3. Invalid Email Addresses: If a recipient has invalid email addresses in their EmailAddresses

collection it may prevent some scripts from working correctly on

that recipient. In order to prevent this potential problem the

script is capable of removing any offending email addresses from

the recipient. To remove invalid email addresses, please specify

both the -FixErrors and the -RemoveInvalidProxies parameters

Example Usages:

Display validation errors for all recipients in the current scope:

.CheckInvalidRecipients.ps1

Fix all recipients in the 'Users' container that have invalid Primary SMTP addresses:

.CheckInvalidRecipients.ps1 -OrganizationalUnit 'Users' -FixErrors

Return all recipients in the current scope after fixing any email address problems:

.CheckInvalidRecipients.ps1 -FixErrors -RemoveInvalidProxies -OutputObjects

Display validation errors and invalid email addresses for mailboxes in the current scope:

Get-Recipient -RecipientType UserMailbox | .CheckInvalidRecipients.ps1 -ShowInvalidProxies

"@

exit

}

############################################################ Function Declarations ################################################################

function HasValidWindowsEmailAddress($obj)

{

return $obj.WindowsEmailAddress.IsValidAddress

}

function HasInvalidPrimarySmtp($obj)

{

return !$obj.PrimarySmtpAddress.IsValidAddress

}

function IsValid($obj)

{

if (!$obj.IsValid)

{ return $false }

foreach ($address in $obj.EmailAddresses)

{

if ($address -is [Microsoft.Exchange.Data.InvalidProxyAddress])

{ return $false }

}

return $true

}

function WriteErrorMessage($str)

{

Write-host $str -ForegroundColor Red

}

function WriteInformation($str)

{

Write-host $str -ForegroundColor Yellow

}

function WriteSuccess($str)

{

Write-host $str -ForegroundColor Green

}

function WriteWarning($str)

{

$WarningPreference = $Global:WarningPreference

write-warning $str

}

function PrintValidationError($obj)

{

foreach($err in $obj.Validate())

{

WriteErrorMessage('{0},{1},{2}' -f $obj.Id,$err.PropertyDefinition.Name,$err.Description)

}

}

function EvaluateErrors($Recipient)

{

PrintValidationError($Recipient)

$tasknoun = $null

# We're comparing the RecipientType to the enum value instead of strings, because the strings may be localized and then this comparison would fail

switch ($Recipient.RecipientType)

{

{$_ -eq [Microsoft.Exchange.Data.Directory.Recipient.RecipientType]::UserMailbox} { $tasknoun = "Mailbox" }

{$_ -eq [Microsoft.Exchange.Data.Directory.Recipient.RecipientType]::MailUser} { $tasknoun = "Mailuser" }

{$_ -eq [Microsoft.Exchange.Data.Directory.Recipient.RecipientType]::MailContact} { $tasknoun = "Mailcontact" }

{$_ -eq [Microsoft.Exchange.Data.Directory.Recipient.RecipientType]::MailUniversalDistributionGroup} { $tasknoun = "DistributionGroup" }

{$_ -eq [Microsoft.Exchange.Data.Directory.Recipient.RecipientType]::MailUniversalSecurityGroup} { $tasknoun = "DistributionGroup" }

{$_ -eq [Microsoft.Exchange.Data.Directory.Recipient.RecipientType]::MailNonUniversalGroup} { $tasknoun = "DistributionGroup" }

{$_ -eq [Microsoft.Exchange.Data.Directory.Recipient.RecipientType]::DynamicDistributionGroup} { $tasknoun = "DynamicDistributionGroup" }

}

if (($tasknoun -ne $null) -AND ($FixErrors -OR $ShowInvalidProxies))

{

# Prepare the appropriate get/set tasks that need to run

$GetRecipientCommand = "get-$tasknoun"

if (![String]::IsNullOrEmpty($DomainController))

{ $GetRecipientCommand += " -DomainController $DomainController" }

$SetRecipientCommand = "set-$tasknoun"

if (![String]::IsNullOrEmpty($DomainController))

{ $SetRecipientCommand += " -DomainController $DomainController" }

# Read the object using the correct Get-Task, so we get all the email properties

$Recipient = &$GetRecipientCommand $Recipient.Identity

# Nothing to do if the recipient is completely valid except output it to the pipeline

if (IsValid($Recipient))

{

# Output the object to the pipeline

if ($OutputObjects)

{ Write-Output $Recipient }

return;

}

# Collect all the invalid proxy addresses in case we need them later

$InvalidProxies = @()

foreach ($Address in $Recipient.EmailAddresses)

{

if ($Address -is [Microsoft.Exchange.Data.InvalidProxyAddress])

{

$InvalidProxies += $Address

}

}

if ($ShowInvalidProxies -AND ($InvalidProxies.Length -gt 0))

{

foreach ($Address in $InvalidProxies)

{

WriteErrorMessage('{0},{1},{2}' -f $Recipient.Id,"EmailAddresses",$Address.ParseException.ToString())

}

}

if ($FixErrors)

{

$RecipientModified = $false

# Fix the major PrimarySmtpAddress problems

# If the WindowsEmailAddress is valid, we'll set that as the Primary since Exchange 2003 used that as the Primary

if ((HasValidWindowsEmailAddress($Recipient)) -AND

(HasInvalidPrimarySmtp($Recipient)))

{

$Recipient.PrimarySmtpAddress = $Recipient.WindowsEmailAddress

WriteInformation("New PrimarySmtpAddress for {0}: {1}" -f $Recipient.Identity, $Recipient.WindowsEmailAddress)

$RecipientModified = $true

}

# If the ExternalEmailAddress is missing from the EmailAddresses collection, we should add it back

if (($null -ne $Recipient.ExternalEmailAddress) -AND

!($Recipient.EmailAddresses.Contains($Recipient.ExternalEmailAddress)))

{

$Recipient.EmailAddresses.Add($Recipient.ExternalEmailAddress)

$RecipientModified = $true

}

# Remove all the invalid proxy addresses if the user specified the RemoveInvalidProxies flag

if ($RemoveInvalidProxies -AND ($InvalidProxies.Length -gt 0))

{

foreach ($Address in $InvalidProxies)

{

# Using this DummyVariable so the script doesn't output the result of the Remove operation

$DummyVariable = $Recipient.EmailAddresses.Remove($Address)

WriteInformation("Removed invalid proxy address from {0}: {1}" -f $Recipient.Identity, $Address)

}

$RecipientModified = $true

}

# Let's try to save the object back to AD

if ($RecipientModified)

{

$numErrors = $error.Count

&$SetRecipientCommand -Instance $Recipient

if ($error.Count -eq $numErrors)

{

WriteSuccess("Successfully saved '{0}'" -f $Recipient.Identity)

}

else

{

WriteErrorMessage("Error while saving '{0}'" -f $Recipient.Identity)

}

}

# Re-read the recipient if we modified it in any way and we want to output it to the pipeline

if ($OutputObjects)

{ $Recipient = &$GetRecipientCommand $Recipient.Identity }

} # if ($FixErrors)

} # if (($tasknoun -ne $null) -AND ($FixErrors -OR $ShowInvalidProxies))

# Output the object to the pipeline

if ($OutputObjects)

{ Write-Output $Recipient }

} # EvaluateErrors

############################################################ Function Declarations End ############################################################

############################################################ Main Script Block ####################################################################

#Ignore Warnings output by the task

$WarningPreference = 'SilentlyContinue'

if ($RemoveInvalidProxies -AND !$FixErrors)

{ WriteWarning("RemoveInvalidProxies has no effect unless FixErrors is also specified") }

# Check if we have any pipeline input

# If yes, MoveNext will return true and we won't run our get tasks

if ($input.MoveNext())

{

# Reset the enumerator so we can look at the first object again

$input.Reset()

if ($ResultSize -NE "Unlimited")

{ WriteWarning("ResultSize parameter has no effect when piping input") }

if (![String]::IsNullOrEmpty($OrganizationalUnit))

{ WriteWarning("OrganizationalUnit parameter has no effect when piping input") }

if (![String]::IsNullOrEmpty($Filter))

{ WriteWarning("Filter parameter has no effect when piping input") }

foreach ($Recipient in $input)

{

# skip over inputs that we can't handle

if ($Recipient -eq $null -OR

$Recipient.RecipientType -eq $null -OR

$Recipient -isnot [Microsoft.Exchange.Data.Directory.ADObject])

{ continue; }

EvaluateErrors($Recipient)

}

}

else

{

$cmdlets =

@("get-User",

"get-Contact",

"get-Group",

"get-DynamicDistributionGroup")

foreach ($task in $cmdlets)

{

$command = "$task -ResultSize $ResultSize"

if (![String]::IsNullOrEmpty($OrganizationalUnit))

{ $command += " -OrganizationalUnit $OrganizationalUnit" }

if (![String]::IsNullOrEmpty($DomainController))

{ $command += " -DomainController $DomainController" }

if (![String]::IsNullOrEmpty($Filter))

{ $command += " -Filter $Filter" }

invoke-expression $command | foreach { EvaluateErrors($_) }

}

}

[@more@]

来自 “ ITPUB博客 ” ,链接:http://blog.itpub.net/23700676/viewspace-1052331/,如需转载,请注明出处,否则将追究法律责任。

转载于:http://blog.itpub.net/23700676/viewspace-1052331/

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
使用 JavaScript 编写的记忆游戏(附源代码)   项目:JavaScript 记忆游戏(附源代码) 记忆检查游戏是一个使用 HTML5、CSS 和 JavaScript 开发的简单项目。这个游戏是关于测试你的短期 记忆技能。玩这个游戏 时,一系列图像会出现在一个盒子形状的区域中 。玩家必须找到两个相同的图像并单击它们以使它们消失。 如何运行游戏? 记忆游戏项目仅包含 HTML、CSS 和 JavaScript。谈到此游戏的功能,用户必须单击两个相同的图像才能使它们消失。 点击卡片或按下键盘键,通过 2 乘 2 旋转来重建鸟儿对,并发现隐藏在下面的图像! 如果翻开的牌面相同(一对),您就赢了,并且该对牌将从游戏中消失! 否则,卡片会自动翻面朝下,您需要重新尝试! 该游戏包含大量的 javascript 以确保游戏正常运行。 如何运行该项目? 要运行此游戏,您不需要任何类型的本地服务器,但需要浏览器。我们建议您使用现代浏览器,如 Google Chrome 和 Mozilla Firefox, 以获得更好、更优化的游戏体验。要玩游戏,首先,通过单击 memorygame-index.html 文件在浏览器中打开游戏。 演示: 该项目为国外大神项目,可以作为毕业设计的项目,也可以作为大作业项目,不用担心代码重复,设计重复等,如果需要对项目进行修改,需要具备一定基础知识。 注意:如果装有360等杀毒软件,可能会出现误报的情况,源码本身并无病毒,使用源码时可以关闭360,或者添加信任。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值