PowerShell

Here’s a quick list of Powershell commands I find myself using frequently. I’ll keep this updated.

Get a list of all brokered connections

I always sort by BrokeringTime because the order appears to be odd otherwise because the log is updated with the initial brokering time as well as the EndTime.

Add an application to multiple delivery groups

Adding an application to multiple desktop groups is 100% supported, it’s just not exposed in the GUI as of XenApp 7.5. Otherwise, if you try to add an application that already exists, you’ll get “Microsoft Word_1”. Also note when you do this, you can also set a priority to different desktop group- by default, the priority is added as zero – take a look:

To set a different priority, use the ‘-priority’ argument:

View the SQL DB info for the XenDesktop site

 

Monitoring Scripts

This script was used with a monitoring solution to return the count of unregistered machines, not in maintenance mode, powered on, and with a user assigned to the desktop:

 

This script returns the count of session disconnections for the last 5 minutes – note that the polling interval for this script would be every 5 minutes. If you wanted to poll every 1 minute for example, change to AddMinutes(-1):

Static Machine Addition

 
This script will get the ID of a VM, add it to the machine catalog with the CatalogUid of 3 and HypervisorConnectionUid of 2 (change these to match your needs); then assign a user and add to a catalog. Note a few variables used: $NewCluster and $NewVMName are specific to the path to the VM object; $NewVMName is the computer name of the machine.

 

XenDesktop Machines that have not been logged in to in the past XX days

 
This is for statically assigned VMs or PvD XD VMs, but it outputs nice human readable user names – requires AD PowerShell Snapin.

 

 


One-liner to get a listing of all users that have not logged into their XD desktop in 60 days or more – this is for static desktops or XD PvD.

Requires:

  • XenDesktop PowerShell SDK (studio commandlets)
  • AD Powershell commandlets

 

 

Notes:

  • Use MaxRecordCount for large environments
  • Replace “DOMAINHERE\” with your domain

Updating MDT OS Images with WSUS

 

 

Cleaning up Static XenDesktop VMs (including PowerCLI)

These series of cmdlets will remove the desktop from the delivery group and machine catalog, remove the object from AD, and delete the VM from vCenter. Replace ‘Domain\ComputerName’ with the appropriate strings; if you are not running on the DDC or not already pointed at it, make sure to add the ‘adminaddress’ parameter. *Note: these commands remove and delete things, so BE CAREFUL!

 

Creating a single text file from multiple files

This one-liner takes all text files in a directory and appends them all to a single new file.

 

 

Copying all users in one AD group to another AD group

Simple one-liner to copy users from one group to another – requires AD Powershell plugins.

 

Output all DFSN Targets from a common root

*Requires DFS tools

 

 

Basic IPMI Commands

Works on SuperMicro BMC IPMI on X9\X10 boards

 

 

 

2 thoughts on “PowerShell

  1. I wanted to run a question by you about the unregistered machines. We see the Orange Triangle on the unregistered VDI’s and I wrote a script similar to yours but check a couple extra options then reboots the machines.

    I’m just looking for validation that I am checking for the valid options. Do I need to use all of them?

    Below are the objects I query for to determine the status of the machines.

    FaultState -ne ‘None’
    -AND MachineInternalState -eq 'Unregistered'

    -AND PowerState -ne ‘Off’
    -AND InMaintenanceMode -ne 'True'

    -AND RegistrationState -ne ‘Registered’ `
    -AND SessionUserName -eq $null}|
    Select-Object HostedMachineName, DesktopGroupName

    • Rob, I’d say it depends on exactly what scenario you’re trying to prevent as to which parameters you need to check, but it sounds similar. I created that to integrate with PRTG and provide a count of unregistered machines…keep in mind this was before I deployed PVS, so all of the machines were statically assigned and the images would continually go nuts, thus causing unregistrations, etc.

Leave a Reply