#CheckVdiskForDel.ps1
#This script file is intended to provide an example script of how vDisk replication can be performed.
#This script will check vdisks chose for deletion to make sure they are not assigned to targets.

#This script was created by Rob Zylowski Citrix CCS
#It should be used as an example of how to perform PVS Disk replication.
#
#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.
#

#Version 2.0.2 modified 7-5-2018
#Version 4.0.0 modified 5-18-2021 - Modified to handle Versions

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 directory 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
}
$InUseFilesOut="$ScriptSource\..\Config\vDisksToDeleteInUse.txt"
if (Test-Path $InUseFilesOut){Remove-Item -Path $InUseFilesOut}
Sleep 2

Write-Host "****Checking vDisks to see if they are in use***"
$DelFiles="$ScriptSource\..\Config\vDisksToDelete.txt"
if (Test-Path $DelFiles)
{
	$FilesToDelete = Get-Content -Path $DelFiles -Encoding ASCII
}
$FileCount = $FilesToDelete.count
Write-Host "[$FileCount] Files were chosen to delete"

#Lets check each master server
$rowoutput=""
foreach ($Master in $Masters)
{

	Set-PvsConnection -Server $Master -ErrorAction SilentlyContinue 
	$myServer = Get-PvsServer -ServerName $Master
	$mySiteName = $myServer.siteName
	foreach ($VDiskLine in $FilesToDelete)
	{
		$deviceoutput=""
		$arrLine = $VDiskLine -split ";"
		$myStore = $arrLine[0]
		$MyvDisk = $arrLine[1]
		$MyvDiskFile = $arrLine[3]
		Write-Host "Checking vdisk Store [$myStore] vDisk [$MyvDisk] for usage"
		try {
			$test = Get-PvsDevice -DiskLocatorName "$MyvDisk" -StoreName "$myStore" -SiteName "$mySiteName" -InformationAction:SilentlyContinue
			
		}
		Catch {
			Write-Host "vDisk Not Found"
			continue
		}
		$Mycount = $test.count
		if ($Mycount -gt 0)
		{
			$i=0
			foreach ($Device in $test)
			{
				#Lets Check for versions
				Write-Host "Checking for Versions"
				$TestVersions = Get-PvsDiskVersion -DiskLocatorName "$MyvDisk" -StoreName "$myStore" -SiteName "$mySiteName"
				#Lets see if your version is in use and if its the highest version.
				if ($TestVersions.Count -gt 1)
				{
					Foreach ($VersionInfo in $TestVersions)
					{
						$DeviceCount = $VersionInfo.DeviceCount
						if ($DeviceCount -gt 0)
						{
							$devicename = $Device.Name
							$Version = $VersionInfo.Version
							$VersionFile = $VersionInfo.DiskFileName
							if ($MyvDiskFile -eq $VersionFile)
							{
								if ($i -gt 2){break}
								if ($i -eq 0)
								{
									$deviceoutput = "$devicename"
								}
								else
								{
									$deviceoutput += ", $devicename"
								}
								break
							}
						
						}
						else
						{
							#If no device skip this
							continue
						}
					}
				}
				else
				{
					#Only list 3 examples
					if ($i -gt 2){break}
					$devicename = $Device.Name
					Write-Host "vDisk [$MyvDisk] is assigned to Target [$devicename]"
					if ($i -eq 0)
					{
						$deviceoutput = "$devicename"
					}
					else
					{
						$deviceoutput += ", $devicename"
					}
				}
				$i++
				
			}
			if ($deviceoutput -ne "")
			{
				$rowoutput += "$Master;$myStore;$MyvDisk-Ver[$Version];$mycount;$deviceoutput`r`n"
				$deviceoutput = ""
			}
			
			
		}
	}
}

if ( $rowoutput -ne "")
{
	$rowoutput | Out-File -FilePath $InUseFilesOut -Encoding ASCII
}
Sleep 3