Created
April 22, 2022 02:05
-
-
Save carlosdoliveira/695b5704b37b9b7bc009ec3c0b642a38 to your computer and use it in GitHub Desktop.
Como substituir sufixos UPN
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Import-Module ActiveDirectory | |
# Insira o sufixo antigo | |
$oldSuffix = "contoso.corp" | |
# Insira o sufixo novo | |
$newSuffix = "dreamzone.com.br" | |
# Informe o caminho do DC | |
$dc = "DC=contoso,DC=corp" | |
#### Não mude nada a partir daqui #### | |
# Criando configuração de menu de alteração | |
$changeAll = New-Object System.Management.Automation.Host.ChoiceDescription '&All', 'Aplicar para todos os usuários' | |
$changeAList = New-Object System.Management.Automation.Host.ChoiceDescription '&List', 'Aplicar para uma lista' | |
$options = [System.Management.Automation.Host.ChoiceDescription[]]($changeAll, $changeAList) | |
$title = "Modo de Execução" | |
$message = "Você gostaria de aplicar esta configuração de que forma" | |
$result = $host.ui.PromptForChoice($title, $message, $options, 0) | |
# Criação do Switch baseado na escolha do usuário | |
switch ($result) { | |
1 { | |
# Obrigatoriamente informar caminho do CSV | |
$Path = Read-Host "Informe o local do arquivo CSV"; | |
try { | |
$users = Import-Csv -Path $Path -ErrorAction Stop | |
} | |
catch { | |
"é necessário informar um caminho" | |
return | |
} | |
if ($users) { | |
foreach ($user in $users) { | |
try { | |
Get-ADUser -Filter "UserPrincipalName -eq '$($user.UPN)'" -ErrorAction Stop | ` | |
ForEach-Object { $newUpn = $_.UserPrincipalName.Replace($oldSuffix, $newSuffix) $_ | ` | |
Set-ADUser -UserPrincipalName $newUpn | |
} | |
} | |
catch { | |
Write-host "não foi possível trocar o UPN do usuário" | |
} | |
} | |
} | |
else { | |
Write-Host "Lista inválida ou vazia" | |
return | |
} | |
} | |
0 { | |
Get-ADUser -SearchBase $dc -filter * | ForEach-Object { | |
$newUpn = $_.UserPrincipalName.Replace($oldSuffix, $newSuffix) $_ | Set-ADUser -UserPrincipalName $newUpn | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment