Pywinrm和Active Directory PowerShell cmdlet

时间:2017-07-25 14:38:24

标签: python powershell active-directory cmdlets kerberos-delegation

我遇到了一个使用pywinrm模块的Python代码的奇怪问题。 让我解释一下。我有一个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 ...您是否有过此主题的经验?

感谢您的帮助。

1 个答案:

答案 0 :(得分:1)

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