Deleting one alias from proxyaddresses

Hi everyone.

 

I’m having an issue with a script i’m working on.

here it is:

[pre]

<#
Script de check, ajout et suppression d’alias
mis en place sur l’ad
2 Fonctions menu + 3 fonctions active
Aucune ressource externe nécéssaire
#>

#Script par Kevin LION CDS de grenoble#

#Menu principal#
Function Show-Menu
{
param (
[string]$Title = ‘Administration des alias’
)
Clear-Host
Write-Host “=== $Title ===” -ForegroundColor yellow -BackgroundColor Black
Write-Host “1: Vérification des alias en place.” -BackgroundColor Black -foregroundcolor green
Write-Host "2: Ajout d’alias sur matricule "-BackgroundColor Black -foregroundcolor green
Write-Host “3: Suppresion d’alias sur matricule”-BackgroundColor Black -foregroundcolor green
Write-Host “Q: entré Q pour quitter”-BackgroundColor red
Write-Host “”
}

#Fonction de choix menu#
Function menu
{
switch ($selection)
{
‘1’ {
CheckProxy
} ‘2’ {
AddAlias
} ‘3’ {
DelAlias
} ‘q’ {
exit
}
}
}

#Fonction de vérification alias en place#
Function CheckProxy {
$user = read-host “Entré un matricule”
Get-ADUser -server ******** -Identity $user -Properties proxyaddresses |Select-Object -ExpandProperty proxyaddresses |Foreach-Object { [pscustomobject]@{ ProxyAddresses = $_ }}
pause
}

#Fonction d’ajout d’alias#
Function AddAlias {
$user = read-host “Entré un matricule”
$cred = Get-Credential
Write-host “===== Voici le format à utiliser ci dessous ===== 'smtp:alias@*.fr’ (SMTP Majuscule pour adresse principale)" -Backgroundcolor black -foregroundcolor yellow
write-host "Exemple: smtp:alias@
.fr” -Backgroundcolor black -foregroundcolor yellow
$Alias = Read-host “Entré l’alias à mettre en place”
Set-ADUser -credential $cred -server “********” -identity $user -Add @{ProxyAddresses="$alias"}
#Verification de l’ajout#
Write-host “tentative d’ajout, Appuyer sur entré pour vérifier l’ajout” -Backgroundcolor black -foregroundcolor green
pause
Get-ADUser -server ******* -Identity $user -Properties proxyaddresses |Select-Object -ExpandProperty proxyaddresses |Foreach-Object { [pscustomobject]@{ ProxyAddresses = $_ }}
pause
}

#Fonction suppression d’alias#
Function DelAlias {
}

#Debut du programme partie active#
While ($selection -ne “q”) {
show-menu
$selection = Read-Host “Veuillez entrée un chiffre et appuyez sur entrée”
menu
} [/pre]

I just can’t figure out how i should do my delete function.

My resarch only show functions to delete in mass.

I’m thinking about asking for the proxyaddresses already setup again and copy paste the one i want to delete but i jus’t cant seem to find what i want on my researches.

 

Can anyone pinpoint me to a ressource that could help me?

Thanks

 

 

I think it would be better if you post only the relevant part of your script and explain what it should do and what the problem is. Regardless of that - you should start to format your code nicely so it is more readable and easier to understand. You might read The Unofficial PowerShell Best Practices and Style Guide.

I believe Set-ADUser’s -Remove parameter will handle that.

Set-AdUser -Identity bob -Remove @{proxyaddresses = "remove.me@justthis.com"}

You can take an element out of an array like this, but Mark’s answer is better.

PS C:\Users\js> 'one','two','three' -ne 'one'
two
three

Hey everyone, thanks for the replies.

[quote quote=155613]I think it would be better if you post only the relevant part of your script and explain what it should do and what the problem is. Regardless of that – you should start to format your code nicely so it is more readable and easier to understand. You might read The Unofficial PowerShell Best Practices and Style Guide.

[/quote]
Thanks a lot for this link i will for sure read it and apply it :slight_smile:

[quote quote=155661]I believe Set-ADUser’s -Remove parameter will handle that.

Set-AdUser -Identity bob -Remove @{proxyaddresses = "remove.me@justthis.com"}
XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX
[/quote] I came up with the same solution by myself in the meantime but it doesn't seems to work =(

[pre]

Function DelAlias {
checkproxy
write-host “========= Merci de copier l’alias à supprimer =========” -Backgroundcolor black -foregroundcolor yellow
pause
$alias = Read-Host “copier ici l’alias à supprimer”
Set-ADUser -credential $cred -server “*******” -identity $user -Remove @{ProxyAddresses="$alias"}

} [/pre]

I get no error message but the alias isn’t deleted either.

I’m going to keep searching, i have one or two leads.

EDIT/Followup:

Okay, i just managed to do it, the problem is that my $user variable is taken in the checking function but its not reused later in my deleting function.

Is there a way to keep my variable in a later function? Should i rething the way i’m inputing the user variable and make it outside of a function?

I’d recommend to make it a parameter of the function and provide it that way. That even keeps the function reusable.