Bonjour,
Je suis toujours a la recherche d'une solution a mon problème. Voici le contenu d'un fichier "_TMP_UserDeleteOffice365.ps1" créé à la suite d'une tentative de suppression de compte office 365 via la console KoXo :
###############################################################################
# #
# Deleting Mailbox for Office 365 #
# V3.92 #
# (C)2006-2017 KoXo Developpement #
# #
###############################################################################
function log_message
{
Param($Header,$Message)
$LogDate=Get-Date
$LogInfo="[$($LogDate)]: [$($Header)] $($Message)"
$LogInfo | Out-File -FilePath $LogFile -Encoding UTF8 -Append -Force
}
###############################################################################
function log_total_errors_count
{
$S="TotalErrorsCount=$($global:total_errors_count)"
$S | Out-File -FilePath $LogFile -Encoding UTF8 -Append -Force
}
###############################################################################
function log_errors_count
{
Param($ID)
$S="ErrorsCount[$($ID)]=$($global:errors_count)"
$S | Out-File -FilePath $LogFile -Encoding UTF8 -Append -Force
}
###############################################################################
function check_version
{
$S=Get-Host
if ($S.Version.Major -lt "2")
{
$S="Powershell's version must be greater or equal to 2.0 !"
log_message "Version" $S
$global:total_errors_count++
write-host $S -foregroundcolor "red"
write-host "Press 'Enter' to continue ..."
Read-Host
exit
}
}
###############################################################################
function welcome
{
write-host "Deleting user for Office 365 $($Version)" -foregroundcolor "white"
write-host "(C) 2006-2017 KoXo Developpement" -foregroundcolor "white"
}
###############################################################################
function connect
{
Param([string] $MyURL,$MyName,$MyPassword,$ProxyEnabled,$ProxyUser,$ProxyPassword)
$error.Clear()
#Proxy
if ($ProxyEnabled)
{
if (![String]::IsNullorEmpty($ProxyUser))
{
$SecuredPassword=ConvertTo-SecureString $ProxyPassword -AsPlainText -Force
$ProxyCred=New-Object System.Management.Automation.PSCredential $ProxyUser, $SecuredPassword
$PSSessionOption=New-PSSessionOption -ProxyAccessType IEConfig -ProxyAuthentication Negotiate -ProxyCredential $ProxyCred
$S="Connection with Internet Explorer proxy parameters, User=$($ProxyUser)"
log_message "OK" $S
}
else
{
$PSSessionOption=New-PSSessionOption -ProxyAccessType IEConfig -ProxyAuthentication Negotiate
$S="Connection with Internet Explorer proxy parameters"
log_message "OK" $S
}
write-host $S -foregroundcolor "cyan"
}
# Set credential
if ( ([String]::IsNullorEmpty($MyPassword)) -or ([String]::IsNullorEmpty($MyName)) )
{
$Cred=Get-Credential
}
else
{
$SecuredPassword=ConvertTo-SecureString $MyPassword -AsPlainText -Force
$Cred=New-Object System.Management.Automation.PSCredential $MyName, $SecuredPassword
}
# Creating new runspace.
if ($ProxyEnabled)
{
$Script:RS=New-PSSession -ConfigurationName microsoft.exchange -ConnectionUri $MyURL -Credential $Cred -Authentication Basic -AllowRedirection -SessionOption $PSSessionOption -ErrorAction SilentlyContinue
}
else
{
$Script:RS=New-PSSession -ConfigurationName microsoft.exchange -ConnectionUri $MyURL -Credential $Cred -Authentication Basic -AllowRedirection -ErrorAction SilentlyContinue
}
if ([String]::IsNullorEmpty($error[0]))
{
$S="Runspace Creation was successful = $($MyURL)"
log_message "OK" $S
# Import-PSSession $Script:RS
}
else
{
$global:total_errors_count++
$S="Runspace creation was unsuccessful to $($MyURL) || $($error[0])"
log_message "ERROR" $S
# Display error
write-host $S -foregroundcolor "red"
write-host "Press 'Enter' to continue ..."
Read-Host
}
return $Cred
}
###############################################################################
function close
{
$error.Clear()
$Script:RS | Remove-PSSession -ErrorAction SilentlyContinue
if ([String]::IsNullorEmpty($error[0]))
{
$S="Successfully cleared Runspace"
log_message "OK" $S
}
else
{
$global:total_errors_count++
$S="Clearing Runspace was unsuccessful. Error = $($error[0])"
log_message "ERROR" $S
}
}
###############################################################################
function user_exists
{
Param([string] $email)
$error.Clear()
$User=Invoke-Command -Session $Script:RS -ErrorAction SilentlyContinue -ArgumentList $email {Param($id) Get-User -Identity $id}
return (![string]::IsNullOrEmpty($User))
}
###############################################################################
function delete_user
{
Param($UserDisplayName,$UserEmail)
$error.Clear()
%{Invoke-Command -Session $Script:RS {param ($id) remove-mailbox -Identity $id -Confirm:$false}-arg $UserEmail}>$Results
if ([String]::IsNullorEmpty($error[0]))
{
$S="Successfully delete mailbox for $($UserDisplayName) ($($UserEmail))"
log_message "OK" $S
}
else
{
$global:errors_count++
$S="Deleting mailbox for $($UserDisplayName) ($($UserEmail)). Error = $($error[0])"
log_message "ERROR" $S
}
}
###############################################################################
function display_user
{
Param ($UserDisplayName,$UserEmail)
Write-Host "User: """ -nonewline
Write-Host $UserDisplayName -foregroundcolor "Green" -nonewline
Write-Host """ Email: """ -nonewline
Write-Host $UserEmail -foregroundcolor "Green" -nonewline
Write-host """"
}
###############################################################################
function exec
{
Param ($UserDisplayName,$UserEmail)
$UserDisplayName=$UserDisplayName.Trim()
log_message "OFFICE365" "$($UserDisplayName) ($($UserEmail))"
if (user_exists($UserEmail))
{
display_user $UserDisplayName $UserEmail
delete_user $UserDisplayName $UserEmail
}
else
{
$global:errors_count++
$S="User ""$($UserEmail)"" does not exists"
write-host $S -foregroundcolor "red"
log_message "ERROR" $S
}
log_errors_count($UserEmail)
$global:total_errors_count=$global:total_errors_count+$global:errors_count
}
###############################################################################
# #
# Main #
# #
###############################################################################
#
$global:Version="V3.92"
#
# Office 365 params
#
$Office365Url="
ps.outlook.com/powershell/
"
$Office365LoginName="adminOffice365@enc-nantes.fr"
$Office365LoginPassword=[System.Text.Encoding]::ASCII.GetString([System.Convert]::FromBase64String("SW5lc2NAbXB1cw=="))
#
# Proxy parameters
#
$ProxyEnabled=$false
$ProxyUser=""
$ProxyPassword=[System.Text.Encoding]::ASCII.GetString([System.Convert]::FromBase64String(""))
#
#
$LogFile=$MyInvocation.MyCommand.Definition
$LogFile=$LogFile -replace(".ps1",".log")
$global:total_errors_count=0
welcome
check_version
connect $Office365Url $Office365LoginName $Office365LoginPassword $ProxyEnabled $ProxyUser $ProxyPassword
if ($global:total_errors_count -eq 0)
{
### BEGIN_REPETITION ###
$global:errors_count=0
$UserDisplayName="Mme Julie BARJOLE"
$UserEmail="jbarjole@enc-nantes.fr"
exec $UserDisplayName $UserEmail
### END_REPETITION ###
close
}
log_total_errors_count