Thursday, July 9, 2015

Skeleton of basic powershell sharepoint file

Below is basic skeleton of powershell file for sharepoint:-


param(

)
function Ensure-PSSnapin
{
    if ((Get-PSSnapin "Microsoft.SharePoint.PowerShell" -ErrorAction SilentlyContinue) -eq $null) 
    {
        Add-PSSnapin "Microsoft.SharePoint.PowerShell" -Verbose:$false
        Write-Verbose "SharePoint Powershell Snapin loaded."
    } 
}

function Release-PSSnapin
{
    if ((Get-PSSnapin "Microsoft.SharePoint.PowerShell" -ErrorAction SilentlyContinue) -ne $null) 
    {
        Remove-PSSnapin "Microsoft.SharePoint.PowerShell" -Verbose:$false
        Write-Verbose "SharePoint Powershell Snapin removed."
    } 
}

Ensure-PSSnapin
$gc = Start-SPAssignment -Verbose:$false

try
{
        
# do your stuff        

} 
catch
{
    write-output -ForegroundColor Red $_.ToString()
 write-output -ForegroundColor Red $_.Exception.ToString()
}
finally 
{
 Stop-SPAssignment $gc -Verbose:$false
 Release-PSSnapin
}

No comments:

Post a Comment