Monday, October 22, 2012

Sample PowerShell .wsp Install Script


Function EndPause {
    Write-Host "Press any key to continue ..."

    $x = $host.UI.RawUI.ReadKey("NoEcho,IncludeKeyDown")
}

$x = [System.Reflection.Assembly]::LoadWithPartialName(”Microsoft.SharePoint”)

$siteUrlDev = "http://devsite/testsite/"
$siteUrlStg = "http://stgsite/testsite/"
$siteUrlProd = "http://prodsite/testsite/"

Try
{
$spSite =  new-object Microsoft.SharePoint.SPSite($siteUrlStg) 
}
Catch {}
Try
{
$spSite =  new-object Microsoft.SharePoint.SPSite($siteUrlDev) 
}
Catch {}
Try
{
$spSite =  new-object Microsoft.SharePoint.SPSite($siteUrlProd) 
}
Catch {}
if ($spSite -eq $null)
{ 
    "ERROR ------------ Site not found. Quitting."
    EndPause
    exit
}

$spWeb = $spSite.OpenWeb()

$spList = $spWeb.Lists["ListName"]

if ($spList -eq $null)
{
    "ERROR -------- ListName list not found. Quitting"
    EndPause
    exit
}
else
{
    {ListName list found}
}
If (!$spList.Fields.ContainsField("newdatefield"))
{ 
    # $spList.Fields.Add("test","DateTime",0) 
    $dateField = $spList.Fields.CreateNewField("DateTime","newdatefield")
    $dateField.DisplayFormat = [Microsoft.SharePoint.SPDateTimeFieldFormatType]::DateOnly
    $dateField.Required = $false;
    $spList.Fields.Add($dateField)
    "Added newdatefield field"    
}
else
{
    "newdatefield field already added. This is fine."
}

if (Test-Path ("MySPApp.wsp"))
{
    Set-Alias -Name stsadm -Value $env:CommonProgramFiles"\Microsoft Shared\Web Server Extensions\12\BIN\STSADM.EXE"
    
    Write-Host "Upgrading solution to 9.0.1" -foregroundcolor green
    &stsadm -o upgradesolution -name "MySPApp.wsp"  -filename "MySPApp.wsp" -immediate -allowGacDeployment -allowCasPolicies

    &stsadm -o execadmsvcjobs
}
else
{
    "MySPApp.wsp file missing. Quitting"
    EndPause
    exit
}
EndPause


No comments:

Post a Comment