Pawel Serwan Blog

Citrix, Microsoft and other stuff

[PowerShell] Generate report of your Citrix policies

6 Comments

CitrixPoliciesSome time ago I needed to create report of Citrix policies configured in my Citrix XenApp 6.5 farm. I thought I can simply do that using Citrix PowerShell commands. But it occurred that it is not so simple. Other requirement was to have the report created in Word format πŸ™‚ So I started to write a simple script that in the end does what I needed: it lists your Citrix policies, checks their configuration and then generates a nice Word document. Below you can find the source code of the script. I hope you will like it and find it useful.

<#
.NAME:
Get-XAPoliciesReport.ps1
.VERSION:
1.0.1 - 15/02/2017 - initial script
1.2.0 - 17/02/2017 - final version
.DESCRIPTION
This script generates a configuration report for Citrix policies defined in Citrix XenApp 6.5 environment in Word document format.
.PARAMETER
.REQUIREMENTS
Citrix XenApp Commands/SDK has to be installed on the server. Might require Citrix Scout installation to get Citrix.GroupPolicy.Commands.psm1
.NOTES
This script was tested on Windows Server 2008 R2/PowerShell 2.0 in Citrix XenApp 6.5 farm. Not checked in XenApp/XenDesktop 7.x
.AUTHOR
Pawel Serwan, https://pawelserwan.wordpress.com/
#>
#Load XenApp Commands snapins (if necessary)
if ( (Get-PSSnapin -Name Citrix.Common.GroupPolicy -ErrorAction SilentlyContinue) -eq $null ) { Add-PSSnapin Citrix.Common.GroupPolicy }
if ( (Get-PSSnapin -Name Citrix.Common.Commands -ErrorAction SilentlyContinue) -eq $null ) { Add-PSSnapin Citrix.Common.Commands }
if ( (Get-PSSnapin -Name Citrix.XenApp.Commands -ErrorAction SilentlyContinue) -eq $null ) { Add-PSSnapin Citrix.XenApp.Commands }

#import PowerShell module from Scout folder - required to retrieve list of Citrix policies - Please provide path!
Import-Module -Name "D:\Scout\Current\Utilities\Citrix.GroupPolicy.Commands.psm1"

#Initalize farm
$farm = Get-XAFarm

#Output list of Current Policies and prompt for one to create a document and backup
$objCtxPolicies = Get-CtxGroupPolicyConfiguration

#Set variables
$docpath = "C:\temp"
$ComputerName = $env:computername
$username = $env:username
$date = Get-Date –format dd-MM-yyyy

#Test path & create if not present
if (!(Test-path -path $docpath)) { new-item $docpath -type directory | out-null }

# Create new Word document
[ref]$SaveFormat = "microsoft.office.interop.word.WdSaveFormat" -as [type]
$objWord = New-Object -COMObject Word.Application
$objWord.Visible = $True
$objDocument = $objWord.Documents.Add()
$objSelection = $objWord.selection
$objSelection.WholeStory
$objSelection.Style = 'Title'
$objSelection.TypeText("Configuration Report for Citrix policies in Citrix XenApp $($farm.ServerVersion) farm: $($farm.FarmName)")
$objSelection.TypeParagraph()
$objSelection.Style = 'Heading 2'
$objSelection.typeText("Report compiled on $date by $username on computer: $ComputerName")

$objDocument.saveas([ref] $docpath, [ref]$saveFormat::wdFormatDocument)

foreach ($objCtxPolicy in $objCtxPolicies)
{
    $arrayCtxPolicy = @()
    #write Citrix Policy details into WORD document
    $objSelection.TypeParagraph()
    $objSelection.Style = 'Heading 1'
    $objSelection.typeText("Configuration Report for Citrix policy: $($objCTXpolicy.PolicyName)/$($objCTXpolicy.Type)")
    $objSelection.TypeParagraph()

    #Going through all enabled settings
    foreach ($objCtxPolicyProperty in @($objCtxPolicy | Get-Member -Type NoteProperty | Select -Expand Name))
    {
        $config = $objCtxPolicy.$objCtxPolicyProperty

        if ($config.State -ne "NotConfigured")
        {
            $CTXconfPolicy = New-Object PSObject
            if (Get-Member -InputObject $config -Name Value)
            {
                $CtxConfPolicy | Add-Member -MemberType NoteProperty -Name ConfiguredSetting -Value "$($config.Path)" -PassThru | Add-Member -MemberType NoteProperty -Name ConfiguredValue "$($config.Value)" -PassThru
            }
            else
            {
                $CtxConfPolicy | Add-Member -MemberType NoteProperty -Name ConfiguredSetting -Value "$($config.Path)" -PassThru | Add-Member -MemberType NoteProperty -Name ConfiguredValue "$($config.State)" -PassThru
            }
            #removing empty spaces and blank entries
            if ($CTXconfPolicy.ConfiguredSetting -ne "")
            {
                $arrayCtxPolicy += $CtxConfPolicy
            }

        }
    }
    $outpol = $arrayCTXpolicy
    #adding table with configuration of Citrix policy into Word document
    $objTableColumns = $CTXconfPolicy | gm -MemberType NoteProperty | select name
    $objNumberOfColumns = $objTableColumns.Count
    $objNumberOfRows = $arrayCtxPolicy.Count + 1
    $x=2
    #$objRange = $objDocument.Range()

    $Table = $objDocument.Tables.Add($objSelection.Range,$objNumberOfRows,$objNumberOfColumns, [Microsoft.Office.Interop.Word.WdDefaultTableBehavior]::wdWord9TableBehavior,
    [Microsoft.Office.Interop.Word.WdAutoFitBehavior]::wdAutoFitContent)

    $Table.Style = "Medium Shading 1 - Accent 1"

     #Putting headers
    for ($i=1; $i -le ($objNumberOfColumns); $i++)
    {
        $Table.cell(1,$i).range.Bold=1
        $Table.Cell(1,$i).Range.Text = "$($objTableColumns[$i-1].name)"
    }

    for ($a=2; $a -le $objNumberOfRows; $a++)
    {
        for ($b=1; $b -le $objNumberOfColumns; $b++)
        {
            $Table.Cell($a,$b).Range.Text = "$($arrayCtxPolicy[$a-2].$($objTableColumns[$b-1].name))"
        }
    }
    #closing table to start writing in lines now
    $objSelection.Start = $objDocument.Content.End
    $objSelection.TypeParagraph()

    $policyfilter = Get-CtxGroupPolicyFilter -PolicyName $objCtxPolicy.PolicyName
    if ($policyfilter -ne $null)
    {
        $objSelection.Font.Bold = $True
        $objSelection.TypeText($policyfilter.PolicyName + " policy applied to:")
        $objSelection.Font.Bold = $False

        $outpolfilter = $policyfilter | Out-String
        $objSelection.TypeText("" + $outpolfilter)
     }
     else
     {
        $objSelection.Font.Bold = $True
        $objSelection.TypeText("Citrix Policy Filter: " + $objCtxPolicy.PolicyName +  " doesn't have any policy filter applied.")
        $objSelection.Font.Bold = $False
        $objSelection.TypeParagraph()
     }
}

$doctitle = "Citrix Policies - Configuration Report for Citrix farm - $($farm.FarmName)"
if (Test-Path -Path "$docpath\$doctitle.doc")
{
    $savepath = "$docpath\$doctitle" + "_$date" +".doc"
}
else
{
    $savepath = "$docpath\$doctitle.doc"
}
$objDocument.SaveAs([ref]$savepath,[ref]$SaveFormat::wdFormatDocument)
$objDocument.Close()
$objWord.Quit() 

Advertisements

6 thoughts on “[PowerShell] Generate report of your Citrix policies

  1. http://carlwebster.com/downloads/download-info/xenapp-6-5/

    Will give you all the policy details along with a lot of other info.

    Like

    • Thanks Carl for comment! I know your scripts and I used them many times. This time however I needed something that will not query and document whole XenApp farm but only Citrix policies. And to get that report quicker.

      Like

      • If all you needed was the Policy information, there is a -Section parameter you can use: -Section Policies. That is even one of the examples in the help text.

        ————————– EXAMPLE 12 ————————–

        PS C:\PSScript >.\XA65_Inventory_V43.ps1 -Section Policies

        Will use all Default values.
        HKEY_CURRENT_USER\Software\Microsoft\Office\Common\UserInfo\CompanyName=”Carl Webster” or
        HKEY_CURRENT_USER\Software\Microsoft\Office\Common\UserInfo\Company=”Carl Webster”
        $env:username = Administrator

        Carl Webster for the Company Name.
        Sideline for the Cover Page format.
        Administrator for the User Name.
        Processes only the Policies section of the report.

        You can even email the report:

        ————————– EXAMPLE 16 ————————–

        PS C:\PSScript >.\XA65_Inventory_V43.ps1 -SmtpServer mail.domain.tld -From XDAdmin@domain.tld -To
        ITGroup@domain.tld

        Will use all Default values.
        HKEY_CURRENT_USER\Software\Microsoft\Office\Common\UserInfo\CompanyName=”Carl Webster” or
        HKEY_CURRENT_USER\Software\Microsoft\Office\Common\UserInfo\Company=”Carl Webster”
        $env:username = Administrator

        Carl Webster for the Company Name.
        Sideline for the Cover Page format.
        Administrator for the User Name.

        Script will use the email server mail.domain.tld, sending from XDAdmin@domain.tld, sending to ITGroup@domain.tld.
        Script will use the default SMPTP port 25 and will not use SSL.
        If the current user’s credentials are not valid to send email, the user will be prompted to enter valid credentials.

        ————————– EXAMPLE 17 ————————–

        PS C:\PSScript >.\XA65_Inventory_V43.ps1 -SmtpServer smtp.office365.com -SmtpPort 587 -UseSSL -From
        Webster@CarlWebster.com -To ITGroup@CarlWebster.com

        Will use all Default values.
        HKEY_CURRENT_USER\Software\Microsoft\Office\Common\UserInfo\CompanyName=”Carl Webster” or
        HKEY_CURRENT_USER\Software\Microsoft\Office\Common\UserInfo\Company=”Carl Webster”
        $env:username = Administrator

        Carl Webster for the Company Name.
        Sideline for the Cover Page format.
        Administrator for the User Name.

        Script will use the email server smtp.office365.com on port 587 using SSL, sending from webster@carlwebster.com,
        sending to ITGroup@carlwebster.com.
        If the current user’s credentials are not valid to send email, the user will be prompted to enter valid credentials.

        Like

      • thanks Carl! To be honest I didn’t know about it πŸ™‚ and it was some time ago when I used your script last time. Still developing my own script was very educational (Word document creation part) and gave me a lot of fun. And just giving back to community the knowledge I take πŸ™‚

        Like

  2. Hello!!

    Thanks for sharing, I was trying to use your script, but I get some errors, do you have any idea of what could be the problem?

    You can dowloand the output of my test here:

    https://aisfiles.sharefile.com/d-s5565d78f4534ab09

    Thanks in advanced!!

    Like

    • hi Hector,

      thanks for using the script πŸ™‚ I checked the output I have two questions:
      1. Do you have Word installed on the Citrix XenApp server on which you launch the script?
      2. Do you launch it in PowerShell (x86)?

      Like

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s