Programmatically Create / Delete Windows Media Services Publishing Point

1.4k Views Asked by At

I'm looking for a .NET wrapper to programmatically Create and Delete Windows Media Services publishing points through WMI.

I can't be the only one to ever want to do this through .NET, so before I re-invent the wheel, anyone seen any code samples out there to do this?

2

There are 2 best solutions below

0
On

In the windows SDK 7.1 there is the following file, could be a starting point.

Windows\v7.1\Samples\multimedia\windowsmediaservices9\jscript\addPub.js

/*---------------------------------------------------------------------
 Copyright (C) Microsoft Corporation. All rights reserved.
 Script name    : Add Publishing Point (addpub)
 Script version : 1.0
 Description    : This script adds a publishing point to the target
                  server.
 Command line parameters :
           [-s <Server1>] -n <pub1> -t <od|bc> -p <path>
 -s represents target server, -n represents publishing point name,
 -p represents path, -t represent type of publishing point.
 There are 4 Publishing point types.
 od = On-demand, bc = broadcast
 Example : addpub -s server1 -n odpub1 -t od -p c:/wsh1
 Returns  :
 1. Usage: addpub [-s <Server1>] -n <pub1> -t <od|bc> -p <path>
 2. Server %server% is not a valid WMS Server
 3.  %type% is not a valid publishing point type
 OS Requirements       :  Windows Server 2003 (all versions)
 Software requirements :  WMS Server
 Scripting Engine      : Jscript
 ---------------------------------------------------------------------*/
WMS_PUBLISHING_POINT_TYPE_ON_DEMAND = 1;
WMS_PUBLISHING_POINT_TYPE_BROADCAST = 2;

var objServer = null;
var dwWhichArg = 0;
var szEachArg = "";
var szTemp = "";
var szArgServer = "";
var szArgPubPoint = "";
var szArgPubPointType = "";
var szArgPubPointPath = "";
var bCheckName = false;
var bCheckPath = false;
var bCheckType = false;

var objArgs = WScript.Arguments;

if( 0 == WScript.Arguments.length )
{
    DisplayUsage(); 
}

// Parse the command to seperate out the server name and publishing points.

while( dwWhichArg < WScript.Arguments.length )
{
    szEachArg = objArgs( dwWhichArg );
    if( "-s" == szEachArg.toLowerCase() )
    {
        dwWhichArg = dwWhichArg + 1;
        if(dwWhichArg >= WScript.Arguments.length )    //ex: addpub ... -n p1 -s
        {
            DisplayUsage();
        }
        szEachArg = objArgs( dwWhichArg );
        if((szEachArg.toLowerCase()== "-n") || (szEachArg.toLowerCase()== "-p") || (szEachArg.toLowerCase()== "-t"))   //if next szEachArg is -p,-t,-n display usage : since syntax is wrong
        {
            DisplayUsage();
        }
        else
        {
            //accept only one server name
            if(szEachArg.lastIndexOf(",")== -1)
            {
                szArgServer = szEachArg;
            }
            else
            {
                DisplayUsage();
            }
        }
    }
    else if(szEachArg.toLowerCase()== "-n")
    {
        bCheckName = true;
        dwWhichArg = dwWhichArg + 1;

        if(dwWhichArg >=WScript.Arguments.length)    //ex: addpub ... -s s1 -p
        {
            DisplayUsage();
        }
        szEachArg = objArgs(dwWhichArg);
        if((szEachArg.toLowerCase()== "-p") || (szEachArg.toLowerCase()== "-t") || (szEachArg.toLowerCase()== "-s"))  //if next szEachArg is -s,-t,-p display usage : since syntax is wrong
        {
              DisplayUsage();
        }
        else
        {
            //accept only one publishing point name
            if(szEachArg.lastIndexOf(",")== -1)
            {
                szArgPubPoint=szEachArg;
            }
            else
            {
                DisplayUsage();
            }
        }
    }
    else if(szEachArg.toLowerCase()== "-p")
    {
        bCheckPath = true;
        dwWhichArg = dwWhichArg + 1;

        if(dwWhichArg >=WScript.Arguments.length)    //ex: addpub ... -s s1 -p
        {
            DisplayUsage();
        }
        szEachArg = objArgs(dwWhichArg);
        if((szEachArg.toLowerCase()== "-t") || (szEachArg.toLowerCase()== "-s") || (szEachArg.toLowerCase()== "-n"))  //display usage : since syntax is wrong
        {
            DisplayUsage();
        }
        else
        {
            //accept only one path
            if(szEachArg.lastIndexOf(",")== -1)
            {
                szArgPubPointPath=szEachArg;
            }
            else
            {
                DisplayUsage();
            }
        }
    }
    else if(szEachArg.toLowerCase()== "-t")
    {
        bCheckType = true;
        dwWhichArg = dwWhichArg + 1;

        if(dwWhichArg >=WScript.Arguments.length)
        {
            DisplayUsage();
        }
        szEachArg = objArgs(dwWhichArg);
        if((szEachArg.toLowerCase()== "-p") || (szEachArg.toLowerCase()== "-n") || (szEachArg.toLowerCase()== "-s"))  //display usage, since syntax is wrong
        {
            DisplayUsage();
        }
        else
        {
            //accept only one type
            if(szEachArg.lastIndexOf(",")== -1)
            {
                szArgPubPointType=szEachArg;
            }
            else
            {
                DisplayUsage();
            }
        }
    }
    else  // if argument is not -p,-n,-t,-s  i.e. if it is an invalid argument
    {
        DisplayUsage();
    }

    dwWhichArg = dwWhichArg + 1;
}

if( (!bCheckName) || (!bCheckPath) || (!bCheckType) )       //Ex:  compulsory part -p,-t or -n is missing.
{
    DisplayUsage();
}

// Connect to Server
//if server name is not mentioned, then start Publishing points on localhost
if( "" == szArgServer )
{
    szArgServer = "LocalHost";
}

var bFailed;
bFailed = false;

try
{
    objServer = new ActiveXObject( "WMSServer.server", szArgServer );
}
catch(e)
{
    bFailed = true;
    szTemp = "Server '" + szArgServer + "' is not a valid WMS Server \n";
    Trace( szTemp );
}

if( !bFailed )
{
    Trace("\nAdding Publishing Points at "+ szArgServer );

    //Add Publishing Point
    AddPublishingPoint();
}


// This function checks if the publishing point name and type are valid, and then
// adds it to server.
function AddPublishingPoint()
{
    var bFailed, nType;
    bFailed = false;

    //check if the type is a valid type
    switch(szArgPubPointType)
    {
        case 'od' :
            nType = WMS_PUBLISHING_POINT_TYPE_ON_DEMAND;
            break;
        case 'bc' :
            nType = WMS_PUBLISHING_POINT_TYPE_BROADCAST;
            break;
        default :
        {
            bFailed = true;
            szTemp = "Adding Publishing Point '" + szArgPubPoint + "' failed: Unknown type\n";
            break;
        }
    }

    if( !bFailed )
    {
        try
        {
            var objPubPoint = objServer.PublishingPoints.Add( szArgPubPoint, nType, szArgPubPointPath );
            szTemp = "Added " + szArgPubPoint;
        }
        catch(e)
        {
            var errorcode = e.number >>> 0;
            szTemp = "Error Code 0x" + errorcode.toString(16) + ": " + e.description;
        }
    }

    Trace( szTemp );
}

function DisplayUsage()
{
    Trace( "Usage: addpub [-s <Server1>] -n <pub1> -t <od|bc> -p <path>" );
    WScript.Quit(1);
}

function Trace(Msg)
{
    WScript.Echo(Msg);
}
0
On

I'm using the Media Services 9 SDK which is documented here. The SDK is bundled with the runtime components, so you'll need to copy Microsoft.WindowsMediaServices.Interop.dll and WMSServerTypeLib.dll from your Windows Server machine with the runtime installed. I found them at C:\Windows\System32\windows media\server. Then, register the type library and add a reference to the Interop assembly.

Then, to use the SDK:

using System.Runtime.InteropServices;
using Microsoft.WindowsMediaServices.Interop;

// instantiating the server for a remote host
Type serverType = Type.GetTypeFromProgID("WMSServer.Server", "MediaServer");

// may need to wrap this in an impersonation context depending the server's ACL
WMSServer server = (WMSServer)Activator.CreateInstance(serverType); 

// removing all of the publish points
for(int i = server.PublishingPoints.Count - 1; i >= 0; i--)
{
   server.PublishingPoints.Remove(i);
}

// adding a push broadcast point
IWMSBroadcastPublishingPoint newPoint = 
          (IWMSBroadcastPublishingPoint) server.PublishingPoints.Add(
             "NewPoint", WMS_PUBLISHING_POINT_CATEGORY.WMS_PUBLISHING_POINT_BROADCAST,
              "Push:*");

// cloning
IWMSPublishingPoint cloned = server.PublishingPoints.Clone("Cloned", newPoint);

I included cloning because I encountered an error (The stub received bad data) when trying to add a new publishing point on a remote machine. I saw another thread where someone had the same issue, so I figured instead of fighting it, I would just create a template publishing point and create all of the new points as clones of that.