Search This Blog

Wednesday, 22 April 2015

Poweshell - Create Sites From CSV

<#
.SYNOPSIS
   <A brief description of the script>
.DESCRIPTION
   <A detailed description of the script>
.PARAMETER <paramName>
   <Description of script parameter>
.EXAMPLE
   <An example of using the script>
#>


$UrlWebApp = Read-Host "Enter site URL (Eg: http://<HostName>:1111/sites/sitecollname )"
$global:strGroupListFileName = Read-Host "Enter the location of Sites.csv"
$LogPath = Read-Host "Enter a location for log file  :"
$LogFileName = Read-Host "Enter name for log file  :"
$FilePath = $LogPath + "\" + $LogFileName


# =========================== LOADING SHAREPOITN POWERSHELL SNAPIN FILE DETAILS =====================================
#Loading Snap in for Microsoft.Sharepoint.Powershell
$snapin = Get-PSSnapin | Where-Object {$_.Name -eq 'Microsoft.SharePoint.Powershell'}

if ($snapin -eq $null)
{
Write-Host "Loading SharePoint Powershell Snapin" -ForegroundColor Green
$snapin = Add-PSSnapin "Microsoft.Sharepoint.Powershell"

Write-Host $snapin
}
# =============================================================================

#=========================== SharePoint DLL ==========================
[void][System.Reflection.Assembly]::LoadWithPartialName("Microsoft.SharePoint")
[void][System.Reflection.Assembly]::LoadWithPartialName("Microsoft.SharePoint.Publishing")



function write-log([string]$label, [string]$logMsg)
{
    if($logFileCreated -eq $False)
    {
        write-host "Creating log file..."
        if((Test-Path -path $LogPath) -ne $True)
        {
            write-host "Provide proper values to LogPath folder" -ForegroundColor Red
        }
        else
        {
            Add-Content -Path $FilePath -Value $logHeader
            $script:logFileCreated  = $True
            write-host "Log file created..."
        }
    }
    else
    {
        [string]$info = [System.String]::Format("[$Date] {0}: {1}",$label, $logMsg)
        Add-Content -Path $FilePath -Value $info
    }
}

function createSubsite($parentSiteCollectionUrl,$SubsiteName,$TemplatePath)
{
$IssubsiteCreated = $false;
$SubsiteUrl ="$parentSiteCollectionUrl/$SubsiteName";
$ParentWeb = Get-SPWeb $parentSiteCollectionUrl
$SubWeb = Get-SPWeb $SubsiteUrl -ErrorAction SilentlyContinue -ErrorVariable err
try
{
if(([string]$err).Length -ne 0)
{
$SubWeb = New-SPWeb $SubsiteUrl -Description "A site created by ps" -ErrorAction SilentlyContinue -ErrorVariable err -name $SubsiteName
$WebTemplate = $ParentWeb.GetAvailableWebTemplates($ParentWeb.language) | Where-Object {$_.Title -eq $TemplatePath};
$SubWeb.ApplyWebTemplate($WebTemplate.Name) | out-null
return $SubWeb.Url;
}
else
{
Write-Host $err -ForegroundColor RED
return $SubWeb.Url;
}
return $SubWeb.Url;
}
finally
{
if($SubWeb -ne $null)
{
$SubWeb.Close();
}
if($ParentWeb -ne $null)
{
$ParentWeb.Close();
}
}
}

[Microsoft.SharePoint.SPSecurity]::RunWithElevatedPrivileges(
{
                Write-Host "Create Process Started...Time - $(get-date -format HH:mm:ss)" -Foregroundcolor Green
                write-log  "Create Process Started...Time -" "$(get-date -format HH:mm:ss)"
                $StartTime=Get-Date

$site = Get-SPSite $UrlWebApp
$SiteCollectionUrl = $Site.Url;
$PathDealSiteTemplate = "STS#0"
       if($site -ne $null)
{
                  $siteUrl = $Site.Url;
 Import-CSV -Path $global:strGroupListFileName   | ForEach-Object {
                    if($_.SiteName -ne $null -and $_.SiteName -ne "")
                     {
$SiteName=$_.SiteName
                    createSubsite $SiteCollectionUrl $SiteName $PathDealSiteTemplate
                        Write-Host "Site created for Asset $SiteName";
                        Write-Log "Site created for Asset $SiteName";
   
                       }
}                  
        }
    $StartTime=(Get-Date) - $StartTime
      write-host "Total execution time- " $StartTime
      write-log "Total execution time- " $StartTime
      Write-Host "Create Process Completed...Time - $(get-date -format HH:mm:ss)" -Foregroundcolor Green
      write-log "Create Process Completed...Time -" "$(get-date -format HH:mm:ss)"                    
})

No comments:

Post a Comment