################################################################################## # # # Script name: Check-SiteDirectory.ps1 # Author: goude@powershell.nu # mattias.karlsson@zipper.se # # Homepage: www.powershell.nu # www.mysharepointofview.com # ################################################################################## param ([string]$Url, [string]$Web, [string]$List, [string]$Template, [switch]$help) function GetHelp() { $HelpText = @" DESCRIPTION: NAME: Check-SiteDirectory.ps1 Checks The SiteDirectory for Approved Sites and creates them if they do not exist PARAMETERS: -Url Url to SharePoint (Required) -Web Relative Url to SiteDirectory (Required) -List List containing information regarding Sites (Required) -Template Template to use when creating New Sites (Required) -help Prints the HelpFile (Optional) SYNTAX: Check-SiteDirectory.ps1 -Url http://makanigo -Web SiteDirectory -List Sites -Template STS#0 Cheks through the List "Sites" for Approved Items and creates New Sites based on the information in the SiteDirectory. Check-SiteDirectory.ps1 -help Displays the help topic for the script Additional information: If you don't know which Templates are available, run the following function from the SharePoint Server function Get-Templates([string]`$Url) { `$SPSite = New-Object Microsoft.SharePoint.SPSite($Url) `$SPSite.GetWebTemplates(1033) | Select Name, Description `$SPSite.Dispose() } Get-Templates -Url http://SharePoint "@ $HelpText } function Check-SiteDirectory ([string]$Url, [string]$Web, [string]$List, [string]$Template) { # GAC [System.Reflection.Assembly]::LoadWithPartialName("Microsoft.SharePoint") | Out-Null # Connect To Specified SharePoint Site $Url = $Url.TrimEnd("/") $Site = New-Object Microsoft.SharePoint.SPSite($Url) $OpenWeb = $Site.OpenWeb($Web) $SiteList = $OpenWeb.Lists[$List] # Enumerate through List Items $SiteList.Items | ForEach { # Set Up Variables [string]$Title = $_["Title"] [string]$Description = $_["Description"] # Get Url Path From Item [string]$Path = $_["URL"] [string]$Path = ($Path.Split(","))[0] # Check if item matches Approved "0" if ( $_["Approval Status"] -match 0) { Write-Host "$Title is Approved" -ForeGroundColor Green # Check if the Site already Exists. if ( ($Site.AllWebs | Select Url) -match $Path) { Write-Host "$Path Already Exists" -ForeGroundColor Yellow } else { # Build ConnectionString to Relative Site $RelativeUrl = $Path.Replace("$Url","") $RelativeUrl = $RelativeUrl.TrimStart("/") $RelativeUrl = $RelativeUrl.Split("/") $Counter = $RelativeUrl.Count -1 # Set WebUrl as last part of total Url $WebUrl = $RelativeUrl[$Counter] if($Counter -eq 0) { } else { $Counter = $Counter -1 } # Build up the Parent Url $RelativeUrl[0..$Counter] | ForEach { $ParentUrl+= "/" + $_ } # Check if Parent Site Exists $ParentSite = $Url + $ParentUrl if ( ($Site.AllWebs | Select Url) -match $ParentSite) { Write-Host "ParentSite = $ParentSite Exists" -ForeGroundColor Green # Connect to The Site $AddSite = $Site.OpenWeb($ParentUrl) # Check if Site Exists if ( ($Site.AllWebs | Select Url) -match $Path) { Write-Host "WebSite $Path Alreadey Exists" -ForeGroundColor Yellow } else { Write-Host "Creating Site $Path" -ForeGroundColor Green # Create the New Site $AddSite.Webs.Add($WebUrl, $Name, $Description, [int]1033, $Template, $False, $False) # Dispose Variable $AddSite.Dispose() $AddSite = $Null $ParentUrl = $Null } } else { # ParentSite Doesn't Exist Write-Host "ParentSite $ParentSite Doesn't Exist." -ForeGroundColor Red Write-Host "Please Create a ParentSite or change the Url Specified in the SharePoint List Item" -ForeGroundColor Yellow } } } else { # Site Is Not Approved Write-Host "$Title is not approved" -ForeGroundColor Red } } # Dispose Variables $Site.Dispose() $OpenWeb.Dispose() } if($help) { GetHelp } if ($Url -AND $Web -AND $List -AND $Template) { Check-SiteDirectory -Url $Url -Web $Web -List $List -Template $Template }