Maintain control of your Microsoft 365 data
Post Reply
whimark
Influencer
Posts: 18
Liked: 5 times
Joined: May 14, 2018 11:54 am
Full Name: Markus Johansson
Location: Gothenburg
Contact:

Remove users from job throug API

Post by whimark »

Hi,

I have managed to retrieve a list of users through the API that are not present in our organisation anymore, and im wondering how I can remove the users from the same jobs through the API?

//Markus
nielsengelen
Product Manager
Posts: 5797
Liked: 1215 times
Joined: Jul 15, 2013 11:09 am
Full Name: Niels Engelen
Contact:

Re: Remove users from job throug API

Post by nielsengelen »

Markus, just to be sure, if you're talking API do you mean the RESTful API or did you leverage Powershell?
Personal blog: https://foonet.be
GitHub: https://github.com/nielsengelen
whimark
Influencer
Posts: 18
Liked: 5 times
Joined: May 14, 2018 11:54 am
Full Name: Markus Johansson
Location: Gothenburg
Contact:

Re: Remove users from job throug API

Post by whimark »

Hi Niels,

I have managed to connected to the RESTful API with powershell.
whimark
Influencer
Posts: 18
Liked: 5 times
Joined: May 14, 2018 11:54 am
Full Name: Markus Johansson
Location: Gothenburg
Contact:

Re: Remove users from job throug API

Post by whimark »

Here is some parts of the code Im using to retrive the users that is not present in Office365 anymore that we get daily warnings for.

Code: Select all

$userstoremove = @()
foreach ($mailboxjob in $mailboxjobs)
{

$missingusers = @()

$currentusers = Invoke-RestMethod -Uri "$uri/jobs/$($mailboxjob.id)/selectedItems" -Method Get -Headers $headers
$sessions = Invoke-RestMethod -Uri "$uri/Jobs/$($mailboxjob.id)/JobSessions" -Method get -Headers $headers -ContentType "application/json" 
$lastsessions = $sessions | ? {$_.endTime -gt $date}

foreach ($lastestsession in $lastsessions)
{
$limit = $lastestsession.progress
$jobsession = Invoke-WebRequest -Uri "$uri/JobSessions/$($lastestsession.id)/logitems?offset=0&limit=$($limit)" -Method get -Headers $headers -ContentType "application/json" 
$missingusers += ($jobsession | ConvertFrom-Json).results | ? {$_.title -like "*User was not found*"}

}
$missingusers.Count
#$missingguids = $null
$missingguids = @()
$missingusers.title | Select-String '{{0,1}([0-9a-fA-F]){8}-([0-9a-fA-F]){4}-([0-9a-fA-F]){4}-([0-9a-fA-F]){4}-([0-9a-fA-F]){12}\}{0,1}' -AllMatches | ForEach-Object {$match = $_.matches; $missingguids += $match.value}
#$missingguids.Count

foreach ($missingguid in $missingguids)
{

$userstoberemoved = $currentusers | ? {$_.user.id -like "$missingguid*"} | select -expand user
$userstoremove += $userstoberemoved

}


}
$userstoremove
nielsengelen
Product Manager
Posts: 5797
Liked: 1215 times
Joined: Jul 15, 2013 11:09 am
Full Name: Niels Engelen
Contact:

Re: Remove users from job throug API

Post by nielsengelen »

Hi Markus, via RESTful, you can't remove users from a job. You would have to use the built-in PowerShell snap-in which has the option to remove items from a jo via Remove-VBOBackupItem.
Personal blog: https://foonet.be
GitHub: https://github.com/nielsengelen
whimark
Influencer
Posts: 18
Liked: 5 times
Joined: May 14, 2018 11:54 am
Full Name: Markus Johansson
Location: Gothenburg
Contact:

Re: Remove users from job throug API

Post by whimark »

Hi Niels,

The problem is that I cant remove them since I cant find them when running Get-VBOOrganizationUser, and then I cant create a job that automaticly remove old users.

//Markus
nielsengelen
Product Manager
Posts: 5797
Liked: 1215 times
Joined: Jul 15, 2013 11:09 am
Full Name: Niels Engelen
Contact:

Re: Remove users from job throug API

Post by nielsengelen » 1 person likes this post

Markus,

Try the following script (just replace the JobName). If you want to debug it first, put the Remove-VBOBackupItem in comments.

Code: Select all

Remove-VBOBackupItem -Job $job -BackupItem $RemoveUser -ErrorAction Stop
Script:

Code: Select all

Import-Module "C:\Program Files\Veeam\Backup365\Veeam.Archiver.PowerShell\Veeam.Archiver.PowerShell.psd1"

$JobName = "TEST"

$Job = Get-VBOJob -Name $JobName
$JobSession = Get-VBOJobSession -Job $Job -Last
$SelectedItems = $Job.SelectedItems
$SelectedUser = $Job.SelectedItems.User
$JobLog = $JobSession.Log

$Seperator = '\[Warning\].*(Exchange\saccount\swas\snot\sfound)'
$Warnings = $JobLog.Title -match "$Seperator"

foreach ($JobWarning in $Warnings) {
    $UID = $JobWarning.Split('(ID: ')
    $UID = $UID[-1].TrimEnd(')')

    $RemoveUser = $SelectedItems | Where-Object {$_.User.OfficeId -eq $UID}
    $DisplayName = $($SelectedUser | Where-Object {$_.OfficeId -eq $UID} | Select-Object -ExpandProperty DisplayName)
    
    try {
        Remove-VBOBackupItem -Job $job -BackupItem $RemoveUser -ErrorAction Stop
        Write-Host -ForegroundColor Green "User named '$DisplayName' with GUID '$UID' has been removed from backup job '$($Job.Name)'."
    } catch {
        Write-Host -ForegroundColor Red "User named '$DisplayName' with GUID '$UID' could not be removed from backup job '$($Job.Name)'."
    }
}
Personal blog: https://foonet.be
GitHub: https://github.com/nielsengelen
whimark
Influencer
Posts: 18
Liked: 5 times
Joined: May 14, 2018 11:54 am
Full Name: Markus Johansson
Location: Gothenburg
Contact:

Re: Remove users from job throug API

Post by whimark »

Thank you , that worked!
Brad.linch
VeeaMVP
Posts: 408
Liked: 153 times
Joined: Mar 15, 2018 6:25 pm
Full Name: Brad Linch
Contact:

Re: Remove users from job throug API

Post by Brad.linch »

@david.tosoff updated the script to include all objects.

Code: Select all

Import-Module "C:\Program Files\Veeam\Backup365\Veeam.Archiver.PowerShell\Veeam.Archiver.PowerShell.psd1"
 
$JobName = "Local Exchange"
 
$Job = Get-VBOJob -Name $JobName
$JobSession = Get-VBOJobSession -Job $Job -Last
$SelectedItems = $Job.SelectedItems
$SelectedUser = $Job.SelectedItems.User
$JobLog = $JobSession.Log
 
# EXAMPLE STRINGS:
#[Warning] Processing mailbox Irvin Sayers completed with warning: User was not found: Irvin Sayers (ID: 80245d93-2859-4a20-a386-4c222e73c765)
#[Warning] Processing archive mailbox Irvin Sayers completed with warning: User was not found: Irvin Sayers (ID: 80245d93-2859-4a20-a386-4c222e73c765)
#[Warning] Processing OneDrive Irvin Sayers completed with warning: User was not found: Irvin Sayers (ID: 80245d93-2859-4a20-a386-4c222e73c765)
#[Warning] Processing site Irvin Sayers () finished with warning: User was not found: Irvin Sayers (ID: 80245d93-2859-4a20-a386-4c222e73c765)
 
$RegexStr = '\[Warning\].*(?:mailbox|archive mailbox|OneDrive|site)\s(?<username>.*)\s(?:completed|\(\) finished) with warning\: User was not found.* \(ID:\s(?<id>.+)\)'
 
$WarningUIDs = @()
foreach ($lineTitle in $JobLog.Title) {
    $match = [RegEx]::Match($lineTitle, $RegexStr)
    if ($match.Success) { $WarningUIDs += $match.Groups["id"].Value }
}
 
foreach ($UID in ($WarningUIDs | group).Name) { 
    $RemoveUser = $SelectedItems | Where-Object {$_.User.OfficeId -eq $UID}
    $DisplayName = $($SelectedUser | Where-Object {$_.OfficeId -eq $UID} | Select-Object -ExpandProperty DisplayName)
 
    try {
        Remove-VBOBackupItem -Job $job -BackupItem $RemoveUser -ErrorAction Stop
        Write-Host -ForegroundColor Green "User named '$DisplayName' with GUID '$UID' has been removed from backup job '$($Job.Name)'."
    } catch {
        Write-Host -ForegroundColor Red "User named '$DisplayName' with GUID '$UID' could not be removed from backup job '$($Job.Name)'."
    }
}
Manager, Enterprise SEs
VMCA
http://Linchtips.com
Post Reply

Who is online

Users browsing this forum: No registered users and 9 guests