This is an old revision of the document!


How to write scripts for WinRM + AD Connector

This page main purpose is to inform developers how to write python and powershell script for our WinRM + AD Connector

Before you start with scripts it's good to know how this connector is working and what is his purpose so please follow the link above a read some information about the connector.

Python

The best way how to show what's the basic logic of python scripts is to show it at some simple example script with additional comments.

This is example create script

#!/usr/bin/env python
# -*- coding: utf-8 -*-
# first line is just shebang line and second is for defining Python source code encodings
# File should be in UTF-8
# Be careful about line ending. If you are using connector server on Linux environment be sure that python script has LF ending otherwise python will not be executed

# All params from IdM is stored in environment of this script and you can get them by os.environ["paramName"]
# For getting attribute you can use prepared method winrm_wrapper.getParam("paramName") which has the benefit of returning unicode value and in case the attribute is not existing
# in environment it will not fail but return empty string

import sys, os
# this is needed for importing file winrm_wrapper from parent dir
sys.path.append(os.path.join(os.path.dirname(__file__), '..'))
import winrm_wrapper
import json

# Getting attribute which is marked as identifier
uid = winrm_wrapper.getParam("sAMAccountName")

# For logging you can use prepared method from wrapper
winrm_wrapper.writeLog("Create start for " + uid)

# Parse multi valued attribute to list
if winrm_wrapper.getParam("ldapGroups"):
    roles = json.loads(winrm_wrapper.getParam("ldapGroups"))

# Load PS script from file and replace params
winrm_wrapper.writeLog("loading script")
f = open(os.environ["script"], "r")
command = f.read()
command = command.replace("$uid", uid)

# Call wrapper
winrm_wrapper.executeScript(os.environ["endpoint"], os.environ["authentication"], os.environ["user"],
                                    os.environ["password"], command, uid)

winrm_wrapper.writeLog("Create end for " + uid)
print("__UID__=" + uid)
sys.exit()

Powershell

  • by kucerar