Scripting zman in ZCM

Here's an example of scripting zman with Python.  I wanted to answer a seemingly simple question:  "How big a task will it be to migrate user sources".  At the moment I know of no simple way of transferring bundle assignments but here's how I scoped the size of the task.  This was performed on a ZCM 11 SP4 appliance. You will probably want this in a browser tab: ZMAN documentation (11 SP4)

Here's a link to the script for those who know what they are doing, below it there are step by step instructions for those who need to get the job done but are unfamiliar with Linux: bundle-assignments I've inserted it as a .txt file but it's really a Python script so rename it to .py if it makes you more comfortable.

Here's the text but please be careful if you copy and paste, in Python scripts whitespace (tabs and spaces) are meaningful. Download it instead and then check it matches the text below.
#!/usr/bin/env python 

# On ZCM 11 SP4 onwards, make sure to run this routine after starting
# the zman shell. It is *much* quicker
# Jon Gerdes, Blueloop Ltd 19 Aug 2015

import subprocess

# Create a list of lists with the bundle name and path in the first item of each list

bundles = []
rawbundles = subprocess.Popen(['zman', 'bl', '--terse', '-r'], stdout=subprocess.PIPE)

for s in iter(rawbundles.stdout.readline, ''):
l = s.split("|")

if l[1] == "Bundle":
bundles.append([l[3].strip() '/' l[0].strip()])
except IndexError:
# Now get the assignments for each bundle and fill them in the corresponding list
i = 0
n = len(bundles)
while i < n:
bundlename = bundles[i][0]
rawassign = subprocess.Popen(['zman', 'blas', bundlename, '--terse', '--typefilter=user'], stdout=subprocess.PIPE)
for s in iter(rawassign.stdout.readline, ''):
l = s.split("|")

if l[1] == "Folder" or l[1] == "User Group" or l[1] == "User":
bundles[i].extend([l[3].strip() '/' l[0].strip()])
except IndexError:
# Output bundle name followed by assignments (if any)
j = 1
m = len(bundles[i])
print bundlename
# Only run if there are actually any assignments
if m > 1:
while j < m:
print " " bundles[i][j]
j = 1
i = 1
print "Number of bundles = " str(n)

Assuming a ZCM 11 SP4 appliance, browse to and login as zenadmin.  You can start the SSH service (System Services) and then use ssh to do the job or there is a built in Terminal and File Explorer, linked under the ZENworks heading, which is what I'll describe here.

Find /home/zenadmin and upload the script (little icon that looks like an intray with an arrow pointing up.  You don't have to rename it because file extensions mean nothing in Linux.  The bit after #! at the start of the script tells Linux what it is and what to do with it.  It can help to put .py on the end of it, so you know what it is without reading it.

Now go to the terminal and type in "ls -l" (no quotes) and hit enter and you will see a file listing.  Now type "chmod u x".  If you hit tab after the bu the shell will fill in the rest for you.

The zman service makes the script run at least 10x faster, first you have to give it a login and password to use as each zman command in the script is run:
zman asc zcm_admin_account
zman ssas

Now run the script, you have to run it as the root user (not sure exactly why yet) which is why there is the sudo command at the front and you will be prompted for the root password:
sudo ./

After a few seconds you will start to see bundles and user assignments scroll up the screen. Go and make a coffee it will take some time.  If it appears to hang or you want to stop it, then use ctrl-c together to stop it.

This command will send the output to a file called bundle-assignments.lst, which you can then download afterwards.  Note that tee echos buffers to the screen so it will appear to hang for a minute or so until you see anything and then pause until the next chunk is ready.
sudo ./ | tee bundle-assignments.lst

The web based terminals will timeout after a while so don't leave it alone for too long.  You can click on the title bar and move it around to reset the timer.  Using a ssh client like PuTTY Download page does not have this problem.

You don't have to use Python for your scripting.  The appliance has BASH, PHP, Perl and Ruby (at least) available so choose your weapon and have a go.



How To-Best Practice
Comment List