#DelvDisks.ps1
#This script file is intended to provide an example script of how vDisk deletion could be performed.
#This script will get a list of DiskLocators to use for deletion.

#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 interrogate 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.
#

#Version 1.0  modified 5-5-2019
#Version 4.0 5-18-2021

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
}

Write-Host "First Master Found - Looking for vhd and vhdx files on Master $FirstMaster"
$DelFiles="$ScriptSource\..\Config\DelFiles.txt"
if (Test-Path $DelFiles){del $DelFiles}
#get Stores for Master

Set-PvsConnection -Server $FirstMaster -ErrorAction SilentlyContinue
$ServerInfo = Get-PvsServer -ServerName $FirstMaster
$mySite = $ServerInfo.SiteName

$rowoutput=""
Write-Host "Getting PVS Stores"
$MasterStoresArray = Get-PvsStore
foreach($Store in $MasterStoresArray)
{
	
	$ServerStorePathOrig =  $Store.Path
	$StoreName = $store.StoreName
	Write-Host "Getting vDisks for Store [$StoreName]"
	$vDiskLocators = Get-PvsDiskLocator -StoreName $StoreName -SiteName $mySite
	foreach ($vDisk in $vDiskLocators)
	{
		$Name = $vDisk.DisklocatorName
		$Id = $vDisk.DisklocatorId
		$VersionInfo = Get-PvsDiskVersion -DiskLocatorId $Id
		
		foreach ($Version in $VersionInfo)
		{
			$filename = $Version.diskfilename
			Write-Host "Found vDisk $Name $filename"
			$rowoutput = "$StoreName;$Name;$Id;$filename"
			$rowoutput | Out-File -LiteralPath $DelFiles -Encoding ASCII -Append
		}
	}
}

Sleep 5



