Blog posts tagged with 'scripting'


22 Sep 2008 04:35 AM EDT
[ Tags: xendesktop,  powershell,  scripting,  sdk,  vdi,  workflow studio ]

The Citrix Desktop Delivery Controller PowerShell SDK provides a snap-in, XDCommands, for the Microsoft PowerShell v1.0 framework. The SDK consists of a number of "cmdlets" that allow you to script many of the administrative tasks you may need to perform on a regular basis.

Step by Step setting up your PowerShell / XenDesktop environment

  • Install PowerShell 1.0 on the DDC (Desktop Delivery Controller)
  • Download and Install Microsoft .NET Framework 3.5
  • The PowerShell SDK is located on the XenDesktop 2.1 CD in the ...\Support\DdcSdk folder. Run the installer XenDesktop_2_0_DDC_Powershell_SDK.msi

Installing the SDK registers the XdCommands snap-in assembly with the Microsoft PowerShell framework. The snap-in makes a number of new classes and "cmdlets" available to PowerShell scripts or interactive shell sessions.

To run scripts you may need to use the built-in "Set-ExecutionPolicy" cmdlet to adjust the PowerShell execution policy to a value such as "RemoteSigned"

  • Start Powershell and set the Excecution Policy. Set-ExecutionPolicy RemoteSigned
  • Change to the folder where the SDK is installed cd \Program Files\Citrix\Desktop Delivery Controller\Powershell
  • Load the snap-in into the PowerShell  Add-PSSnapin XdCommands

Alternatively, use the installed PowerShell console file, XdCommands.psc1, to start an interactive PowerShell shell session with the XdCommands snap-in pre-loaded. Citrix provides a shortcut on the Start menu to start such a session. This shortcut also runs the "XdAliases.ps1" PowerShell script that sets up aliases for most of the SDK cmdlets. This shortcut will not function properly until the PowerShell execution policy, as described above, is set appropriately.

Help

Online help is available for all Desktop Delivery Controller SDK cmdlets. To obtain a list of cmdlets offered by the snap-in, run the built-in "Get-Command" cmdlet, as follows: Get-Command -psSnapin XdCommands
Online help for individual cmdlets is available using the built-in "Get-Help" cmdlet. For example, to view the online help for the "Get-XdDesktopGroup" cmdlet, run the following command: Get-Help Get-XdDesktopGroup
For an overview of all cmdlets provided by the SDK, view the "about_XdCommands" help topic. To view this information, run the following command: Get-Help about_XdCommands

Samples

Creating a new VM-based desktop group

This command creates a new VM-based desktop group, "testgrp", containing three machines, and published to all domain users.
$usr = New-XdUser 'domain users' -group
$cred = Get-Credential 'root'
$hs = New-XdHostingServer 'XDS01' $cred
$machineName= 'machine1','machine2','machine3'
#find all the VM machines in the pool
$allvms = Get-XdHostedMachine $hs
#Find the workers and set the AD identity to the correct machine
$dsk = $machineName | foreach { $vm=$_; $allvms | where {$_.HostingName -match $vm } | foreach { $_.Name = $vm; $_ }}
$hgs = New-XdGroupHostingSettings $hs
$ng = New-XdDesktopGroup -pub 'testgrp' -desk $dsk -user $usr -hosting $hgs


Adding a virtual desktop to an existing VM-based desktop group

This command adds a new virtual desktop, hosted by a VM, to an existing VM-based desktop group. Before adding a VM to the group, you must create a mapping between the VMs host ID and Active Directory ID. To do this, run the Get-XdHostedMachine cmdlet to obtain a list of host IDs for VMs and assign Active Directory IDs to those VMs.

# get all the groups whose name starts with 'test' (should be just one)
$grp = Get-XdDesktopGroup test*
# get all the workers whose friendly names have 'machine3' in them (should be just one)
$dsk= Get-XdHostedMachine $grp.HostingSettings.HostingServer -name *machine3*
# Set up the mapping to the AD name for the new Virtual Desktop machine
$dsk.Name = 'machine3'
$grp.Desktops.Add($dsk)
Set-XdDesktopGroup $grp

If host ID to Active Directory ID mappings have been created previously, run the following command:

Get-XdDesktopGroup test* | *%* { \[void\]$\_.Desktops.Add($(Get-XdHostedMachine $\_.HostingSettings.HostingServer \-name \*machine3\*)); $\_ }| Set-XdDesktopGroup

Logging off a user from all current sessions, after sending a warning message

This command displays a warning message to all users whose names start with "christian" before logging them off. Note that in this example there is specified time period (10 seconds) before logoff occurs.

# get sessions for all users whose names start with 'christian'
$sess = Get-XdSession -user christian*
# warn the user
Send-XdSessionMessage $sess 'Forced log off in 10 seconds'
Start-Sleep 10
#Then go ahead with the logoff
Stop-XdSession $sess

Adding a user to an existing desktop group

This command adds users in all groups whose names match "GroupName" to an existing desktop group.
# get all the groups whose name matches 'GroupName' (should be just one)
# Note could also be written as:

#  $grp = Get-XdDesktopGroup GroupName
$grp = Get-XdDesktopGroup | ? {$_.Name -match "GroupName" }
$Usr = New-XdUser "UserName"
$grp.Users.Add($Usr)
Set-XdDesktopGroup $grp


Expand Blog Post
27 Aug 2008 05:34 AM EDT
[ Tags: powershell,  mfcom,  scripting,  xenapp,  workflow studio ]

In my last blog I have started with the Introduction of PowerShell and MFCom. Today we'll provide you more information's about MFCom Objects and how to use them with PowerShell.

Check Session State


To retrieve the session state for each session within the Citrix farm you have to query IMetaFrameSession.SessionState. If the returned state of the session is MFSessionStateStale, no further attempt to read other session properties should be made. This state indicates that the session may no longer exist.
The following PowerShell script displays all active sessions within the farm and adds additional session information to the output. To check other session's states you only have to replace the session state type in the last line of the code.

#Type Definitions
$MetaFrameWinFarmObject =
 

$MFSessionStateUnknown = 0         #Unknown state
$MFSessionStateActive = 1          #User logged on
$MFSessionStateConnected = 2       #Connected to client
$MFSessionStateConnecting = 3      #Connecting to client
$MFSessionStateShadowing = 4       #Shadowing another session
$MFSessionStateDisconnected = 5    #Logged on but no client
$MFSessionStateIdle = 6            #Waiting for connection
$MFSessionStateListening = 7       #Listening for connection
$MFSessionStateResetting = 8       #Reset in progress
$MFSessionStateDown = 9            #Down due to error
$MFSessionStateInit = 10           #Initializing
$MFSessionStateStale = 11          #Stale session object
 

#Main
$farm = new-Object -com "MetaframeCOM.MetaframeFarm"
$farm.Initialize($MetaFrameWinFarmObject)
$farm.FarmName

$farm.Sessions | Where-Object { $_.SessionState -eq $MFSessionStateActive {color:black}}| Format-table Username,SessionName,AppName,ServerName,SessionState

Find disabled Application      

Finding disabled applications is mostly useful in your production or test environment where you have to handle with many published applications. This property IMetaFrameApplication.EnableApp enables or disables the application.

  • When you publish an application, it is enabled by default. Enabled applications are available to the users specified when the application was published. Disabled applications are not available to users.
  • The application can become disabled internally if its server list becomes empty, or if its user list becomes empty and the application is not configured to accept anonymous connections.
  • A disabled application is not available to clients.

#Type Definitions
$MetaFrameWinFarmObject = 1

#Main
$farm = new-Object -com "MetaFrameCOM.MetaframeFarm"
$farm.Initialize($MetaFrameWinFarmObject)
$farm.FarmName
$app = $farm.Applications
$app | foreach { $_ | ? { $_.EnableApp -eq 0 }}|Format-Table DistinguishedName

 

Expand Blog Post
25 Jul 2008 04:13 AM EDT
[ Tags: powershell,  mfcom,  scripting,  sdk ]

Microsoft Windows PowerShell command line shell and scripting language helps IT professionals achieve greater control and productivity. Using a new admin-focused scripting language, more than 130 standard command line tools, and consistent syntax and utilities, Windows PowerShell allows IT professionals to control system administration and accelerate automation more easily

With PowerShell, Citrix Administrators can script MFCom Objects to manage and administer  the XenApp Farm. The secret of using COM objects starts with the command: New-Object -COM.

The following PowerShell example creates a new MetaFrame object (do not get confused with the COM Object naming), initializes the Farm and prints out the farmname:

$farm = new-Object -com "MetaframeCOM.MetaframeFarm"
$farm.Initialize(1)
$farm.FarmName

It's not going to be a spectacular script. But look at the following little code enhancement:

$farm = new-Object -com "MetaframeCOM.MetaframeFarm"
$farm.Initialize(1)
$farm.FarmName
$farm.sessions | Format-Table UserName,ClientAddress

Just adding one more lines of code and you will get all sessions within the farm displaying the Username and IP Address.

Setting up your PowerShell / MFCom environment

Beginning  with PowerShell / MFCom Scripting you should install Microsoft Powershell on a Citrix Presentation / XenApp Server in your lab. I recommend downloading the PowerShell Graphical Helpfile which also provides great information's about VBScript to PowerShell conversion.

For creating and editing your PowerShell scripts I suggest downloading the free PowerGui graphical user interface and script editor. Its easy to use and works well with COM Objects.

PowerShell
http://www.microsoft.com/windowsserver2003/technologies/management/powershell/download.mspx

PowerShell Graphical Help File
http://www.microsoft.com/downloads/details.aspx?FamilyId=3B3F7CE4-43EA-4A21-90CC-966A7FC6C6E8&displaylang=en

PowerGui - Graphical user interface and script editor
http://www.powergui.org

Displaying apps in your farm

To give you some basic ideas where PowerShell leverages your daily administrative tasks, I've created the following script:
$farm = new-Object -com "MetaframeCOM.MetaframeFarm"
$farm.Initialize(1)
$farm.FarmName
$farm.applications| where {$_.BrowserName -like "Winword*" {color:black}} | select DistinguishedName

The script above enumerates each application published in the farm and selects all applications where Winword* is contained in the BrowserName.

PowerShell Examples provided by CDN

PowerShell and other scripting examples can be found on the Citrix Developer Network:

http://community.citrix.com/label/cdn/powershell

Expand Blog Post