Take the 2-minute tour ×
SharePoint Stack Exchange is a question and answer site for SharePoint enthusiasts. It's 100% free, no registration required.

I am unable to use default Deploy Feature in VS2010, for SharePoint 2010 deployment most of the times due to complicated setup we have here.

I usually have to run STSADM commands, one at a time to deploy the WSP. I need to add a postbuild command (PowerShell) to deploy my solution as soon as I build it. Tried searching on Web but couldn't find anything usefull.

Could anyone please help with any reference or actual script?

share|improve this question
add comment

1 Answer 1

up vote 1 down vote accepted

I created this PowerShell script that waits until solution is retracted before removing and waits until a solution is properly added until it returns.

Theres still a couple of things i want to add to this, including proof of succesfull provisioning and support for dependent solutions, but it works for me.

function Provision-SPSolution
{
    [CmdletBinding()]
    param
    (
        [Parameter(Mandatory=$true)]
        [string]
        $solutionPath
    )

    $solutionAbsolutePath = Resolve-Path($solutionPath) -EA 0 -EV err
    if ($err)
    { 
        Write-Host "`nException: `n$err" -ForegroundColor Yellow
        break
    }

    $Error.Clear()
    Write-Host "Trying to add solution $solutionAbsolutePath"
    $solution = Microsoft.Sharepoint.Powershell\Add-SPSolution $solutionAbsolutePath -EA 0 -EV err

    if ($err)
    { 
        Write-Host "`nException: `n$err" -ForegroundColor Yellow
        break
    }

    Write-Host "`nSolution added"
    Microsoft.Sharepoint.Powershell\Install-SPSolution $solution.Name -GacDeployment
    $timeout = 30 # seconds
    $startTime = Get-Date
    $endTime = $startTime.AddSeconds($timeout) 
    $timeSpan = New-TimeSpan $startTime $endTime

    while ( $solution.JobExists -or ( $timeSpan -gt 0 ) )
    {
        $timeSpan = new-timespan $(get-date) $endTime
        Write-Host "." -NoNewline
        Sleep 1
    }

    Write-Host "`nSolution deployed"
}

function Unprovision-SPSolution
{
    [CmdletBinding()]
    param
    (
        [Parameter(Mandatory=$true)]
        [string]
        $solutionName
    )

    $solution = Microsoft.Sharepoint.Powershell\Get-SPSolution | ?{$_.Name -eq $solutionName}

    if (!$solution)
    {
        Write-Host "`nNo solution found with name $solutionName" -ForegroundColor Yellow
        return
    }

    Write-Host "`nSolution found!"

    if ($solution.Deployed)
    {
        Write-Host "`nSolution is deployed, so uninstalling..."

        Microsoft.Sharepoint.Powershell\Uninstall-SPSolution $solutionName -Confirm:$false
        $timeout = 30 # seconds
        $startTime = Get-Date
        $endTime = $startTime.AddSeconds($timeout) 

        while ( $solution.JobExists )
        {
            if ( $startTime -gt $endTime )
            {
                Write-Host "Timeout reached ($timeout s)"
                break
            }
            Write-Host "." -NoNewline

            Sleep 1
        }
        Write-Host "`nSolution uninstalled"
    }
    Microsoft.Sharepoint.Powershell\Remove-SPSolution $solutionName -Confirm:$false

    Write-Host "`nSolution removed"
}
share|improve this answer
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.