:: PowerCLI | Remove VMs

:: PowerCLI | Remove VMs

Just a quick script from me that will help delete a bunch of VMs.
You need to have a list with all computer names in a .txt or .csv file for that.

Uncomment the get-credential part if you are not running the ISE with your admin that has access to the vCenter server.

.SYNOPSIS Removes a VM from View and vCenter 
.EXAMPLE get-content "list-of-machines.csv" | remove-vm.ps1 
.EXAMPLE remove-vm.ps1 vm1 
.EXAMPLE remove-vm.ps1 vm1, vm2, vm3 
.PARAMETER VM One or more Virtual Machine names 
[CmdletBinding()]param([Parameter(Mandatory=$True,ValueFromPipeline=$True)][string[]]$VM ) 

  $ErrorActionPreference = "Stop" 

  ForEach ($a in $VM) { 
    try { 
      remove-vm -vm $a -DeletePermanently -confirm:$false 
      Write-Output "$a successful!" 

catch { 
  $ErrorMessage = $_.Exception.Message 
  Write-Output $ErrorMessage 
  Write-Output "$a failed!" 

finally {} 
END {}

ViewCLI | Find users & assigned VM that did not login for more than 90 days

I didn’t find this anywhere else so i decided to create my own ViewCLI script.

Just run it against a View Event Database to get all information you need.

Please note:

If there is more than one machine assigned to a user it will show “System.Object[]” instead of a machine name.

You can still use “get-desktopVM | where { $_.user_displayname -eq “domain\username” } to list all machines in that case.

It actually results users that did login to the broker. This is enough for me as i don’t need the agent_login itself.

Just make sure you edit the sql query if you want to see agent connects.

Let me know if you have any questions or problems:


Check environment for users login times and get machines with no login since more than 90days.


#generate a timestamp that can be used in filename
Function logstamp {
if ($mo.length -lt 2) {
$mo="0"+$mo #pad single digit months with leading zero
if ($dy.length -lt 2) {
$dy="0"+$dy #pad single digit day with leading zero
if ($hr.length -lt 2) {
$hr="0"+$hr #pad single digit hour with leading zero
if ($mi.length -lt 2) {
$mi="0"+$mi #pad single digit minute with leading zero
Write-Output $yr$mo$dy$hr$mi

#variables - modify accordingly
$dbserver = ""
$user = ""
$pwd = read-host 'View Event DB Password' -AsSecureString
$database = "VIEWEVENTS"
$connectionString = "Server=$dbserver;uid=$user; pwd=$pwd;Database=$database;Integrated Security=False;"
$domain = ""
#this query actually gets all users that did not login between today and -90 days but before -90 days until -180 days.
#you could choose to increase or decrease the -180 as you want.
$query = 
SELECT ModuleAndEventText 
FROM VE_event_historical 
WHERE (EventType = 'BROKER_USERLOGGEDIN') AND (Time BETWEEN dateadd(day,-180,getdate()) AND dateadd(day,-90,getdate())) 
SELECT ModuleAndEventText 
FROM VE_event_historical 
WHERE (EventType = 'BROKER_USERLOGGEDIN') AND (Time BETWEEN dateadd(day,-90,getdate()) AND dateadd(day,0,getdate()))

$connection = New-Object System.Data.SqlClient.SqlConnection
$connection.ConnectionString = $connectionString
$command = $connection.CreateCommand()
$command.CommandText  = $query
$result = $command.ExecuteReader()
$table = new-object “System.Data.DataTable”

$properties = @{UserID = ''; VM=''}
$object = New-Object -TypeName PSObject -Property $properties


$table | foreach-object {
    $objloop = $object.PSObject.Copy()
    #make sure to edit the 2nd -replace to match your domain
    $objloop.UserID = $_.ModuleAndEventText -replace " has logged in", "" -replace "User domain\\", ""
    $objloop.VM = get-desktopvm | where { $_.user_displayname -eq "$domain\$($objloop.UserID)" } | Select-Object -exp Name
    $collection += $objloop

write-output "Logging output to InactiveDesktops_$timestamp.csv"

$timestamp = logstamp
$filename = "InactiveDesktops_$timestamp.csv"
$collection | Export-Csv $filename -NoTypeInformation



View Agent | The JVM has aborted

On View desktops that have HP QuickTest Professional installed, View Agent fails (2036437)


When HP QuickTest Professional software is installed on a View desktop, View Agent stops running and cannot communicate with View Connection Server. As a result, users cannot access the View desktop.
In the view agent logs, you see entries similar to:

T09:50:47.588-05:00 WARN (0870-0874) <Main Thread> [] Error occurred during initialization of VM
T09:50:47.588-05:00 WARN (0870-0874) <Main Thread> [] Could not find agent library jvmhook on the library path, with error: Can’t load IA 32-bit .dll on a AMD 64-bit platform
T09:50:47.588-05:00 WARN (0870-0874) <Main Thread> []
T09:50:47.588-05:00 FATAL (0870-0874) <Main Thread> [] The JVM has aborted.
T09:50:47.744-05:00 INFO (03E0-0838) <JavaBridge> [wsnm_jmsbridge] wsnm_jms died, restarting in a minute

Note: For more information on View Agent log locations, see Location of VMware View log files (1027744).

This issue occurs because HP QuickTest Professional sets the following environment variables on the desktop virtual machine:

When View Agent loads the jvm.dll, it attempts to load the libraries specified by these environment variables. When View Agent cannot load the specified libraries, it fails.


After installing HP QuickTest Professional, remove the following environment variables from the virtual machine that you will use to create desktop pools:

After removing these environment variables, install View Agent.

View | Unassign User PowerCLI

If you dont want to unassign all users by hand simply provide this script a .csv file with desktop names and it will do it for you.

$filepath = "desktops.csv"
import-csv $filepath -Header Desktop | Foreach-object {
remove-userownership -machine_id (Get-DesktopVM -name $_.Desktop).machine_id


Change & Grow VM Hard Disk

We are right now in the need of resizing all of our Hard Disks on the Virtual Desktop VMs from 40GB to 60GB.
I was looking quite some time to find the appropriate command and this is how you do it:

Get-HardDisk -vm "VMName" | where {$_.Name -eq "Hard Disk 1"} | Set-HardDisk –CapacityKB 62914560 -ResizeGuestPartition -Confirm:$false

A little bit of extra information (we ran into some issues performing this):

The second part of the script (Set-Harddisk) actually calls a library function that is using the “Invoke-VMScript” command.

You need to have following privileges on the vCenter System to execute it.
VirtualMachine.GuestOperations.Modify and VirtualMachine.GuestOperations.Execute.



This script makes it even more comfortable. You provide a list of desktops in the input file and it will do all of them in one run. After that you will see a report of the changes in the output file.

Import-Csv -Header Desktop "hdd-increase-input.csv" | foreach-object { Get-HardDisk -vm $_.Desktop | where {$_.Name -eq "Hard disk 1"} | Set-HardDisk –CapacityKB 62914560 -ResizeGuestPartition -Confirm:$false
Write-host "-----------------------------------------------------------------------------------------" -foregroundColor White -backgroundColor DarkGreen
} | export-csv "hdd-increase-output.csv"

New Job

Since the 1st of july i finally got into a very interesting position in my company.
I will now be responsible for support, reporting and analysis of a VMWare View virtual desktop infrastructure.
Starting off with a 90day training plan and a View 5.2 class room training i am looking forward to be on a good level pretty soon.