#Import PVS PowerShell Module
#This script file is intended to provide an example script of how vDisk replication can be performed.
#This script will get secondary site servers for each Farm defined by a Master Server

#This script was created by Rob Zylowski Citrix CCS with help from many folks
#It should be used as an example of how to perform PVS Disk replication.
#This script will interogate defined PVS master Servers to compile a list of stores and store paths.
#
#Although every effort has been made to ensure that these sample scripts work properly, 
#Citrix disclaims any responsibility for any and all liability or responsibility for any 
#damages that may result from using these scripts. 
#The sample scripts are provided to you as is, with no warranty or guarantees.
#
try{
    Import-Module 'C:\Program Files\Citrix\Provisioning Services console\Citrix.PVS.SnapIn.dll' -ErrorAction Stop
} catch {
    Write-Host "PVS PowerShell Snap-In Not Found" -BackgroundColor Black -ForegroundColor Red
    $msg = "PVS PowerShell Snap-In Not Found on $env:computername.  Script terminated."
    Write-EventLog -LogName Application -Source $logSource -EventId 2 -EntryType Error -Message $msg -Category 0
    exit
}


function Get-ScriptDirectory
#This gets the current directlry which we use to store the text data files
{
 $Invocation = (Get-Variable MyInvocation -Scope 1).Value
 Split-Path $Invocation.MyCommand.Path
}

$ScriptSource = Get-ScriptDirectory

$Masters = Get-Content "$ScriptSource\..\config\masters.txt"
if ($Masters.count -gt 1)
{
	$FirstMaster = $Masters[0].ToLower()
}
else
{
	$FirstMaster = $Masters
}

$MasterStores = @{}
#get Stores for Master
Set-PvsConnection -Server $FirstMaster -ErrorAction SilentlyContinue
#clear output variable
$rowoutput=""
$servercount = 0
foreach ($MasterServerName in $Masters)
{
	Write-Host "Conecting to Master Server $MasterServerName"
	Set-PvsConnection -Server $MasterServerName -ErrorAction SilentlyContinue
	$MasterServer = Get-PvsServer -ServerName $MasterServerName
	$masterServerSite = $MasterServer.SiteName
	$Farm = Get-PvsFarm
	$FarmName = $Farm.FarmName
	$MasterServerFarm = $FarmName
	if ($MasterServer.Name -eq $null){continue}
	
	# Creating global list
	Write-Host "Retrieving all required objects from PVS"
	[array]$AllSites = Get-PvsSite
	[array]$AllServers = Get-PvsServer
	
	# Generate DataTable
	ForEach ($Site in $AllSites) 
	{
		$SiteName = $Site.SiteName
		#if its the master servers site skip it
		if ($SiteName -eq $masterServerSite){continue}
		[string]$SiteID = $Site.SiteID
		Write-Host -NoNewline "Retrieving replica servers for site "; Write-Host -ForegroundColor Blue $($Site.SiteName)
		ForEach ($Server in $AllServers) 
		{
			$ServerName = $Server.Name
			$ServerSiteName = $Server.SiteName
			$myServer = $Server.ServerName
			if ($Server.SiteName -ne $SiteName){continue}
			if ($Masters -contains $myServer ){continue}
			$rowoutput += "$FarmName" + ";" + "$SiteName" + ";" + "$myServer" + "`r`n"
			$servercount++
		}
	}
	
}
$SecondaryServers = "$ScriptSource\..\config\SecondarySiteServerList.txt"
if ($servercount -eq 0)
{
	$rowoutput = "No Secondary Site Servers Found"
}
	
$rowoutput | Out-File -LiteralPath $SecondaryServers -Encoding ASCII




