Additional “Quick and Dirty” PVS Write Cache / Reboot Idle XenDesktop Scripts


 

A few years ago I had an issue where I needed to check the size of .vdiskcache.  Times have changed and now instead of “.vdiskcache” we have “.vhdx” with our snazzy new “Cache in RAM with overflow” write cache option.  Here we have an updated better version.  You can output $results to something nice (like export-csv or something else).

 

Add-PSSnapin Citrix*
$siteControllers="site1Controller"
$results=@()
foreach($controller in $siteControllers)
{
$reg=get-brokerdesktop -MaxRecordCount 9999 -RegistrationState Registered -adminaddress $controller

$reg |where-object {$_.AssociatedUserNames.count -lt 1} | %{
$comp=$_.hostedmachinename;
Write-host "Checking $comp ..." -ForegroundColor Yellow
$cache=dir "\\$comp\D`$\vdiskdif.vhdx" -force;
$OS= (gwmi win32_operatingSystem -ComputerName $comp)
$BootTime = $OS.ConvertToDateTime($OS.LastBootUpTime)
$Uptime = $OS.ConvertToDateTime($OS.LocalDateTime) - $boottime
$compResult= New-Object -type PSObject -property @{
'Computer'= $comp
'CacheLength' = $($($cache.length)/1GB)
'Uptime' = $Uptime
}
$results+=$compResult

}
}

 

You might, instead, want to take said machines that are ready to, or have already, fall(en) over and reboot them.  But we don’t want to nuke machines with users!
asnp Citrix*

$siteControllers="site1Controller","site2Controller"
foreach ($controller in $siteControllers)
{
$collection=get-brokerdesktop -maxrecordcount 9999 -adminaddress $controller  | where-object {($_.AssociatedUserNames).Count -lt 1 }
write-host "Got $($collection.count) machines"

$interval=100 #How many machines in a batch.  Limited to XenDesktop power settings, max "actions per minute" under Hosting/Properties/Advanced!
$seconds=30
$index=0
$indexCheck=0
while ($index -lt $collection.count)
{
$LogSuccessResult=$false
$logging=Start-LogHighLevelOperation -Text "Resetting next $interval idle machines to refresh. $index of $($collection.count) " -source "Script" -OperationType AdminActivity -AdminAddress $controller
try{
for (;$index -lt ($indexCheck+$interval) -and $index -lt $collection.Count; $index++)
{


if( (Get-BrokerMachine -HostedMachineName $($collection[$index]).HostedMachineName -AdminAddress $controller).AssociatedUserNames.Count -lt 1)
{
write-host "Trying to reset $($($collection[$index]).HostedMachineName)"
New-BrokerHostingPowerAction -Action Reset -MachineName $($collection[$index]).HostedMachineName -AdminAddress $controller -LoggingId $logging.id | Select hostedmachinename,state,requesttime
}

}
$LogSuccessResult=$true
}
catch {$LogSuccessResult=$false}
Stop-LogHighLevelOperation -HighLevelOperationId $logging.Id -AdminAddress $controller -IsSuccessful $LogSuccessResult

write-host "Shhh, go to sleep little babe for $seconds seconds" -ForegroundColor Yellow
start-sleep $seconds
$indexCheck=$index
}
}

Leave a comment

Your email address will not be published.