Cesivi PowerShell Examples¶
This document provides practical examples for using Cesivi PowerShell cmdlets.
Table of Contents¶
Getting Started¶
Installation and Setup¶
# Build the module
cd Cesivi.PowerShell
dotnet build
# Import the module
Import-Module .\bin\Debug\net10.0\Cesivi.PowerShell.dll
# Verify installation
Get-Command -Module Cesivi.PowerShell
# Set default server URL (optional)
$env:CESIVI_SERVER_URL = "http://localhost:5000"
Start Cesivi Server¶
# In a separate terminal, start the server
cd Cesivi.Server
dotnet run
Site Management¶
List All Site Collections¶
# Get all site collections
$sites = Get-CSSite -All
$sites | Format-Table Url, Title, Owner
# Filter sites
$sites | Where-Object { $_.Title -like "*Team*" }
# Count sites
$sites.Count
Get a Specific Site Collection¶
# Get site by identity
$site = Get-CSSite -Identity "/sites/team"
$site
# Display site properties
$site | Format-List Url, Title, Description, Owner, Created
# Check if site exists
try {
$site = Get-CSSite -Identity "/sites/nonexistent" -ErrorAction Stop
Write-Host "Site exists"
} catch {
Write-Host "Site not found"
}
Create a New Site Collection¶
# Basic site creation
$newSite = New-CSSite -Url "/sites/project" `
-Title "Project Site" `
-OwnerLogin "admin"
# Site with all parameters
$newSite = New-CSSite -Url "/sites/hr" `
-Title "HR Department" `
-OwnerLogin "admin" `
-Description "Human Resources portal" `
-Template "STS#0"
# Create multiple sites
$siteDefinitions = @(
@{ Url="/sites/hr"; Title="HR"; Owner="admin" }
@{ Url="/sites/it"; Title="IT"; Owner="admin" }
@{ Url="/sites/finance"; Title="Finance"; Owner="admin" }
)
foreach ($def in $siteDefinitions) {
New-CSSite -Url $def.Url -Title $def.Title -OwnerLogin $def.Owner
}
Update Site Properties¶
# Update site title
Set-CSSite -Identity "/sites/team" -Title "Updated Team Site"
# Update title and description
Set-CSSite -Identity "/sites/project" `
-Title "Active Projects" `
-Description "All active company projects"
# Bulk update sites
Get-CSSite -All | Where-Object { $_.Description -eq $null } |
ForEach-Object {
Set-CSSite -Identity $_.Url -Description "Company site"
}
Delete Site Collections¶
# Delete with confirmation
Remove-CSSite -Identity "/sites/oldsite"
# Delete without confirmation
Remove-CSSite -Identity "/sites/test" -Confirm:$false
# Delete multiple sites
$testSites = Get-CSSite -All | Where-Object { $_.Url -like "*/test*" }
$testSites | ForEach-Object {
Write-Host "Deleting: $($_.Url)"
Remove-CSSite -Identity $_.Url -Confirm:$false
}
# Safe deletion with backup check
$siteToDelete = Get-CSSite -Identity "/sites/important"
if ($siteToDelete) {
Write-Host "WARNING: About to delete $($siteToDelete.Url)"
$response = Read-Host "Type 'DELETE' to confirm"
if ($response -eq "DELETE") {
Remove-CSSite -Identity $siteToDelete.Url -Confirm:$false
}
}
Web Management¶
List All Webs¶
# Get all webs
$webs = Get-CSWeb -All
$webs | Format-Table Url, Title, ParentWebUrl
# Filter webs by parent
$webs | Where-Object { $_.ParentWebUrl -eq "/sites/intranet" }
# Group webs by site
$webs | Group-Object { $_.Url.Split('/')[0..2] -join '/' }
Get a Specific Web¶
# Get web by identity
$web = Get-CSWeb -Identity "/sites/team"
$web
# Display web properties
$web | Format-List Url, Title, Description, Created
# Get web with error handling
try {
$web = Get-CSWeb -Identity "/sites/team/subweb" -ErrorAction Stop
Write-Host "Web found: $($web.Title)"
} catch {
Write-Host "Web not found"
}
Create New Webs¶
# Create a subweb
$newWeb = New-CSWeb -Url "projects" `
-Title "Projects" `
-ParentWeb "/sites/team"
# Create web with description
$newWeb = New-CSWeb -Url "hr" `
-Title "HR Portal" `
-ParentWeb "/sites/intranet" `
-Description "Human Resources" `
-Template "STS#0"
# Create web hierarchy
$parentWeb = "/sites/company"
# Level 1
New-CSWeb -Url "departments" -Title "Departments" -ParentWeb $parentWeb
# Level 2
$deptWeb = "$parentWeb/departments"
New-CSWeb -Url "hr" -Title "HR" -ParentWeb $deptWeb
New-CSWeb -Url "it" -Title "IT" -ParentWeb $deptWeb
New-CSWeb -Url "finance" -Title "Finance" -ParentWeb $deptWeb
# Level 3
New-CSWeb -Url "recruiting" -Title "Recruiting" -ParentWeb "$deptWeb/hr"
New-CSWeb -Url "payroll" -Title "Payroll" -ParentWeb "$deptWeb/hr"
Update Web Properties¶
# Update web title
Set-CSWeb -Identity "/sites/team/projects" -Title "Active Projects"
# Update title and description
Set-CSWeb -Identity "/sites/intranet/hr" `
-Title "HR Department" `
-Description "Human Resources portal"
# Bulk update webs
Get-CSWeb -All | Where-Object { $_.Title -like "*Department*" } |
ForEach-Object {
Set-CSWeb -Identity $_.Url `
-Description "Organizational unit - $($_.Title)"
}
Delete Webs¶
# Delete with confirmation
Remove-CSWeb -Identity "/sites/team/oldproject"
# Delete without confirmation
Remove-CSWeb -Identity "/sites/team/test" -Confirm:$false
# Delete all test webs
Get-CSWeb -All | Where-Object { $_.Url -like "*/test*" } |
ForEach-Object {
Write-Host "Deleting: $($_.Url)"
Remove-CSWeb -Identity $_.Url -Confirm:$false
}
Advanced Scenarios¶
Scenario 1: Complete Site Provisioning¶
# Provision a complete site structure
# 1. Create site collection
$site = New-CSSite -Url "/sites/acme" `
-Title "ACME Corporation" `
-OwnerLogin "admin" `
-Description "Company intranet"
Write-Host "Created site: $($site.Url)"
# 2. Create department webs
$departments = @("HR", "IT", "Finance", "Marketing", "Sales")
foreach ($dept in $departments) {
$deptUrl = $dept.ToLower()
$web = New-CSWeb -Url $deptUrl `
-Title "$dept Department" `
-ParentWeb "/sites/acme" `
-Description "$dept organizational unit"
Write-Host " Created web: $($web.Url)"
}
# 3. Verify structure
Write-Host "`nSite structure:"
Get-CSWeb -All | Where-Object { $_.Url -like "/sites/acme*" } |
Format-Table Url, Title, Description
Scenario 2: Site Inventory Report¶
# Generate comprehensive site inventory
$report = @()
# Get all sites
$sites = Get-CSSite -All
foreach ($site in $sites) {
# Get webs in this site
$webs = Get-CSWeb -All | Where-Object { $_.Url -like "$($site.Url)*" }
$report += [PSCustomObject]@{
SiteUrl = $site.Url
SiteTitle = $site.Title
Owner = $site.Owner
WebCount = $webs.Count
Created = $site.Created
}
}
# Display report
$report | Format-Table -AutoSize
# Export to CSV
$report | Export-Csv -Path "site-inventory.csv" -NoTypeInformation
Write-Host "Report exported to site-inventory.csv"
Scenario 3: Bulk Site Creation from CSV¶
# Create sites from CSV file
# CSV format: Url,Title,Owner,Description
# Example row: /sites/team1,Team 1 Site,admin,Collaboration site
$csv = Import-Csv -Path "sites.csv"
foreach ($row in $csv) {
Write-Host "Creating site: $($row.Url)"
try {
$site = New-CSSite -Url $row.Url `
-Title $row.Title `
-OwnerLogin $row.Owner `
-Description $row.Description `
-ErrorAction Stop
Write-Host " ✓ Success" -ForegroundColor Green
}
catch {
Write-Host " ✗ Failed: $($_.Exception.Message)" -ForegroundColor Red
}
}
Scenario 4: Site Cleanup by Age¶
# Clean up old sites (BE CAREFUL!)
$cutoffDate = (Get-Date).AddMonths(-6)
Write-Host "Finding sites older than $cutoffDate"
$oldSites = Get-CSSite -All | Where-Object {
$_.Created -lt $cutoffDate -and
$_.Url -notlike "*/production*" # Protect production sites
}
Write-Host "Found $($oldSites.Count) sites to review:"
$oldSites | Format-Table Url, Title, Created
$response = Read-Host "Delete these sites? (yes/no)"
if ($response -eq "yes") {
foreach ($site in $oldSites) {
Write-Host "Deleting: $($site.Url)"
Remove-CSSite -Identity $site.Url -Confirm:$false
}
Write-Host "Cleanup complete"
} else {
Write-Host "Cleanup cancelled"
}
Scenario 5: Web Hierarchy Visualization¶
# Display web hierarchy as a tree
function Show-WebTree {
param(
[string]$ParentUrl = "",
[int]$Level = 0
)
$webs = Get-CSWeb -All | Where-Object {
if ($Level -eq 0) {
# Root level sites
$_.ParentWebUrl -eq $null -or $_.ParentWebUrl -eq ""
} else {
# Child webs
$_.ParentWebUrl -eq $ParentUrl
}
} | Sort-Object Url
foreach ($web in $webs) {
$indent = " " * $Level
Write-Host "$indent└─ $($web.Title) ($($web.Url))"
# Recursively show children
Show-WebTree -ParentUrl $web.Url -Level ($Level + 1)
}
}
Write-Host "Web Hierarchy:"
Write-Host "==============="
Show-WebTree
Script Templates¶
Template 1: Site Management Script¶
# Site management template
param(
[Parameter(Mandatory=$true)]
[ValidateSet("Create", "Update", "Delete", "List")]
[string]$Action,
[string]$SiteUrl,
[string]$Title,
[string]$Owner = "admin"
)
# Import module
Import-Module Cesivi.PowerShell
# Set server URL
$env:CESIVI_SERVER_URL = "http://localhost:5000"
switch ($Action) {
"Create" {
if (-not $SiteUrl -or -not $Title) {
throw "SiteUrl and Title required for Create action"
}
New-CSSite -Url $SiteUrl -Title $Title -OwnerLogin $Owner
}
"Update" {
if (-not $SiteUrl -or -not $Title) {
throw "SiteUrl and Title required for Update action"
}
Set-CSSite -Identity $SiteUrl -Title $Title
}
"Delete" {
if (-not $SiteUrl) {
throw "SiteUrl required for Delete action"
}
Remove-CSSite -Identity $SiteUrl -Confirm:$false
}
"List" {
Get-CSSite -All | Format-Table Url, Title, Owner
}
}
Template 2: Automated Provisioning¶
# Automated site provisioning
# Configuration
$config = @{
ServerUrl = "http://localhost:5000"
Sites = @(
@{
Url = "/sites/intranet"
Title = "Company Intranet"
Owner = "admin"
Webs = @("news", "events", "resources")
},
@{
Url = "/sites/projects"
Title = "Project Management"
Owner = "admin"
Webs = @("active", "archived", "templates")
}
)
}
# Import module
Import-Module Cesivi.PowerShell
$env:CESIVI_SERVER_URL = $config.ServerUrl
# Provision sites
foreach ($siteConfig in $config.Sites) {
Write-Host "Creating site: $($siteConfig.Title)"
# Create site
$site = New-CSSite -Url $siteConfig.Url `
-Title $siteConfig.Title `
-OwnerLogin $siteConfig.Owner
# Create webs
foreach ($webName in $siteConfig.Webs) {
Write-Host " Creating web: $webName"
New-CSWeb -Url $webName `
-Title $webName `
-ParentWeb $siteConfig.Url
}
}
Write-Host "Provisioning complete!"
Tips and Best Practices¶
1. Error Handling¶
Always use try-catch for robust scripts:
try {
$site = Get-CSSite -Identity "/sites/example" -ErrorAction Stop
# Process site
}
catch {
Write-Error "Failed to get site: $($_.Exception.Message)"
# Handle error
}
2. Server URL Configuration¶
Set default server URL to avoid repeating it:
# Set once at script start
$env:CESIVI_SERVER_URL = "http://cesivi.example.com"
# Then use cmdlets without -ServerUrl
Get-CSSite -All
3. Pipeline Usage¶
Leverage PowerShell pipeline for efficiency:
# Good: Pipeline
Get-CSSite -All | Where-Object { $_.Title -like "*Test*" } | Remove-CSSite -Confirm:$false
# Bad: Loop
$sites = Get-CSSite -All
foreach ($site in $sites) {
if ($site.Title -like "*Test*") {
Remove-CSSite -Identity $site.Url -Confirm:$false
}
}
4. Logging¶
Add logging for production scripts:
$logFile = "script-$(Get-Date -Format 'yyyyMMdd-HHmmss').log"
function Write-Log {
param($Message)
$timestamp = Get-Date -Format "yyyy-MM-dd HH:mm:ss"
"$timestamp - $Message" | Tee-Object -FilePath $logFile -Append
}
Write-Log "Starting site creation"
New-CSSite -Url "/sites/new" -Title "New Site" -OwnerLogin "admin"
Write-Log "Site creation complete"
See Also¶
- PowerShell Migration Guide
- PowerShell Troubleshooting
- Use
Get-Help <cmdlet-name> -Examplesfor cmdlet-specific examples