我的Python代码使用pywinrm模块遇到一个奇怪的问题。
让我解释一下。我有一台Linux服务器,在其中启动以下python脚本:

import winrm

"""Create security group"""
s = winrm.Session('https://servername:5986/wsman',
   auth=(None, None), transport='kerberos',
   server_cert_validation='ignore')

name = "test"
path = "OU=Security Groups,DC=test,DC=org"

ps_command = 'New-ADGroup -Name "{0}"
-GroupScope Universal
-GroupCategory Security
-Path "{1}" -Server ldap.test.org'.format(name, path)

r = s.run_ps(ps_command)

if r.status_code == 0 :
    print(r.std_out.decode('UTF-8'))
else:
    print(r.std_err('UTF-8'))

该服务器将连接Windows服务器(不是DC)的HTTPS侦听器,然后启动组创建命令。

当我直接在Windows服务器上启动AD cmdlet时,它运行良好,并且在AD中创建了安全组。但是通过脚本,我得到以下响应:
$ python3 test_winrm.py
New-ADGroup : Unable to contact the server. This may be because this server does not exist, it is currently down,
or it does not have the Active Directory Web Services running.
At line:1 char:1
+ New-ADGroup -Name "test" -GroupScope Universal -GroupCategory Security
-Path "O ...
+
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
~~~
+ CategoryInfo          : ResourceUnavailable: (:) [New-ADGroup], ADServer
DownException
+ FullyQualifiedErrorId : ActiveDirectoryServer:0,Microsoft.ActiveDirector
y.Management.Commands.NewADGroup

我还想注意到,如果我用一个基本命令替换当前的PowerShell命令(例如,在Windows服务器上创建一个文件夹),它将起作用。

因此,即使安装了RSAT,它也可以在Windows服务器上本地运行,但不能与AD cmdlet一起运行。

谢谢您的帮助。

最佳答案

非常感谢@BenH的帮助,您对问题的来源有所了解,经过几天/头痛之后,我终于在这里找到解决方案:https://github.com/diyan/pywinrm/issues/58
当使用kerberos和pywinrm时,必须设置kerberos_delegation=True来支持多跳。

关于python - Pywinrm和Active Directory PowerShell cmdlet,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/45306504/

10-12 22:26