Saturday, January 19, 2008

Dctm-PoSh Integration - Chapter 1

December 17, 2006 - I get a new job at a company in IT for the first time in my life. My official position? "That Documentum Guy." What is Documentum, you might ask? Only one of the most sophisticated pieces of software out there for document management. It is produced by EMC Corp. My job? Make document management happen at my company. And how much Documentum experience had I had? Well, I'd never heard of Documentum. It was going to be a challenge.

Any sysadmin is going to want to be able to effect change on huge amounts of files, programmatically. And how much programming experience had I had? Very little. Batch file programming was my specialty. I remember when I was about 12 making a batch file menu, that would boot the computer with certain options for certain environments that I needed. So, I started out writing Documentum scripts in batch files. Not very scalable, and certainly not very easy.

That's when I discovered PowerShell (PoSH), Microsoft's new command line environment built on the .NET framework. The main difference between PoSH and batch files is that PoSH is object-oriented, not procedural. The main advantage when dealing with scripting Documentum, however, was the ability to write much more complicated logic, such as FOR statements, FOREACH statements, and embedding such statements within each other. I called the project of getting PowerShell to talk to Documentum Big Thunder Mountain, after one of my favourite rides at Disneyland. I wrote the first PoSH script in July of 2007. It wasn't very advanced - all it did was take a look at people's default folder, see if there was a folder in there marked Private, and create one if it didn't exist. I had to teach myself some stuff about parsing text files, and stage one was complete. I was now able to write rudimentary files for connecting to a docbase.

Details of the First Script

I realized early on that I would be writing more than one Big Thunder Mountain script, so I developed three lines of code that would run SELECT statements in Documentum:
echo "select $sourcecolumn from ""dm_folder"" where folder ('/Cabinet/Home Folders')" set-content -path c:\test\run.txt
echo "go" add-content -path c:\test\run.txt
cmd /c c:\test\1.bat c:\test\folders.txt
[1.bat was a batch file I wrote for running idql32.exe, as I couldn't figure out how to pass the correct arguments from PowerShell to the program. More on that later]

PowerShell requires that all variables begin with a dollar sign, so $sourcecolumn was the name of the column I was going to select from the database. In this case, I was looking for object_name.

The next few lines parsed the text file output, and put them into a text file, then loaded them into an object:
findstr /v "$sourcecolumn ---------------" c:\test\folders.txt > c:\test\temp.txt
findstr /v "affected" c:\test\temp.txt > c:\test\folders.txt
remove-item c:\test\temp.txt
$folders = get-content c:\test\folders.txt

At this point I was still using findstr.exe from the old Command Prompt days. Those days, however, were soon to end.

The next few lines included a couple FOREACH statements, designed to go through each folder and see if there was a folder called Private. If there wasn't, it wrote the name of the person to a text file (one text file per person - a horrible way to do administration). If there was, it did not include the person.
foreach ($folder in $folders){$folder = $folder.TrimEnd()
echo "select $sourcecolumn from ""dm_folder"" where folder ('/Cabinet/Home Folders/$folder')" set-content -path c:\test\run.txt
echo "go" add-content -path
c:\test\run.txtcmd /c c:\test\1.bat "c:\test\$folder.usr"
$userfolder = get-content "c:\test\$folder.usr"
foreach ($item in $userfolder){$item = $item.TrimEnd()if ($item -eq "Private")
{remove-item "c:\test\$folder.usr"}}}
get-childitem c:\test\*.usr set-content -path c:\test\noprivate.txt
remove-item c:\test\*.usr

Wow, I am so embarrassed by this script now. I realize now how little I know both about DQL statements and PoSH. As we'll see in the next installment, I learned some tricks.

No comments: