It is very common that you need to run some commands on a remote server. For example, you may want to config your source control server to start a build process on a dedicated build machine once it receives any checkins. To achieve this, you can leverage the WMI scripting APIs.
The steps are:
WMI WbemScripting script sample:
Function RemoteExecute(strServer, strUser, strPassword, CmdLine)
Const Impersonate = 3
RemoteExecute = -1
Set Locator = CreateObject("WbemScripting.SWbemLocator")
Set Service = Locator.ConnectServer(strServer, "root\cimv2", strUser, strPassword)
Service.Security_.ImpersonationLevel = Impersonate
Set Process = Service.Get("Win32_Process")
result = Process.Create(CmdLine, , , ProcessId)
If (result <> 0) Then
WScript.Echo "Creating Remote Process Failed: " & result
Wscript.Quit
End If
RemoteExecute = ProcessId
End Function
NAnt is very popular in the .Net development community. You may want to write a C# extension for NAnt scripts to achieve some build automations on remote servers. You can achieve that easily too. I am posting a WMI C# sample below for your reference too.
WMI C# sample:
using System;
using System.Collections.Generic;
using System.Text;
using System.Management;
namespace WMISample
{
class Program
{
static void Main(string[] args)
{
string remoteMachine = "localhost";
ConnectionOptions connOptions = new ConnectionOptions();
connOptions.Impersonation = ImpersonationLevel.Impersonate;
connOptions.EnablePrivileges = true;
ManagementScope manScope = new ManagementScope(String.Format(@"\\{0}\ROOT\CIMV2", remoteMachine), connOptions);
manScope.Connect();
ObjectGetOptions objectGetOptions = new ObjectGetOptions();
ManagementPath managementPath = new ManagementPath("Win32_Process");
ManagementClass processClass = new ManagementClass(manScope, managementPath, objectGetOptions);
ManagementBaseObject inParams = processClass.GetMethodParameters("Create");
inParams["CommandLine"] = @"notepad.exe";
ManagementBaseObject outParams = processClass.InvokeMethod("Create", inParams, null);
Console.WriteLine("Creation of the process returned: " + outParams["returnValue"]);
Console.WriteLine("Process ID: " + outParams["processId"]);
Console.ReadLine();
}
}
}
THIS POST IS PROVIDED "AS-IS" WITH NO WARRANTIES AND CONFERS NO RIGHTS. Build time: Sun 03/30/2008 . ©2007 Dalun Software. All rights reserved. Back to Article List