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.
To show you how CLI scripts work, we are going to use the provision and create users script template.
This script uses the Box CLI to build and create an 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.
To automatically create users and folder structure, the script performs the following steps:
- Uses a
.csvfile to load employee data in bulk.
- Defines folder structure using a JSON file or uploads the structure from the user's local directory.
- Creates a managed user a predetermined folder structure to each user.
Install the latest version of dotnet core.
Clone the script to a directory of your choice and navigate to that directory.
git clone https://github.com/box/boxcli.git box-cli cd box-cli/examples/User\ Creation\ \&\ Provisioning/
Adapt the script to run in your own environment. In this example, you will use the sample data provided with the script.
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.
#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 contains the folder structure you want to create.
As an example you will create a
Market Research and a
folder, each with a subfolder
Big Pharma respectively.
The script will place this folder structure
#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)"
Provide 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"
You can also upload a folder structure directly from the local file system:
- 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"
- Comment the
New-Folder-Structurecall, 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)"
- Change the directory to the folder containing the script.
In this example, it is the
User Creation & Provisioningfolder.
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. /home/rvb/box-cli/examples/User Creation & Provisioning>
- Run the script:
PS /home/rvb/box-cli/examples/User Creation & Provisioning> ./Users_Create_Provision.ps1
The response will be similar to the following:
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 users Onboarding folder for provisioning
You explored automation using a PowerShell script with the Box CLI to provision users and create an initial folder structure.