Maintain control of your Microsoft Office 365 email data
Post Reply
OmarN
Novice
Posts: 5
Liked: never
Joined: Sep 26, 2017 6:03 pm
Full Name: Omar Nova
Contact:

Adding mulitple users to jobs through powershell

Post by OmarN » Aug 23, 2018 1:32 am

Working on creating new jobs and adding users to current jobs through powershell. I'm using the script below to add single users but would like to see if there is a way to add multiple users from a csv or txt file.


$org = Get-VBOOrganization -Name "xyz.onmicrosoft.com"
$user = Get-VBOOrganizationUser -Organization $org -Name "Username"
$backupItemUser = New-VBOBackupItem -User $user -Mailbox:$True -ArchiveMailbox:$true -OneDrive:$false -Sites:$false
$job = Get-VBOJob -Name "yourjobgoeshere"
Add-VBOBackupItem -Job $job -BackupItem $backupItemUser

Polina
Veeam Software
Posts: 945
Liked: 148 times
Joined: Oct 21, 2011 11:22 am
Full Name: Polina Vasileva
Contact:

Re: Adding mulitple users to jobs through powershell

Post by Polina » Aug 23, 2018 2:54 pm

Hi Omar,

I reached out to our RnD team to get the answer, and here's their suggestion:

Code: Select all

$org=Get-VBOOrganization 
$usernames=Get-Content -Path C:\Folder\Your Users.txt
$Users=Get-VBOOrganizationUser -Organization $org | ?{$usernames.Contains($_.UserName)}
$bi=New-VBOBackupItem -User $users -Mailbox:$True -ArchiveMailbox:$true -OneDrive:$false -Sites:$false
Add-VBOJob -Organization $org -Name Job1 -Repository (Get-VBORepository) -SelectedItems $bi
Where $bi stands for a backup items scope. Hope this helps.

Great question and thanks for asking!

OmarN
Novice
Posts: 5
Liked: never
Joined: Sep 26, 2017 6:03 pm
Full Name: Omar Nova
Contact:

Re: Adding mulitple users to jobs through powershell

Post by OmarN » Aug 23, 2018 4:01 pm

Thanks, for the reply. I tried the code provided but i receive this error message. i'm adding the names on the list with last name, first name...not sure if that is causing the issue but this is how we look up the users when adding them through the gui.

New-VBOBackupItem : Cannot bind argument to parameter 'User' because it is null.
At line:4 char:29
+ $bi=New-VBOBackupItem -User $users -Mailbox:$True -ArchiveMailbox:$tr ...
+ ~~~~~~
+ CategoryInfo : InvalidData: (:) [New-VBOBackupItem], ParameterBindingValidationException
+ FullyQualifiedErrorId : ParameterArgumentValidationErrorNullNotAllowed,Veeam.Archiver.PowerShell.Cmdlets.BackupItems.NewVBO
BackupItem

Add-VBOJob : Cannot convert 'System.Object[]' to the type 'Veeam.Archiver.PowerShell.Model.VBORepository' required by parameter
'Repository'. Specified method is not supported.
At line:5 char:68
+ ... $org -Name "user mailboxes 2" -Repository (Get-VBORepository) -Select ...
+ ~~~~~~~~~~~~~~~~~~~
+ CategoryInfo : InvalidArgument: (:) [Add-VBOJob], ParameterBindingException
+ FullyQualifiedErrorId : CannotConvertArgument,Veeam.Archiver.PowerShell.Cmdlets.Jobs.AddVBOJob

Polina
Veeam Software
Posts: 945
Liked: 148 times
Joined: Oct 21, 2011 11:22 am
Full Name: Polina Vasileva
Contact:

Re: Adding mulitple users to jobs through powershell

Post by Polina » Aug 28, 2018 3:03 pm

Omar,

My apologies for the delayed response.
I just tried the provided script in my lab and it works fine. The names were listed in .txt in the following way:
username1@organization.onmicrosoft.com
username2@organization.onmicrosoft.com
username3@organization.onmicrosoft.com

OmarN
Novice
Posts: 5
Liked: never
Joined: Sep 26, 2017 6:03 pm
Full Name: Omar Nova
Contact:

Re: Adding mulitple users to jobs through powershell

Post by OmarN » Sep 25, 2018 9:21 pm

Thanks for the Reply, after a few trial and errors here is what worked...for the usernames i also used the email address, not sure why in my environment it didn't work the way you recommended but we have something that works now and will facilitate creating our jobs more easier. Hope this helps others down the road.


$org=Get-VBOOrganization -Name "YourOrggoeshere"
$usernames=Get-Content -Path C:\migration\mailboxes.txt
$Users=Get-VBOOrganizationUser -Organization $org | ?{$usernames.Contains($_.UserName)}
$job=get-vbojob -name "Jobnamegoeshere"
$bi=New-VBOBackupItem -User $users -Mailbox:$True -ArchiveMailbox:$true -OneDrive:$false -Sites:$false
Add-vbobackupitem -job $job -backupitem $bi

habibalby
Expert
Posts: 379
Liked: 24 times
Joined: Jul 18, 2011 9:30 am
Full Name: Hussain Al Sayed
Location: Bahrain
Contact:

[MERGED] PowerShell: Add-VBOJob & New-VBOBackupItem from CSV File

Post by habibalby » Nov 22, 2018 6:17 am

Hi Guys,

I'm trying to achieve the following;
1.List all users in a CSV file.
2.Create a Backup Job and import the selected users from a csv;

Code: Select all

$org=Get-VBOOrganization 
$Usernames = import-csv -path C:\Scripts\UserList.csv
ForEach($User in $Usernames)

{

$User.Mailbox
Get-VBOOrganizationUser -Name $User.Mailbox -Organization $Org
$bi=New-VBOBackupItem -User $User.Mailbox -Mailbox:$True -ArchiveMailbox:$true -OneDrive:$true -Sites:$true
Add-VBOJob -Organization $org -Name Job99 -Repository (Get-VBORepository) -SelectedItems $bi

}

But i'm always getting error message;

New-VBOBackupItem : Cannot bind parameter 'User'. Cannot convert the "username"...

Any help.. ?

Regards,

Polina
Veeam Software
Posts: 945
Liked: 148 times
Joined: Oct 21, 2011 11:22 am
Full Name: Polina Vasileva
Contact:

Re: PowerShell: Add-VBOJob & New-VBOBackupItem from CSV File

Post by Polina » Nov 22, 2018 6:50 am 1 person likes this post

Hi Hussain,

Kindly review the above posts in this thread to get some hints on how to fix your script. Hope they will help you.

Thanks!

habibalby
Expert
Posts: 379
Liked: 24 times
Joined: Jul 18, 2011 9:30 am
Full Name: Hussain Al Sayed
Location: Bahrain
Contact:

Re: Adding mulitple users to jobs through powershell

Post by habibalby » Nov 22, 2018 7:00 am

Hi Polina,

Thanks for your reference, but this script it doesn;t exit out of the Foreach loop at all..:) Unless I kill the entire console and Application.. The .txt file should contains an header "UserName" isn't ?

Regards,

habibalby
Expert
Posts: 379
Liked: 24 times
Joined: Jul 18, 2011 9:30 am
Full Name: Hussain Al Sayed
Location: Bahrain
Contact:

Re: Adding mulitple users to jobs through powershell

Post by habibalby » Nov 22, 2018 10:17 am

Hi there,
this won't work;

Code: Select all

$Org = Get-VBOOrganization
$Usernames = Import-csv -Path "C:\Scripts\UserList.csv"
$Users=Get-VBOOrganizationUser -Organization $org | ?{$usernames.Contains($_.UserName)}
$bi=New-VBOBackupItem -User $users -Mailbox:$True -ArchiveMailbox:$true -OneDrive:$false -Sites:$false
Add-VBOJob -Organization $org -Name JobBmA -Repository (Get-VBORepository) -SelectedItems $bi
The CSV file contains header Username and the username format is username@domain.com;

Any help...?

Regards,

Polina
Veeam Software
Posts: 945
Liked: 148 times
Joined: Oct 21, 2011 11:22 am
Full Name: Polina Vasileva
Contact:

Re: Adding mulitple users to jobs through powershell

Post by Polina » Nov 22, 2018 9:35 pm

Hi Hussain,

Try using "Get-Content -Path "C:\Scripts\UserList.csv" instead of "Import-csv" in the above script, and kill a header in your csv leaving just usernames in the username@domain.com format.

Btw, in your initial script, one of the ussies was that it tried to create a job with the same "Job99" name for each of your users, which couldn't work. If you added an index parameter "$i=0" and correspondingly changed job name to "-Name "Job$i", a separate job would be created for each user.

habibalby
Expert
Posts: 379
Liked: 24 times
Joined: Jul 18, 2011 9:30 am
Full Name: Hussain Al Sayed
Location: Bahrain
Contact:

Re: Adding mulitple users to jobs through powershell

Post by habibalby » Nov 23, 2018 5:50 am

Hi Poline,

I've tried that and still didn't work. Any help..?

Regards,

Polina
Veeam Software
Posts: 945
Liked: 148 times
Joined: Oct 21, 2011 11:22 am
Full Name: Polina Vasileva
Contact:

Re: Adding mulitple users to jobs through powershell

Post by Polina » Nov 23, 2018 9:53 am

Hussain,

Which error(s) did you get?

habibalby
Expert
Posts: 379
Liked: 24 times
Joined: Jul 18, 2011 9:30 am
Full Name: Hussain Al Sayed
Location: Bahrain
Contact:

Re: Adding mulitple users to jobs through powershell

Post by habibalby » Nov 23, 2018 10:07 am

Hi Polina,
Here's the error;

Code: Select all


PS C:\Scripts> .\Add-VBROJob-Mailbox.ps1
Get-VBOOrganizationUser : Connecting to remote server outlook.office365.com failed with the following error message :
Access is denied. For more information, see the about_Remote_Troubleshooting Help topic.
At C:\Scripts\Add-VBROJob-Mailbox.ps1:51 char:10
+ $users = Get-VBOOrganizationUser -Name $usernames  -Organization $Org
+          ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

habibalby
Expert
Posts: 379
Liked: 24 times
Joined: Jul 18, 2011 9:30 am
Full Name: Hussain Al Sayed
Location: Bahrain
Contact:

Re: Adding mulitple users to jobs through powershell

Post by habibalby » Nov 23, 2018 10:15 am

Hi Polina,
Here;s the original error, the ignore the above error as it indicates to username/password error accessing the Tenant Organization.

Code: Select all

PS C:\Scripts> .\Add-VBROJob-Mailbox.ps1
New-VBOBackupItem : Cannot bind parameter 'User'. Cannot convert the "jdagami@domain.com " value of type "System.String"
to type "Veeam.Archiver.PowerShell.Model.VBOOrganizationUser".
At C:\Scripts\Add-VBROJob-Mailbox.ps1:27 char:29
+ $bi=New-VBOBackupItem -User $User -Mailbox:$True -ArchiveMailbox:$true -OneDrive ...
+                             ~~~~~
    + CategoryInfo          : InvalidArgument: (:) [New-VBOBackupItem], ParameterBindingException
    + FullyQualifiedErrorId : CannotConvertArgumentNoMessage,Veeam.Archiver.PowerShell.Cmdlets.BackupItems.NewVBOBacku
   pItem

Add-VBOJob : Cannot validate argument on parameter 'SelectedItems'. The argument is null or empty. Provide an argument
that is not null or empty, and then try the command again.
At C:\Scripts\Add-VBROJob-Mailbox.ps1:28 char:90
+ ... -SelectedItems $bi
+                    ~~~
    + CategoryInfo          : InvalidData: (:) [Add-VBOJob], ParameterBindingValidationException
    + FullyQualifiedErrorId : ParameterArgumentValidationError,Veeam.Archiver.PowerShell.Cmdlets.Jobs.AddVBOJob

New-VBOBackupItem : Cannot bind argument to parameter 'User' because it is null.
At C:\Scripts\Add-VBROJob-Mailbox.ps1:54 char:29
+ $bi=New-VBOBackupItem -User $users -Mailbox:$True -ArchiveMailbox:$true -OneDriv ...
+                             ~~~~~~
    + CategoryInfo          : InvalidData: (:) [New-VBOBackupItem], ParameterBindingValidationException
    + FullyQualifiedErrorId : ParameterArgumentValidationErrorNullNotAllowed,Veeam.Archiver.PowerShell.Cmdlets.BackupI
   tems.NewVBOBackupItem

Add-VBOJob : Cannot validate argument on parameter 'SelectedItems'. The argument is null or empty. Provide an argument
that is not null or empty, and then try the command again.
At C:\Scripts\Add-VBROJob-Mailbox.ps1:55 char:92
+ ... -SelectedItems $bi
+                    ~~~
    + CategoryInfo          : InvalidData: (:) [Add-VBOJob], ParameterBindingValidationException
    + FullyQualifiedErrorId : ParameterArgumentValidationError,Veeam.Archiver.PowerShell.Cmdlets.Jobs.AddVBOJob

habibalby
Expert
Posts: 379
Liked: 24 times
Joined: Jul 18, 2011 9:30 am
Full Name: Hussain Al Sayed
Location: Bahrain
Contact:

Re: Adding mulitple users to jobs through powershell

Post by habibalby » Nov 23, 2018 10:17 am

And the code i'm using is this;

Code: Select all

$org=Get-VBOOrganization 
$usernames = Get-Content -Path C:\Scripts\Users.txt
ForEach($User in $Usernames)

{
Get-VBOOrganizationUser -Name $User -Organization $org
$bi=New-VBOBackupItem -User $User -Mailbox:$True -ArchiveMailbox:$true -OneDrive:$true -Sites:$true
Add-VBOJob -Organization $org -Name Job99 -Repository (Get-VBORepository) -SelectedItems $bi
}



Polina
Veeam Software
Posts: 945
Liked: 148 times
Joined: Oct 21, 2011 11:22 am
Full Name: Polina Vasileva
Contact:

Re: Adding mulitple users to jobs through powershell

Post by Polina » Nov 23, 2018 11:50 am 1 person likes this post

So, let's fix the above script a bit and make it work:

1. The following line is missing: $Users=Get-VBOOrganizationUser -Organization $org| ?{$usernames.Contains($_.UserName)}
2. Need to add index parameter to set a unique name for each job (as with ForEach you'll get a separate backup job for each user)
3. Modify ForEach the following way: ForEach($User in $Users)

This way, you should get the following script:

Code: Select all

$org=Get-VBOOrganization 
$usernames = Get-Content -Path C:\Scripts\Users.txt
$Users=Get-VBOOrganizationUser -Organization $org| ?{$usernames.Contains($_.UserName)}
$i=0
ForEach($User in $Users)
{
Get-VBOOrganizationUser -Name $User -Organization $org
$bi=New-VBOBackupItem -User $User -Mailbox:$True -ArchiveMailbox:$true -OneDrive:$true -Sites:$true
Add-VBOJob -Organization $org -Name Job99$i -Repository (Get-VBORepository) -SelectedItems $bi
$i++
}

Thanks!

whimark
Influencer
Posts: 18
Liked: 5 times
Joined: May 14, 2018 11:54 am
Full Name: Markus Johansson
Location: Gothenburg
Contact:

Re: Adding mulitple users to jobs through powershell

Post by whimark » Nov 23, 2018 12:01 pm 1 person likes this post

I think it will would work better with the array ($foo)

Code: Select all

$org=Get-VBOOrganization 
$usernames = Get-Content -Path C:\Scripts\Users.txt
$Users=Get-VBOOrganizationUser -Organization $org| ?{$usernames.Contains($_.UserName)}
$i=0
$foo = @()
ForEach($User in $Users)
{
Get-VBOOrganizationUser -Name $User -Organization $org
$bi=New-VBOBackupItem -User $User -Mailbox:$True -ArchiveMailbox:$true -OneDrive:$true -Sites:$true
$foo += $bi 
$i++
}

Add-VBOJob -Organization $org -Name Job99$i -Repository (Get-VBORepository) -SelectedItems $foo

habibalby
Expert
Posts: 379
Liked: 24 times
Joined: Jul 18, 2011 9:30 am
Full Name: Hussain Al Sayed
Location: Bahrain
Contact:

Re: Adding mulitple users to jobs through powershell

Post by habibalby » Nov 23, 2018 7:54 pm

Hi Whimark,

Thank you very much, this has worked... but it looks like its a bit slow and doesn't exit the command unless I hit Enter to quit it.. At least it does the job. much appreciated your effort and taking the time to write it up..

Regards,

whimark
Influencer
Posts: 18
Liked: 5 times
Joined: May 14, 2018 11:54 am
Full Name: Markus Johansson
Location: Gothenburg
Contact:

Re: Adding mulitple users to jobs through powershell

Post by whimark » Nov 26, 2018 2:58 pm 1 person likes this post

Yes, the reason for that the script is so slow is beacuse "Get-VBOOrganizationUser -Name $User -Organization $org" I present in the foreach, which I didnt noticed when I added the array. If that where removed I think it would have run faster for you. Strange that you needed to hit enter to quit

habibalby
Expert
Posts: 379
Liked: 24 times
Joined: Jul 18, 2011 9:30 am
Full Name: Hussain Al Sayed
Location: Bahrain
Contact:

Re: Adding mulitple users to jobs through powershell

Post by habibalby » Nov 26, 2018 3:24 pm

Thank you very much.. I will try to present it within the ForEach and let's see if that will speed it up.

Regards,

habibalby
Expert
Posts: 379
Liked: 24 times
Joined: Jul 18, 2011 9:30 am
Full Name: Hussain Al Sayed
Location: Bahrain
Contact:

Re: Adding mulitple users to jobs through powershell

Post by habibalby » Dec 04, 2018 5:22 am

Hi Whimark,

I tried to remove the pipe line between the VBOOrganizationUser, but that didn't help.

@Veeam {PowerShell scripting should make our life easier rather than waiting hell of time to get one job to exist the PowerShell Console}

Regards,

whimark
Influencer
Posts: 18
Liked: 5 times
Joined: May 14, 2018 11:54 am
Full Name: Markus Johansson
Location: Gothenburg
Contact:

Re: Adding mulitple users to jobs through powershell

Post by whimark » Dec 04, 2018 8:51 am 1 person likes this post

Hi Habibalby,

Can you send me a version of your CSV file?

habibalby
Expert
Posts: 379
Liked: 24 times
Joined: Jul 18, 2011 9:30 am
Full Name: Hussain Al Sayed
Location: Bahrain
Contact:

Re: Adding mulitple users to jobs through powershell

Post by habibalby » Dec 04, 2018 8:56 am

hi Markus,

its simple .txt file that includes email only and same format as Get-VBOOrganizationUser;

OrganizationId : xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx
OfficeId : xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx
OnPremisesId : 00000000-0000-0000-0000-000000000000
DisplayName : xxxxxxxxxxxxxxx
UserName : xxxxxxxxxxxx (The txt file is populated with the same format..)
Type : User
IsBackedUp : True

whimark
Influencer
Posts: 18
Liked: 5 times
Joined: May 14, 2018 11:54 am
Full Name: Markus Johansson
Location: Gothenburg
Contact:

Re: Adding mulitple users to jobs through powershell

Post by whimark » Dec 04, 2018 9:29 am 1 person likes this post

Hi Hussain

I think this should work for you. Just update the "job99" to the correct repository and job name.

If you want to add users to a current job remove the bracket in the foreach and and a bracket to add-vbojob (#Add-VBOJob -Organization $org -Name $name -Repository $repository -SelectedItems $foo)

Code: Select all

$org=Get-VBOOrganization 
$repository = Get-VBORepository | where name -eq "Job99"
$name = "Job99"
$Usernames = import-csv -path C:\Scripts\UserList.csv
$orgusers = Get-VBOOrganizationUser -Organization $org
$users = compare $Usernames $orgusers.username -IncludeEqual | where sideindicator -eq "==" | select -ExpandProperty InputObject
$i=0
$foo = @()
ForEach($User in $Users)
{
$newbackupuser = $orgusers | where where {$_.username -like $user}
$bi=New-VBOBackupItem -User $newbackupuser -Mailbox:$True -ArchiveMailbox:$true -OneDrive:$true -Sites:$true
#Add-VBOBackupItem -Job $name -BackupItem $bi
$foo += $bi 
$i++
}

Add-VBOJob -Organization $org -Name $name -Repository $repository -SelectedItems $foo

Polina
Veeam Software
Posts: 945
Liked: 148 times
Joined: Oct 21, 2011 11:22 am
Full Name: Polina Vasileva
Contact:

Re: Adding mulitple users to jobs through powershell

Post by Polina » Dec 05, 2018 8:30 am

Markus,

Thank you for your input on the matter, much appreciated!
I'll use the updated script for my lab as well )

habibalby
Expert
Posts: 379
Liked: 24 times
Joined: Jul 18, 2011 9:30 am
Full Name: Hussain Al Sayed
Location: Bahrain
Contact:

Re: Adding mulitple users to jobs through powershell

Post by habibalby » Dec 05, 2018 12:38 pm

HI Markus,

The updated script didn't work...:) I managed to create all the jobs with the previous script. Much appreciated your effort on making this a really and optimum solution.

Regards,

Post Reply

Who is online

Users browsing this forum: No registered users and 1 guest