Using PowerShell Scripts with the Box CLI
Using PowerShell Scripts with the Box CLI
By itself, the CLI is already powerful, but using it alongside a PowerShell script lets you complete repetitive tasks even quicker. We created an example scripts folder within our CLI GitHub repository to help jump start your development.
For this step in the quick start, we are going to use the create users and provisioning automation script template.
This script uses the Box CLI to build and create a user-owned (admin or service account) onboarding folder structure, create managed users in bulk, and provision such new users by adding them to the newly created folder structure as collaborators with viewer or uploader roles.
The use case presented in this example is to illustrate the automatic creation of users:
- using a
.csvfile to load users in bulk
- have a predetermined folder structure associated to each user
- define that folder structure using a JSON file
- or, optionally, create the folder structure by uploading from a local drive
- Install the latest version of dotnet core.
- Install PowerShell
git clone https://github.com/box/boxcli.git box-cli cd box-cli/examples/User\ Creation\ \&\ Provisioning/
You must adapt this script to run in your own environment.
You can use the following sample files to load users:
Each will load 1, 5 or 10 new users.
Customize these files for a test run. For example, update the
Employees_1.csv with the following data:
Users_Create_Provision.ps1 script file, specify which
file you would like to load. For example,
#Set Employee List CSV Path $EmployeeList = "./Employees_1.csv"
You can either create a folder structure from a JSON file, or upload it from your local drive.
Folder_Structure.json file represents the folder structure you want to create.
As an example we're going to create a
Market Research and a
folder, each with a subfolder
Big Pharma respectively.
On the script file
Users_Create_Provision.ps1 you also have some options to
The folder creation section in the script has the folder
coded. This means that whatever folders are present
they will be created under the
#First create Onboarding folder owned by current user $script:OnboardingFolderId = box folders:create 0 "Onboarding" --id-only Write-Output "Created a user owned Onboarding folder with id: $($OnboardingFolderId)"
Set the location of the
#Onboarding Folder Structure: Set either path build off JSON or directly # upload a local folder $FolderStructureJSONPath = "./Folder_Structure.json" #$LocalUploadPath = "./OnboardingLocalUpload"
The script also shows the example of uploading a folder structure directly from the local file system. If you want to try that, set the path to your local folder:
#Onboarding Folder Structure: Set either path build off JSON or directly # upload a local folder #$FolderStructureJSONPath = "./Folder_Structure.json" $LocalUploadPath = "./OnboardingLocalUpload"
New-Folder-Structure call, and uncomment the next section:
#Create Folder Structure from JSON #New-Folder-Structure #OR directly upload Folder structure to current user's root folder # from local directory $script:OnboardingFolderId = box folders:upload $LocalUploadPath --id-only Write-Output "Uploaded local folder structre to current user's root folder with $($script:OnboardingFolderId)"
Now all you need to do is run the script.
Change the directory to the folder containing the script.
In this example, it is the
User Creation & Provisioning
rvb@lab:~/box-cli/examples/User Creation & Provisioning$ pwsh PowerShell 7.2.4 Copyright (c) Microsoft Corporation. https://aka.ms/powershell Type 'help' to get help. PS /home/rvb/box-cli/examples/User Creation & Provisioning>
Run the script:
PS /home/rvb/box-cli/examples/User Creation & Provisioning> ./Users_Create_Provision.ps1 Starting User Creation & Provisioning script... firstName lastName email --------- -------- ----- Isaac Newton firstname.lastname@example.org Extracting folder structure Found current User ID: 18622116055 Created a user owned Onboarding folder with id: 164734146745 Created subfolder Market Research under Onboarding folder with id: 164735375585 Created subfolder under Statistics folder with id: 164734956242 Created subfolder Sales Plays under Onboarding folder with id: 164735683001 Created subfolder under Big Pharma folder with id: 164736160637 Creating employee Managed User account with first name: Isaac, last name: Newton, email: email@example.com, and Created Managed user with id: 19605663027 Type: collaboration ID: '37250833128' Created By: Type: user ID: '18622116055' Name: Rui Barbosa Login: firstname.lastname@example.org Created At: '2022-06-07T13:58:05-07:00' Modified At: '2022-06-07T13:58:05-07:00' Expires At: null Status: accepted Accessible By: Type: user ID: '19605663027' Name: Isaac Newton Login: email@example.com Invite Email: null Role: viewer uploader Acknowledged At: '2022-06-07T13:58:05-07:00' Item: Type: folder ID: '164734146745' Sequence ID: '0' ETag: '0' Name: Onboarding Collaborated Managed User Isaac Newton to current user's Onboarding folder for provisioning
- You explored automation using a PowerShell script with the Box CLI to provision users and create their initial folder tree.