Creates Knowledge Base (KB) folders for all companies in your Hudu instance. It includes an option to create standardized subfolders within each main KB folder.
Features:
Folder Creation: Automatically generates a main KB folder for each company, named according to the company name or a standardized name.
Subfolder Creation: Optionally creates a set of predefined subfolders within each main KB folder.
Duplicate Check: Includes logic to check for existing folders to avoid creating duplicates.
# Create-KBFolderWithSubfolders.ps1
# This script creates a main KB folder with optional subfolders for each company in your Hudu instance.
# It includes checks to prevent duplicate folders from being created.
# Usage: ./Create-KBFolderWithSubfolders.ps1
# -------------------------------------------------------------------------
# User Environment
# -------------------------------------------------------------------------
# Before starting, you'll need to set 3 variables, just below their explanations.
# 1. Set your Azure Key Vault name
# 2. Set the name of your secret (which holds Hudu API key)
# 3. Set the URL of your Hudu instance-
$AzVault_Name = "ENTER YOUR KEY VAULT NAME HERE"
$AzVault_HuduSecretName = "ENTER YOUR KEY VAULT SECRET NAME HERE"
$HuduBaseURL = "HTTPS://YOUR.HUDU.DOMAIN"
# -------------------------------------------------------------------------
# Init Modules and Sign-In
# -------------------------------------------------------------------------
foreach ($module in @('Az.KeyVault', 'HuduAPI')) {
if (Get-Module -ListAvailable -Name $module) {
Write-Host "Importing module, $module...";
Import-Module $module
} else {
Write-Host "Installing and importing module $module...";
Install-Module $module -Force -AllowClobber;
Import-Module $module
}
}
if (-not (Get-AzContext)) {
Connect-AzAccount };
New-HuduAPIKey "$(Get-AzKeyVaultSecret -VaultName "$AzVault_Name" -Name "$AzVault_HuduSecretName" -AsPlainText)"
New-HuduBaseUrl $HuduBaseURL
# -------------------------------------------------------------------------
# Begin Script Logic
# -------------------------------------------------------------------------
# Function to create a KB folder with optional subfolders for all companies
function Create-KBFolderWithSubfoldersForAllCompanies {
# Step 1: Retrieve All Companies
# The script begins by getting all companies in your Hudu instance.
# The $companies variable will hold the list of companies to process.
$companies = Get-HuduCompanies
# Step 2: Define Subfolders to Create
# Modify the array below to add more subfolders or change their names.
# Example: Add "Policies" and "Training" to create more subfolders.
# To exclude subfolders, you can comment out the subfolder loop below.
$subfolders = @("Documentation", "Processes", "Guides")
# Step 3: Loop Through Each Company
# The script loops through each company and creates a KB folder with subfolders.
foreach ($company in $companies) {
$companyId = $company.Id
$companyName = $company.Name
# Step 4: Customize Folder Name
# The $folderName variable sets the name of the main KB folder.
# By default, it uses the company name, but you can set a fixed name if desired.
# Example: $folderName = "Standard KB Folder" will create the same folder name for all companies.
$folderName = "$companyName KB Folder"
try {
# Step 5: Check for Existing Folder
# Before creating the main folder, the script checks if a folder with the same name already exists for this company.
# If the folder exists, the script skips folder creation for this company to avoid duplicates.
$existingFolder = Get-HuduFolders -CompanyId $companyId | Where-Object { $_.name -eq $folderName }
if ($existingFolder) {
Write-Host "Folder '$folderName' already exists for company '$companyName'. No new folder will be created." -ForegroundColor Yellow
continue # Skip to the next company if the folder already exists
}
# Step 6: Create the Main KB Folder
# If no existing folder is found, create the main KB folder for the company.
$parentFolder = New-HuduFolder -Name $folderName -CompanyId $companyId
# Step 7: Extract the Parent Folder ID
# The script retrieves the ID of the newly created parent folder for use in creating subfolders.
$parentFolderId = $parentFolder.folder.id
if ($parentFolderId) {
# Step 8: Create Subfolders (Optional)
# The script creates subfolders under the main KB folder.
# Comment out the following loop if you don't want to create subfolders.
foreach ($subfolder in $subfolders) {
New-HuduFolder -Name $subfolder -CompanyId $companyId -ParentFolderId $parentFolderId
Write-Host "Created subfolder '$subfolder' under '$folderName' for company '$companyName'" -ForegroundColor Green
}
} else {
Write-Host "Failed to retrieve the ParentFolderId for '$folderName'." -ForegroundColor Red
}
} catch {
# Step 9: Error Handling
# If there's an error during folder or subfolder creation, the script will output an error message.
Write-Host "Failed to create folder or subfolders for company '$companyName' (ID: $companyId)" -ForegroundColor Red
Write-Host "Error: $_" -ForegroundColor Red
}
}
}
# Step 10: Run the Function
# The script ends by running the function to create the KB folders with subfolders for all companies.
Create-KBFolderWithSubfoldersForAllCompanies
Remove-HuduAPIKey
Script Data
Language: PowerShell
Run As: Logged In User
Script Timeout Duration: 5 Mins
Prerequisites:
Valid Hudu API key with necessary permissions.
Customizable script parameters (folder names, subfolder names).