powershell 轻松更新所有AD用户电子邮件域的脚本

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了powershell 轻松更新所有AD用户电子邮件域的脚本相关的知识,希望对你有一定的参考价值。

Param (
	[string]$oldDomain = "{OLDDOMAIN}",
	[string]$newDomain = "{NEWDOMAIN}",
	[string]$SearchBase = "{SEARCHBASE}"
)

Write-Verbose "$(Get-Date): Loading ActiveDirectory module..."
Try { Import-Module ActiveDirectory -ErrorAction Stop }
Catch { Write-Host "Unable to load Active Directory module, is RSAT installed?" -ForegroundColor Red; Exit }

Get-ADUser -Filter * -Properties givenName, Surname, EmailAddress, sAMAccountName -searchbase $SearchBase | %{
	if ($_.SurName -eq $null)
	{
		Write-Host "User Name:" $($_.sAMAccountName) "has been not been updated" -ForegroundColor Yellow
	}
	else
	{
		if ($_.EmailAddress)
		{
			if ($_.EmailAddress.EndsWith($oldDomain))
			{
				#Write-Output $($_.EmailAddress).Replace($oldDomain, $newDomain)
				Set-ADUser -Identity $_ -EmailAddress $($_.EmailAddress).Replace($oldDomain, $newDomain)
				Write-Host "User Name:" $($_.sAMAccountName) "has been updated" -ForegroundColor Green
			}
		}
		else
		{
			Write-Host "User Name:" $($_.sAMAccountName) "has not been updated" -ForegroundColor Red
		}
	}
}

以上是关于powershell 轻松更新所有AD用户电子邮件域的脚本的主要内容,如果未能解决你的问题,请参考以下文章

在AD中导出所有用户资料

Powershell管理系列(四十)PowerShell查询和解锁AD账号(改进后,只发一次邮件)

Powershell管理系列(三十九)PowerShell查询和解锁AD账号

Powershell管理系列(三十九)PowerShell查询和解锁AD账号

Powershell 修改用户配置文件

Powershell日常AD管理-1