简体   繁体   English

使用 Python 而不是 Powershell 获取 Citrix VDI

[英]get Citrix VDIs using Python instead of Powershell

I have to get the list(+ some parameters) of all the VDIs(Virtual Desktop Interfaces) which are connected to a Citrix Broker.我必须获取连接到 Citrix Broker 的所有 VDI(虚拟桌面接口)的列表(+ 一些参数)。

In PowerShell, I do it like this:在 PowerShell 中,我是这样做的:

...
# 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.几个小时以来,我一直在努力寻找 python 的解决方案,我正在用 python 测试一些东西,但似乎没有任何效果。 I have mostly played with WSman but I start to feel that this is not the right way... still not sure though.我大部分时间都使用 WSman,但我开始觉得这不是正确的方法……但仍然不确定。

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:端点似乎是错误的: https://<my_controller_ip>:443/wsman错误是:

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.这应该与使用 CmdLets 连接到 VMware 或 Nutanix 非常相似,但我从来没有用 python 做过,而且我似乎不知道该怎么做。

Can someone please help me to find a python based solution to this?有人可以帮我找到一个基于 python 的解决方案吗?

Additional info:附加信息:

  • the script will run in a docker container on a linux based machine.该脚本将在基于 linux 的机器上的 docker 容器中运行。

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. wsman 是 go 的正确方法,但是当我使用调试器调试工作 powershell 脚本时,我发现我做错了什么。

  • I should not be using SSL at all # this might not be the same for other situations, but it is for my particular case我根本不应该使用 SSL # 这在其他情况下可能不一样,但它适用于我的特殊情况
  • because of previous mention, I had to remove the WSMan port由于前面提到,我不得不删除 WSMan 端口
  • In PS script I was using auth=negotiate, therefore I did the same in Python.在 PS 脚本中,我使用了 auth=negotiate,因此我在 Python 中做了同样的事情。

All the rest the same, and it worked.所有 rest 都一样,而且有效。

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])

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM