Maintain control of your Microsoft Office 365 email 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 » Feb 07, 2019 12:08 pm

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
Veeam Software
Posts: 2619
Liked: 539 times
Joined: Jul 15, 2013 11:09 am
Full Name: Niels Engelen
Contact:

Re: Remove users from job throug API

Post by nielsengelen » Feb 07, 2019 1:02 pm

Markus, just to be sure, if you're talking API do you mean the RESTful API or did you leverage Powershell?
VCP-DCV
Veeam Certified Architect (VMCA)
http://foonet.be

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 » Feb 07, 2019 1:18 pm

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 » Feb 07, 2019 3:07 pm

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
Veeam Software
Posts: 2619
Liked: 539 times
Joined: Jul 15, 2013 11:09 am
Full Name: Niels Engelen
Contact:

Re: Remove users from job throug API

Post by nielsengelen » Feb 08, 2019 2:56 pm

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.
VCP-DCV
Veeam Certified Architect (VMCA)
http://foonet.be

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 » Feb 11, 2019 1:27 pm

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
Veeam Software
Posts: 2619
Liked: 539 times
Joined: Jul 15, 2013 11:09 am
Full Name: Niels Engelen
Contact:

Re: Remove users from job throug API

Post by nielsengelen » Feb 11, 2019 3:52 pm 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)'."
    }
}
VCP-DCV
Veeam Certified Architect (VMCA)
http://foonet.be

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 » Feb 12, 2019 9:35 am

Thank you , that worked!

Post Reply

Who is online

Users browsing this forum: No registered users and 2 guests