########################################################## # # Script-Name: Create-Sites.ps1 # # authors: Niklas Goude # Mattias Karlsson # # Url: http://mysharepointofview.com # http://www.PowerShell.nu # ########################################################## param ([string]$Csv, [string]$Site,[switch]$help) function GetHelp() { $HelpText = @" DESCRIPTION: NAME: Create-Sites.ps1 Adds Information From a CSV file to SharePoint PARAMETERS: -Csv Name of the Csv File Path (Required) -Site The Sharepoint Site (Required) -help Prints the HelpFile (Optional) SYNTAX: Create-Sites.ps1 -Csv "C:\Filepath\CompanyListExample.csv -Site "http://Sharepoint" Adds Sites specified in CompanyListExample.csv to the SharePoint site http://Sharepoint And adds the SIte to the MOSS Site Directory Create-Sites.ps1 -help Displays the help topic for the script "@ $HelpText } function Get-Csv ([string]$CsvFile, [string]$Site) { Import-Csv -Path $CsvFile | ForEach { # Checking That Name Doesn't Conatain any Special Characters $Name = $_.Name -replace " ","" $Name = $Name -replace "/","" $Name = $Name -replace "-","" $Name = $Name -replace "_","" $Name = $Name -replace "\\","" $Name = $Name -replace ",","" $Name = $Name -replace "\.","" $Name = $Name -replace "å","a" $Name = $Name -replace "ä","a" $Name = $Name -replace "ö","o" $SiteUrl = $Site.TrimEnd("/") $FullName = $_.Name $SAPNumber = $_.SAPNumber $Url = $SiteUrl + "/" + $Name $Description = $_.Description # Call the Add-Sites function Add-Site $Site $FullName $Name $SAPNumber $Url } } function Add-Site([string]$Site, [string]$FullName, [string]$Name, [string]$SAPNumber, [string]$Url, [string]$Description) { # GAC [System.Reflection.Assembly]::LoadWithPartialName("System.Web") | Out-Null [System.Reflection.Assembly]::LoadWithPartialName("Microsoft.SharePoint") | Out-Null # Setting up Variables $ListItem = "Sites" $SiteDirectory = "SiteDirectory" $SPSite = New-Object Microsoft.SharePoint.SPSite($Site) $OpenWeb = $SPSite.OpenWeb($SiteDirectory) $List = $OpenWeb.Lists[$ListItem] $UrlObject = $Url + ", " + $FullName # Creating List Item if ($List.Items | where { $_.Name -like $FullName }) { Write-Host "ListItem $FullName Already Exists" -ForeGroundColor Yellow } else { # Cretaing List Item $item = $List.Items.Add() $item["Title"] = $FullName $item["Site name"] = $Name $item["URL"] = $UrlObject $item["SAP Number"] = $SAPNumber $item.Update() Write-Host "$FullName Added To $ListItem List." -ForegroundColor Green } # Creating Site # Checking if Site exists $CheckSite = $SPSite.Allwebs | Where { $_.Name -match $Name } if ($CheckSite) { Write-Host "Site $FullName Already Exists" -ForegroundColor Yellow } else { $SPSite.AllWebs.Add($Name,$FullName,$Description,[int]1033,"CustomerTemplate.stp",$FALSE,$FALSE) Write-Host "Site $FullName Created" -ForegroundColor Green } $SPSite.Dispose() $OpenWeb.Dispose() } if($help) { GetHelp } if($Csv -AND !$Site) { GetHelp } if($Csv -AND $Site) { Get-Csv $Csv $Site }