Pawel Serwan Blog

Citrix, Microsoft and other stuff

[PowerShell] Citrix StoreFront 3.x installation script

2 Comments

psRecently I was working on preparation of moving from Web Interface 5.4 to StoreFront 3.x. Installing few servers manually doesn’t make sense for me so I wrote a PowerShell script that automates whole installation process. Source code is available below. I hope you will like it.


<# .SYNOPSIS
This script installs StoreFront 3.x
.DESCRIPTION
The following components are installed:
- necessary Roles and Features
- StoreFront 3.x
- no further customization in this script
.PARAMETER
.REQUIREMENTS
Citrix StoreFront 3.x setup media has to be downloaded.
It is suggested to store it in the same folder as this installation script.
.NOTES
This script was tested on Windows Server 2012 R2
.AUTHOR
Pawel Serwan, https://pawelserwan.wordpress.com/
#>

# Funtion Get-ScriptDirectory
function Get-ScriptDirectory
{
    $Invocation = (Get-Variable MyInvocation -Scope 1).Value;
    if($Invocation.PSScriptRoot)
    {
        $Invocation.PSScriptRoot;
    }
    Elseif($Invocation.MyCommand.Path)
    {
        Split-Path $Invocation.MyCommand.Path
    }
    else
    {
        $Invocation.InvocationName.Substring(0,$Invocation.InvocationName.LastIndexOf("\"));
    }
}
#defining were logfile will be created
$currentDir =  Get-ScriptDirectory
$logfile = $currentDir + "\StoreFrontInstallLog.txt"
$WinFeaturesLog = $currentDir + "\WinFeaturesLog.log"
Remove-Item $logfile -force -EA SilentlyContinue
Remove-Item $WinFeaturesLog -Force -EA SilentlyContinue
# Function Write-Log - sends the results into a logfile as well as in the PowerShell window
Function Write-Log()
{
    Param( [parameter(Mandatory = $true, ValueFromPipeline = $true)] $logEntry,
	   [switch]$displaygreen,
	   [switch]$error,
	   [switch]$warning,
	   [switch]$displaynormal,
       [switch]$displayscriptstart,
       [switch]$displayscriptend
	   )
    if($error) {Write-Host "$logEntry" -Foregroundcolor Red; $logEntry = "[ERROR] $logEntry" }
	elseif($warning) {Write-Host "$logEntry" -Foregroundcolor Yellow; $logEntry = "[WARNING] $logEntry"}
	elseif ($displaynormal) {Write-Host "$logEntry" -Foregroundcolor White; $logEntry = "[INFO] $logEntry" }
	elseif($displaygreen) {Write-Host "$logEntry" -Foregroundcolor Green; $logEntry = "[SUCCESS] $logEntry" }
    elseif($displayscriptstart) {Write-Host "$logEntry" -Foregroundcolor Green; $logEntry = "[SCRIPT_START] $logEntry" }
    elseif($displayscriptend) {Write-Host "$logEntry" -Foregroundcolor Green; $logEntry = "[SCRIPT_END] $logEntry" }
    else {Write-Host "$logEntry"; $logEntry = "$logEntry" }

	$logEntry | Out-File $logFile -Append
}

#importing PowerShell modules
Import-Module ServerManager

#Checking server OS version
$OS = (Get-WmiObject Win32_OperatingSystem).caption
#Checking if script is launched on server with Windows Server 2012 R2
if ($OS -notmatch "Microsoft Windows Server 2012 R2")
{
   Write-Log "StoreFront 3.x should be installed only on servers with Microsoft Windows Server 2012 R2. Please check your operating system version." -error

    Start-Sleep -s 10
   Write-Log "Script will finish now." -warning
    Start-Sleep -s 10
    exit
}

# getting list of installed Windows Features
$InstalledFeatures = Get-WindowsFeature | where {$_.installed -eq "True"} | select name

# Installing prerequisites for Citrix StoreFront 3.x
#http://docs.citrix.com/en-us/storefront/3/sf-install-standard.html

Write-Log "Installing Citrix StoreFront 3.x Prerequisites" -displaynormal
Write-Log "Check list at: http://docs.citrix.com/en-us/storefront/3/sf-install-standard.html" -displaynormal

#Defining required prerequisites: Windows Features and Role Services
$prerequisites = @(
"Web-Server",
"Web-Basic-Auth",
"Web-Http-Redirect",
"Web-Windows-Auth",
"Web-App-Dev",
"Web-Net-Ext45",
"Web-AppInit",
"Web-Asp-Net45",
"Web-Mgmt-Tools",
"Web-Scripting-Tools",
"NET-Framework-45-Features"
)

#installing required Windows Features and Role Services
foreach ($prerequisite in $prerequisites)
{
    if ($InstalledFeatures -match $prerequisite)
    {
       Write-Log "The following feature/role: $prerequisite is already installed on the server." -displaynormal
    }
    else
    {
       Write-Log "Installing $prerequisite on the server." -displaynormal
       $install = Add-WindowsFeature -Name $prerequisite -LogPath $WinFeaturesLog
       if ($install.ExitCode -contains "Success")
       {
            Write-Log "$prerequisite was installed on the server successfully" -displaygreen
       }
       else
       {
            Write-Log "$prerequisite was not installed properly. Please check log in: $currentDir\logfile.log" -error
            Write-Log "Script will finish in 10 seconds." -warning
            Start-Sleep -Seconds 10
            exit
       }
    }
}

Write-Log "Finished Installing Prerequisites. All required Windows Features/Roles were installed successfully." -displaygreen

# Installing Citrix StoreFront 3.x

Write-Log "Installing Citrix StoreFront 3.x" -displaynormal
# Checking if StoreFront setup media exists
if ((Test-Path -Path "$currentDir\CitrixStoreFront-x64.exe") -eq $true)
{
    Write-Log "Citrix StoreFront 3.x installer was found. Installation started." -displaynormal
    start-process -FilePath "$currentDir\CitrixStoreFront-x64.exe" -ArgumentList "-silent" -wait
}
else
{
    Write-Log "No setup media for Citrix StoreFront 3.x was found. Asking for path to the installer." -warning
    $installer = Read-Host "Provide FULL path to installer of Citrix StoreFront 3.x"

    if ((Test-Path -Path "$installer") -eq $false)
    {
        do
        {
            Write-Log "No setup media for Citrix StoreFront 3.0.1 was found. Asking for path to the installer." -warning
            $installer = Read-Host "Provide FULL path to installer of Citrix StoreFront 3.x"
        }
        until ((Test-Path -Path "$installer") -eq $false)
    }

        Write-Log "Citrix StoreFront 3.x installer was found. Installation started." -displaynormal
        start-process -FilePath $installer -ArgumentList "-silent" -wait
}
#checking if last PowerShell command was executed successfully
if($?)
{
    Write-Log "The last command executed successfully. Citrix StoreFront 3.x was installed successfully." -displaygreen
}
else
{
    Write-Log "The last command failed. Citrix StoreFront 3.x was " -error
}

#checking logged in users
$loggedUsers = Get-WmiObject -Class win32_process -computer $env:COMPUTERNAME -Filter "name='explorer.exe'" | % { $_.GetOwner() } | select user

if ($loggedUsers.count -lt 2)
{
# Reboot system

Write-Log “Rebooting System..." -displaynormal

Restart-Computer -ComputerName $env:COMPUTERNAME
}
else
{
    Write-Log "There are users logged to the server. Please reboot server manually." -warning
}

If you have any comments or suggestions just let me know below 🙂 Hope it will be useful
for some of you.

 

Advertisements

2 thoughts on “[PowerShell] Citrix StoreFront 3.x installation script

  1. Thanks Pawel for this script. It helps me alot! *ThumbsUp*

    Like

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s