get Citrix VDIs using Python instead of Powershell

Question:

I have to get the list(+ some parameters) of all the VDIs(Virtual Desktop Interfaces) which are connected to a Citrix Broker.

In PowerShell, I do it like this:

...
# credentials used for remote connection to citrix controllers
$pass=$(ConvertTo-SecureString -String $env:CitrixPassword -AsPlainText -Force)
[pscredential]$creds = New-Object System.Management.Automation.PSCredential ($env:CitrixUser,$pass)

#create the session
$session = New-PSSession -ComputerName $controller -Credential $creds -Authentication Negotiate

Write-Output -message "Session on $controller successfully established!"

Write-Output -message "loading citrix snapin"
Invoke-Command -Session $session -ScriptBlock {Add-PSSnapin citrix*} # pay attention here
Write-Output -message "Loading Snapin successful"

#get the stuff
Write-Output -message "Read the data..."
$controllers = Invoke-Command -Session $session -ScriptBlock {Get-BrokerController}
Write-Output -message "... controllers done"
$desktops = Invoke-Command -Session $session -ScriptBlock {Get-BrokerDesktop -MaxRecordCount 10000}
Write-Output -message "... desktop done"
...

I am struggling since some hours to find a solution for python, and I was testing a few stuff with python but nothing seems to be working. I have mostly played with WSman but I start to feel that this is not the right way… still not sure though.

My test looks like that:

...
# create the session
wsman = WSMan(citrix_ddc,
    username    = citrix_user,
    password    = citrix_pass,
    auth        = "basic",
    port        = 443,
    cert_validation = False)
    
with RunspacePool(wsman) as pool:
    ps = PowerShell(pool)
    ps.add_cmdlet("Add-PSSnapin").add_parameter("citrix*")
    ps.invoke()
    # we will print the first object returned back to us
    print(ps.output[0])

The endpoint seems to be wrong: https://<my_controller_ip>:443/wsman and the error is:

Code: 404, Content: <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN""http://www.w3.org/TR/html4/strict.dtd">
<HTML><HEAD><TITLE>Not Found</TITLE>
<META HTTP-EQUIV="Content-Type" Content="text/html; charset=us-ascii"></HEAD>
<BODY><h2>Not Found</h2>
<hr><p>HTTP Error 404. The requested resource is not found.</p>
</BODY></HTML>

This should be pretty similar with connecting with CmdLets to VMware or Nutanix, but I never did it with python and it seems that I can’t figure out how to do it.

Can someone please help me to find a python based solution to this?

Additional info:

  • the script will run in a docker container on a linux based machine.

Answers:

I have finally found an answer. The wsman is the right way to go, but when I was debugging the working powershell script using a debugger, I found out what I did wrong.

  • I should not be using SSL at all # this might not be the same for other situations, but it is for my particular case
  • because of previous mention, I had to remove the WSMan port
  • In PS script I was using auth=negotiate, therefore I did the same in Python.

All the rest the same, and it worked.

Here is my working code:

# create the session
wsman = WSMan(citrix_controller_ip,
    username        = citrix_user,
    password        = citrix_pass,
    ssl             = False,
    auth            = "negotiate",
    encryption      = 'never'
    )

with RunspacePool(wsman) as pool:
    ps = PowerShell(pool)
    ps.add_cmdlet('Add-PSSnapin').add_parameter('Name', 'Citrix*')
    ps.add_statement()
    ps.add_cmdlet("Invoke-Expression").add_parameter("Command", "Get-BrokerDesktop -MaxRecordCount 1000000 | ConvertTo-Json -Compress")
    ps.add_cmdlet("Out-String").add_parameter("Stream")
    ps.invoke()
    print(json.loads(ps.output[0])
Categories: questions Tags: , , ,
Answers are sorted by their score. The answer accepted by the question owner as the best is marked with
at the top-right corner.