Category Archives: Scripting

Lync and fortunes


“Parachute for sale. Only used once, never opened, small stain”

“Life is tough. Get a helmet”

“I don’t have a smart phrase to share with you”

Some people love to put sentences like these in their messenger. I, for sure, do. In case of OCS, you could do it manually. But really, only one message of the day? Boooooring (catchphrase: “Somebody’s boring me … I think it’s me. ”), especially if you have collected lots of them. Changing them manually, almost as boring as not changing them. Spend some time to create a small program for that: that’s how we, geeks, do the stuff!

Anyway, I tried it with OCS, but had no luck. Either I was too lazy to find some simple solution or it wasn’t simple enough. But With the new releases the things are usually better then before (“If at first you don’t succeed, call it version 1.0”) and with Lync we have received a new SDK with the most precious peace of it, which finally allows to change the status programmatically Подмигивающая рожица

To cut long story short: you can read about the SDK parts which helped me to build the script, then read about PoSh scripting for Lync using the extensibility API and then read about how to operate the needed parameters. One more step – to download and install the SDK. After that you’ll be able to write your own script for that stuff.

Or you can just use this one:


#Load Assembly

$assemblyPath = “C:Program Files (x86)Microsoft LyncSDKAssembliesDesktopMicrosoft.Lync.Model.DLL”

Import-Module $assemblyPath


#Initialize objects

$client = [Microsoft.Lync.Model.LyncClient]::GetClient()

$self = $client.Self;


#Get a fortune

$Fortunes = Get-Content c:tempfortunes.txt

$linesNumber = $Fortunes.Length

$todaysFortuneNumber = Get-Random Maximum $linesNumber Minimum 1

$todaysFortune = $Fortunes[$todaysFortuneNumber 1]


#Publish personal note of the local user

$contactInfo = new-object ‘System.Collections.Generic.Dictionary[Microsoft.Lync.Model.PublishableContactInformationType, object]’

$contactInfo.Add([Microsoft.Lync.Model.PublishableContactInformationType]::PersonalNote $todaysFortune)

$ar = $self.BeginPublishContactInformation($contactInfo, $null, $null)



The script isn’t ideal and needs improving, but you can consider it as a working prototype =)

BITS Transfer PowerShell cmdlets

One friend of mine told me that I shouldn’t have spread knowledge about BITSAdmin command while there was the PowerShell cmdlets in place. Well, to some extent he is definitely right:

  • 1) PowerShell is better self-documented.

2) It is waaaay easier to script with.

3) It is more simple to use in some basic situations like “just give me that darn file”.

4) Many people just like PoSh.

So, the tasks I did in my post about BITSAdmin seems to be done in one command:

Start-BitsTransfer –source <URL> –destination <PathToFile>

but one need to do his homework better:


Seems like the module for BITS is not imported by default. Let’s correct the mistake:


and now we have our cmdlets:


So, let’s our download begin:


Excellent, isn’t it (it even show the progress very visually)? No, it isn’t. Because when I turned my network connection off the download was cancelled:


Even though it was stated that “BITS will try again” – it wouldn’t and there wasn’t any job registered with BITS. I don’t know why, actually (I hope my friend will explain it to me), but I found a “workaround”. Just add “-Asynchronous” option to the string and it will fork just fine for you although you won’t be able to see that beautiful download bar:



But even when the state changed to “Transferred”, there was only a .tmp file in my directory. Actually, when I started the command without “-Asynchronous” option, I’ve got the file immediately after the end of the transfer, but you already know that you cannot then resume the transfer if it was interrupted. Therefore, I had to complete the transfer manually:


Not very big difference from what we did with BITSAdmin, I guess. And I couldn’t tell how to do is to monitor my jobs in fasion BITSAsmin /MONITOR does.

So, let’s sum it up:

1) PoSh is best for scripting

2) You can use for interactive tasks whichever command set you are used to, but remember, that BITSAdmin can be discontinued any moment

So, my best approach is, do everything I can with PoSh and monitor with BITSAdmin, until someone tells me how to do it with PoSh 😉

How to change attribute in AD: alternatives #2

Returning to the question of AD attributes change tools I should go on for some more graphical tools. From now on I know only some self-created possibilities, which require some coding. First is to create some

Custom GUI Application

There are multitudes of variants: C#, VBScript, C, you name it. Being somewhat lazy, I decided to take a short cut. In a beautiful book from Windows 2008 resource kit, namely: Windows Administration Resource Kit, there are some useful additions. Among them there is an .HTA script, named “Object_Attribute_EmployeeNumber.hta”. It allows me to show EmployeeNumber attribute and set it. As we were demonstrating EmployeeID attribute changes I had to implement some changes, like replacing where it was needed word “number” with word “ID” (be careful: not every “number” entry needs to be replaced), like that:


and some minor bug fixing. But since I’ve done it – voila:


What are pros of the method? Obviously, it is very flexible method and you may create the application as powerful as you need. And this method requires less education for your staff. Still, you have some drawbacks: you have to create some app, you have to support and develop it in case it becomes stale.

Anyway, this leaves us with one more method:

extending the ADUC or other AD mmc consoles

It should be absolutely cool, but it is way over my head at the moment. I am not really ready to give anyone a step-by-step guide how to implement the feature yet, so I will postpone the article till I am able to.

DPM: Scripting Additions to a Backup

image It’s beautiful when we are able to use features of DPM to backup an application as a whole (say, SharePoint or Hyper-V virtual machine), but what will happen when it is not enough to just copy files and system state (like in OCS in some cases, or Windows Server 2008 BMR Backup), or our MOSS 2007 farm configured in a way we can’t backup it at once (some SQL mirroring scenarios prevent us from doing so)?

Well… It is where scripting steps in.  DPM actually lets us to run any script before backup starts or after it finishes. Is it difficult? Not at all. Is it something I can recommend to create comprehensive backup packages, or run a bunch of pre-/post-backup tasks? Not really. Let us discuss why:

1) Difficulty. Actually you have just to publish your scripts to the local drive of a protected computer and change a configuration file. Script may be of any type: shell, VBS, PowerShell or even Perl. For example, script for BMR backup on Windows 2008 may look like

@echo off
setlocal enabledelayedexpansion
set BACKUP_TARGET=\BackupServerServerBackup
rd /s /q "%BACKUP_TARGET%WindowsImageBackup%computername%"
wbadmin start backup -backuptarget:"%BACKUP_TARGET%" -allcritical -quiet
if %ERRORLEVEL% == 0 (
rem    pushd "%BACKUP_TARGET%WindowsImageBackup%computername%"
rem    for /f "tokens=*" %%i in (‘dir /b /s *.vhd’) do move /Y "%%i"


(the code is almost from this document)

Anyway, the most important part of the task belongs to the file ScriptingConfig.xml which usually is situated in folder c:Program FilesMicrosoft Data Protection ManagerDPMScripting

The content of the file by default is:

<?xml version="1.0" encoding="utf-8" ?>

What we need is to add some configuration before </ScriptConfiguration>. The final file looks like that:

<?xml version="1.0" encoding="utf-8" ?>

<ScriptConfiguration xmlns:xsi="" xmlns:xsd="" xmlns="">

<DatasourceScriptConfig DataSourceName="c:">

  <PreBackupScript>"Path-To-Script or command line to run script"</PreBackupScript>

  <PostBackupScript />

You see: everything looks clear in the code.

  • DataSourceName – the name of the data source before (or after) backing up which the script runs.
  • PreBackupScript – string which should run before backup
  • Postbackupscript – string which should run after backup
  • TimeOut – script timeout in minutes

Pretty simple, isn’t it?

2) Why not to do it unless you really have to? Well… It’ is hard to monitor whether the script worked fine or not. DPM console won’t tell you about it. For example, when we are speaking of Windows 2008 Bare Metal Recovery Backups we may backup any file on a file system preceding the backup with a script which makes BMR backup with WBAdmin.  If the file itself is backed up successfully then your DPM server will show you a green mark even if the BMR Backup script failed. The only ways to know if everything went good are:

  • Make a test recovery (good idea usually, but just imagine hundreds of servers backed up every week… Impossible to check them all every week)
  • Make the script complex enough to catch errors in a backup and report them. Again, not really bad idea, but it is totally unmanaged and may become very costly in large environments
  • Use 3rd party tools.

But for some tasks it is really good stuff and you may use it.

Ready for 2010 Scripting Games? Prepare!

image The Games’ organizers  have issued the list of skills necessary (Hey! No one told you the skills are sufficient 😉 ) for you to successfully pass through the Games’ tasks. Thus, what do you need to be ready:

1) Working with the registry

2) Event log

3) Text files and folders

4) Environment variables

5) WMI

6) Looping

7) Special folders

8) Arrays

9) Functions and subroutines

10) Errors handling

11) Graphical sccripting

On first sight it is such a list! But even a scripting newbie will learn a lot from the learning materials’ links which given by the organizers  with the other details.

Join and enjoy – it will be fun!