使用Python而不是Powershell获取Citrix vdi



我必须获得连接到Citrix Broker的所有vdi(虚拟桌面接口)的列表(+一些参数)。

在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"
...

我一直在努力寻找python的解决方案,我正在用python测试一些东西,但似乎没有任何工作。我大部分时间都在玩WSman,但我开始觉得这不是正确的方式……但还是不确定。

我的测试是这样的:

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

端点似乎是错误的: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>

这应该与使用CmdLets连接到VMware或Nutanix非常相似,但我从来没有用python做过,似乎我不知道如何做到这一点。

有人能帮我找到一个基于python的解决方案吗?

额外的信息:

  • 脚本将在linux机器的docker容器中运行。
我终于找到了答案。wsman是正确的方法,但是当我使用调试器调试工作的powershell脚本时,我发现我做错了什么。
  • 我根本不应该使用SSL #这可能与其他情况不同,但这是我的特殊情况
  • 由于前面提到的,我不得不删除WSMan端口
  • 在PS脚本中我使用auth=negotiate,因此我在Python中做了同样的事情。

其余的都一样,而且有效。

下面是我的工作代码:
# 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])

最新更新