Citrix PVS Target Devices – searching dump files with PowerShell script

When you are using Citrix Provisioning Services to deliver XenApp/XenDesktop machines you have to remember about all trashes that are collected on persistent disk. Yes, I know, that WriteCache is purged every system reboot. But WriteCache is just a file (.vdiskcache), and on persistent disk we usually have more other data like Event Logs, EdgeSight data, pagefile, etc. So, if you have memory dump file redirected to persistent disk you should remember to delete this file if you system gets BSoD. On our cases EdgeSight agents sometime crashes – and this files we also have to delete. Keeping free space on persistent disk is important, because of WriteCache.

So I wrote a script, that is executed on every system reboot (on SYSTEM account). It search all files with *.dmp extension (you can add more file masks if you want) on disk D:. At the end script is just adding event to application event log (source: PVS_TD_Logs, id: 100, type: Information or Warning). In my case I have SCOM (Microsoft System Center Operations Manager) to monitor all windows servers. So SCOM is finding this event and generating alert.

To add event source:

[System.Diagnostics.EventLog]::CreateEventSource($source, $logname)

you need to have Administrator rights.

Of course you can change this script. It can send you an email, put log file on netowork share or just delete this dump files.

To execute this file in system scheduler you can copy this script to local disk and create batch file (FindDumps.bat):

@echo off
PowerShell.exe -ExecutionPolicy RemoteSigned -File "FindDumps.ps1"

Here are the files (PoSH and batch) – if you want to download it.

#REQUIRES -Version 2.0

<#
.SYNOPSIS
 Finding dump files
.DESCRIPTION
 This script is used for searching all existence of *.dmp files on persistent disk of provisioned targert device.
.NOTES
 File Name : FindDumps.ps1
 Author : Jaroslaw Sobel
 Prerequisite : PowerShell V2
 Copyright 2014 (c) Jaroslaw Sobel, www.xenthusiast.com
.LINK
 Script posted over:
 http://xenthusiast.com/citrix-pvs-target-devices-searching-dump-files-with-powershell-script/

#>

# ---
# Configuration section
# ---

$WriteCacheDisk = "d:\*"
$dumpFiles = ("*.dmp")

$logname = "Application"
$source = "PVS_TD_Logs"
$eventId = 100

# ---
# Execution section
# ---

if ([System.Diagnostics.EventLog]::SourceExists($source) -eq $false)
{
 write-host "Creating event source $source on event log $logname"
 [System.Diagnostics.EventLog]::CreateEventSource($source, $logname)
 write-host -foregroundcolor green "Event source $source created"
}
else
{
 write-host -foregroundcolor yellow "Warning: Event source $source already exists. Cannot create this source on Event log $logname"
}

$fileList = ""
$fileFound = 0

if((Test-Path $WriteCacheDisk) -eq 1)
{
 $files = @(Get-ChildItem $WriteCacheDisk -include $dumpFiles -recurse)

 if($files.Count -gt 0)
 {
 foreach($file in $files)
 {
 $size = [math]::Round(($file.length/1048576),2)
 $fileList += "File: $($file.FullName), size: $size MB, creation date: $($file.CreationTime)`n"
 $fileFound++
 }
 }
}

$msg = ""
$hostname = $env:computername

if($fileFound -gt 0)
{
 $file = "file"
 if($fileFound -gt 1)
 {
 $file = "files"
 }

 $msg = "Found $fileFound $file on host $hostname `n$fileList`nPlease check and delete them"
 $type = "Warning"
 write-host -foregroundcolor yellow "`n$msg"
}
else
{
 $msg = "No DUMP files was found on D: disk on $hostname"
 $type = "Information"
 write-host -foregroundcolor green "`n$msg"
}

Write-EventLog -logname $logname -source $source -eventID $eventId -entrytype $type -message $msg -category 0

Post author

Leave a Reply