You can customize and use the following sample script to delete machines that are not in a desktop pool from the Connection Server LDAP database.

This is a PowerShell script and does not require PowerCLI to be installed. To run the script, log in to View Connection Server as an administrator, create the script and input files, and run the script from a PowerShell window. If a machine in the input file is in a desktop pool, this script displays a message and does not delete it from the LDAP database.

Note:

Before running the script, take a snapshot of the Connection Server virtual machine in case it becomes necessary to undo the changes from running the script.

To copy and paste the script content without page breaks, use the HTML version of this topic, available from the Horizon 6 documentation page at https://www.vmware.com/support/pubs/view_pubs.html.

Script Input

The input file is of type csv and contains the names of the machines to be deleted. The following example shows what the input file may contain.

MachineName
linux-001
linux-002
linux-003

The sample script assumes that the name of this input file is DeleteMachineItems.csv and that the file is located in the same folder as the scripts.

Script Content

<#
bulk Delete registered machine items from broker LDAP database.
#>

$csvFile = '.\DeleteMachineItems.csv'

#check if file exists
if (!(Test-Path $csvFile))
{
	write-host  -ForeGroundColor Red "CSV File $CSVFile not found"
	exit
}

function IsNameInCsv($name)
{
    $csvData = Import-CSV $csvFile;
    foreach ($line in $csvData)
    {
        if($line.MachineName -eq $name)
        {
            Write-Host "$name exists in csv"
            return $true;
        }
    }
    Write-Host "$name does not exists in csv"
    return $false;
}

$obj = [adsi]'LDAP://localhost:389/OU=Servers,DC=vdi,DC=vmware,DC=int';
foreach ($child in $obj.psbase.Children)
{
  $machineName = $child.get("pae-DisplayName"); 
  if(IsNameInCsv($machineName))
  {
      $machineAttributes = $child  | get-member;
      $machineAttributes = $machineAttributes -join ' ';
      if ($machineAttributes -like "*pae-MemberDNOf*")
      {
          $desktopPool = $child.get("pae-MemberDNOf");
          Write-Host "Skip $machineName, which is included by desktop pool $desktopPool";
      }
      else
      {
          Write-Host "Delete $machineName, which is in csv file and not included by any desktop pool";
          $child.distinguishedName;
          $child.DeleteObject(0);
      }  
      
  }
  else
  {
      Write-Host "Skip $machineName, which is not in csv file";  
  }
  Write-Host '--------------------------------------';
}