IT STORYs

이메일 주소 기준으로 UPN 변경 스크립트 본문

스크립트

이메일 주소 기준으로 UPN 변경 스크립트

295~ 2023. 6. 1. 08:13

최근 3일내 생성된 AD 계정기준으로

이메일 주소값으로 UPN 값을 변경하는 스크립트

$InputFile = $null
$InputFile2 = $null
$InputFile3 = $null

######## @gsconst.local+최근3일+AD활성화 된 계정 모으기
$prvDate = ((Get-Date).AddDays(-3)).Date
$InputFile = Get-ADUser -Filter "EmailAddress -like '*@gsenc.com' -and enabled -eq 'true'" -SearchBase "OU=GS건설,OU=Users,OU=GSEnC,DC=gsconst,DC=local" -Properties whenCreated | where {$_.whenCreated -ge $prvDate} 

$InputFile2 = Get-ADUser -Filter "EmailAddress -like '*@xisnd.com' -and enabled -eq 'true'" -SearchBase "OU=GS건설,OU=Users,OU=GSEnC,DC=gsconst,DC=local" -Properties whenCreated | where {$_.whenCreated -ge $prvDate} 

$InputFile3 = Get-ADUser -Filter "EmailAddress -like '*@xicna.com' -and enabled -eq 'true'" -SearchBase "OU=GS건설,OU=Users,OU=GSEnC,DC=gsconst,DC=local" -Properties whenCreated | where {$_.whenCreated -ge $prvDate} 

$report = @()

######## UPN 변경하기
foreach ($i in $InputFile)
{
######## 변경 전UPN 정보 기록
	$tmp = Get-AdUser -identity $i.SamAccountName | Select  UserPrincipalName,SamAccountName,name
	$report += $tmp

	$tmp = $i.SamAccountName + "@gsenc.com"
	Set-AdUser -identity $i.SamAccountName -UserPrincipalName $tmp	
}
foreach ($i in $InputFile2)
{
######## 변경 전UPN 정보 기록
	$tmp = Get-AdUser -identity $i.SamAccountName | Select  UserPrincipalName,SamAccountName,name
	$report += $tmp

	$tmp = $i.SamAccountName + "@xisnd.com"
	Set-AdUser -identity $i.SamAccountName -UserPrincipalName $tmp	
}
foreach ($i in $InputFile3)
{
######## 변경 전UPN 정보 기록
	$tmp = Get-AdUser -identity $i.SamAccountName | Select  UserPrincipalName,SamAccountName,name
	$report += $tmp

	$tmp = $i.SamAccountName + "@xicna.com"
	Set-AdUser -identity $i.SamAccountName -UserPrincipalName $tmp	
}

$filename = get-date -uformat "C:\batch\setaduser_log\%Y%m%d_%H%M%S_Set_Aduser.txt" 
$report | export-csv $filename -NoTypeInformation -encoding UTF8
Comments