Tuesday, February 28, 2012

PowerShell - Completing the configuration of a SysPrep'd SharePoint 2010

I recently created a template for spawning new development virtual machines (VM).  This template included SharePoint 2010 (SP2010).  I am using Virtual Machine Manager to generate my templates.  Software, such as SP2010, are sysprep’d as part of the process.

Since it is sysprep’d, then I’m going to need to finish the configuration once a new VM is spawned.  The great thing about SP2010 is that I can accomplish this with a PowerShell script.

Below is my configuration file and PowerShell script for making this happen.

---------- SharePointConfig.xml ----------

<Configuration>
  <Log Path="\\SomeDefinedLocationForLogs\SharePoint.log.txt" />
  <SharePoint>
    <Farm
        ConfigureEmail="true"
        FarmAccount="domain\username"
        FarmAccountPassword="password"
        DBServer="NEW_MSSQLSERVER"
        ConfigDB="SharePoint_Config"
        AdminContentDB="CentralAdmin_Content"
        Passphrase="password"
        EmailFromAddress="sharepoint@domain"
        EmailReplyToAddress="sharepoint@domain"
        SMTPServer="smtp.domain">
        <CentralAdmin Port="2007"/>
    </Farm>
  </SharePoint>
</Configuration>

------------------------------------------------
---------- SharePoint2010.ps1 ----------

    # Add SharePoint cmdlets reference
    Add-PSSnapin Microsoft.SharePoint.PowerShell
   
    $serverName = $env:computername

    $configFilepath = $args[0]
    $config = [xml](Get-Content $configFilepath)
    $logFile = $config.Configuration.Log.Path

try
{
    # Capture configuration into local variables
    $farmAcctUsr = $config.Configuration.SharePoint.Farm.FarmAccount
    $farmAcctPwd = $config.Configuration.SharePoint.Farm.FarmAccountPassword
    $configDB = $config.Configuration.SharePoint.Farm.ConfigDB
    $adminContentDB = $config.Configuration.SharePoint.Farm.AdminContentDB
    $configEmail = $config.Configuration.SharePoint.Farm.ConfigureEmail
    $smtp = $config.Configuration.SharePoint.Farm.SMTPServer
    $fromAddr = $cong.Configuration.SharePoint.Farm.EmailFromAddress
    $replyAddr = $config.Configuration.SharePoint.Farm.EmailReplyToAddress
    $passphrase = $config.Configuration.SharePoint.Farm.Passphrase
    $caPort = $config.Configuration.SharePoint.Farm.CentralAdmin.Port
   
    #Database Server (local?)
    $server = $serverName
    $server += "\"
    $server +=  $config.Configuration.SharePoint.Farm.DBServer
    
    # Create credential account object for Farm
    $farmAcct = New-Object System.Management.Automation.PSCredential $farmAcctUsr, (ConvertTo-SecureString $farmAcctPwd -AsPlainText -force)
   
    # Verify value of $passphrase variable
    if ($passphrase.Length)
    {
        $passphrase = (ConvertTo-SecureString $passphrase -AsPlainText -force)
    }
    else
    {
        $passphrase = $farmAcct.Password
    }
   
    # Create Farm
    New-SPConfigurationDatabase -DatabaseName $configDB -DatabaseServer $server -AdministrationContentDatabaseName $adminContentDB -Passphrase $passphrase -FarmCredentials $farmAcct
   
    # Verify farm creation
    $spFarm = Get-SPFarm -ErrorAction SilentlyContinue -ErrorVariable err
    if ($spfarm -eq $null)
    {
        throw "Unable to verify farm creation!"
    }
   
    # Secure SharePoint Resources
    Initialize-SPResourceSecurity
   
    # Install Services
    Install-SPService
   
    # Install Features
    Install-SPFeature -AllExistingFeatures
   
    # *** Install Central Admin ***
    # Lookup existing central admin
    $url = "http://$($serverName):$($caPort)"
    $sca = [Microsoft.SharePoint.Administration.SPWebApplication]::Lookup($url)
   
    # If no central admin, create one
    if ($sca -eq $null)
    {
        # Provisioning Central Administration
        New-SPCentralAdministration -Port $caPort -WindowsAuthProvider "NTLM"
       
        #Install Help
        Install-SPHelpCollection -All
       
        #Install Application Context
        Install-SPApplicationContent
    }
   
    # Configure Outgoing Email
    if ($configEmail -eq $true)
    {
        $cmd = "stsadm"
       
        $argList = " -o email"
        $argList += " -outsmtpserver `"$smtp`""
        $argList += " -fromaddress `"$fromAddr`""
        $argList += " -replytoaddress `"$replyAddr`""
        $argList += " -codepage 65001"
   
        # Execute the stsadm command for modifying email configuration.  Use the farmAcct as the account to execute under.
        start-process $cmd -ArgumentList $argList -Credential $farmAcct
    }
    else
    {
        # Log email skipped
    }
   
    echo ""
}
catch
{
    $err = $Error[0].Exception
    Write-Log $logFile $err 2
}

---------------------------------------------


No comments:

Post a Comment