2 PowerShell Scripts to help with SharePoint Development

This script from here by Omar Stewart

The first, is a script to quickly lookup a SharePoint Error Based on Correlation ID. Here’s the script:

————————————————————————————————————-

#

# Filename: Find-SPError.ps1

#

Write-Host

$start = (Get-Date).AddMinutes(-2);

$end = (Get-Date).AddMinutes(2);

#Get Correlation ID

$correlation = "*";

$correlation += Read-Host "Correlation ID";

$correlation = $correlation.Trim();

$correlation +="*";

Write-Host "Searching for Correlation..";

$error = Get-SPLogEvent -StartTime $start -EndTime $end | ? {$_.Correlation -like $correlation};

if ($error -eq $null){

Write-Host "Correlation Not Found";

exit;

}

$error | Select Message | Format-List;

try{

$error | Out-GridView;

}

Catch{

Write-Host "Enable Grid-View for pop-up display.";

}

————————————————————————————————————-

The next script is to provision a site collection :

————————————————————————————————————–

#File: NewDevSite.ps1

#

# check to ensure Microsoft.SharePoint.PowerShell is loaded

Write-Host "Loading SharePoint Powershell Snapin" -ForegroundColor Yellow;

Add-PSSnapin "Microsoft.SharePoint.Powershell" -ErrorAction SilentlyContinue;

Write-Host;

#SharePoint Environment URL:

$DevUrl = "http://stewey.com/sites/";

$DEV = [Security.Principal.WindowsIdentity]::GetCurrent().Name;

$Template = "STS#0";

$SiteTitle = Read-Host "Enter Site Title";

$site = $null;

if ($SiteTitle.Trim() -ne ""){

Write-Host "Creating New Site.." -ForegroundColor Yellow;

Write-Host;

$url= $DevUrl + $SiteTitle;

$description = $SiteTitle + " – Staging";

$site = New-SPSite -Url $url -Name $SiteTitle -Template $Template -Description $description -OwnerAlias $DEV;

Write-Host "Site Successfully Created" -ForegroundColor Yellow;

Write-Host;

}

else {Write-Host "Invalid Site Title."}

if ($site -ne $null){

Write-Host "Opening Site..";

#Site Pages

$homepage = $site.url;

$settingspage = $site.url + "/_layouts/settings.aspx";

$viewallpage = $site.url + "/_layouts/viewlsts.aspx";

#Open Pages In New Tabs

$OpenInNewTab = 0x0800;

$ie = New-Object -Com "InternetExplorer.Application";

$ie.Navigate2($homepage, $OpenInNewTab);

$ie.Navigate2($settingspage, $OpenInNewTab);

$ie.Navigate2($viewallpage, $OpenInNewTab);

$ie.Visible = $true;

}

————————————————————————————————————–

I hope these are useful to you and maybe even serve as an introduction to PowerShell. If you have any corrections to the scripts, or additions please share!

Advertisements