Hi all,
I’ve this code and it’s working just fine the only thing I want to be able to is to restart from the top if a user is still active when this code is executed
[pre]
#Connect-AzureAD
#read file with users (email address)
$users = import-csv c:\temp\csv\toRemove.csv
$ErrorLog = “c:\temp\groupdeletionErrors.txt”
foreach ($user in $users) {
#check if the user is disabled
$accountEnabled = Get-AzureADUser -ObjectId $user.Email
if(!($accountEnabled.AccountEnabled)) {
#get the users objectID from Azure
$UserObjectID =get-AzureAdUser -objectId $user.Email |select objectID
#grab the clean objectID from the user
$SelectUserObjectID= $userObjectID.objectID
#find all the groups a user is member off
$UserObjectIDGroupMemberShip = get-AzureAdUserMembership -objectID $SelectUserObjectID
$a= $userObjectIDGroupMemberShip.count
$Groupsremoved = 0
$ErrorGroup = 0
foreach ($group in $UserObjectIDGroupMemberShip) {
#remove the user from each indivudual group
try {
Remove-AzureADGroupMember -ObjectId $group.objectID -MemberId $SelectUserObjectID
$Groupsremoved++
}
catch {
#the groups that cannot be removed are safed in the error log
“Error removing $group : $($_)” |Add-content $ErrorLog
#write-host “$errorcount groups could not be removed from $($user.samaccountname)”
$Errorgroup++
}
finally {
#output on screen
}
}
write-host “#####################################################################”
write-host “result for $($user.Email)”
write-host “Total groups Found: $a”
Write-host “Total groups Removed: $Groupsremoved”
Write-host “Total groups not removed: $Errorgroup”
write-host “#####################################################################”
}
Else {
write-host “$($user.Email) cannot be removed at this time”
}
}
[/pre]
if the account is still active I’m correctly diverted into the Else part, is there a way that I can re-launch this code for a second attempt?
or do I simply need to call the script again by adding ./mypowershell.ps1 ?
thanks for your input in this matter