Saturday, August 6, 2011

Beginning Scriptable Remote SharePoint 2010 Deployment

This post is going to cover how I setup my environment to remotely deploy to SharePoint 2010. It assumes that you have a Powershell script that will perform all of your deployment needs as if you were executing it locally on the server. I will cover my deployment script in a different post.

The following is an example of a non-scripted/able remote deployment. It will attach to the server using CredSSP and prompt for credentials, then it will setup the SharePoint Management Console. Each line must be run separately.

Enter-PSSession -ComputerName devel39.portaldev.doitbestcorp.com -Authentication CredSSP -Credential $([Security.Principal.WindowsIdentity]::GetCurrent().Name);
& ' C:\Program Files\Common Files\Microsoft Shared\Web Server Extensions\14\CONFIG\POWERSHELL\Registration\sharepoint.ps1 '; 
Set-Location C:\Deploy; 
.\Deploy.ps1;
Exit-PSSession;

There are a couple things of note. Enter-SPSession is not useful for scripting as it doesn't send the subsequent commands to the remote session. Start-Transaction, to record the script output, throws an error and continues because it is not supported through PSSession.
This is all well and good if you want to put a lot of effort into deployment.

Powershell provides New-PSSession to open a remote connection. Use the Invoke-Command to send commands to that session.

$remote = New-PSSession -ComputerName name.domain.com;
Invoke-Command -session $remote -scriptblock {
    sl "C:\SharePointDeploy";
    & .\Deploy.ps1 
};
Remove-PSSession $remote;
We can build on this a bit by prompting for credentials and invoking the command as a job. "AsJob" can be loosely equivocated to threads in powershell. There may be many times where the order in which commands end does not matter or you can benefit from an asynchronous programming model which this argument provides.
$remote = New-PSSession -ComputerName name.domain.com -Authentication CredSSP -Credential $([Security.Principal.WindowsIdentity]::GetCurrent().Name); Invoke-Command -session $remote -scriptblock { 
    sl "C:\SharePointDeploy"; 
    & .\Deploy.ps1 
} -AsJob;
Remove-PSSession $remote;

No comments: