首先,我假设您正在接管一个现有项目,因为您的问题有点矛盾:您是 Windows 服务的新手,但您声明您的系统中有一个。我还将考虑您必须维护现有的软件模型,同时您可以控制两台机器。所以:
- 机器 1上的 WCF 服务提供 PS 脚本
- 机器 2上的 Windows 服务应在这些脚本可用并由机器 1上的 WCF 服务传递时立即执行这些脚本
为了解决这个问题,您可以考虑在机器 2上托管另一个 WCF 服务:
How to: Host a WCF Service in a Managed Windows Service
这将如何运作?每次有新的 PS 脚本可用时,您都可以从机器 1上的 WCF 服务调用机器 2上的第二个 WCF 服务。随后,机器 2上的 WCF 服务可能会将脚本保存到某个存储库(文件、数据库、内存中)并调用Windows 服务上的ServiceController.ExecuteCommand 方法。此时,Windows 服务将从其保存位置获取脚本并执行它。虽然我觉得这是一个不必要的过于复杂的软件项目,但这里有一个针对您的情况的实现,只是为了回答您的问题,是的,这是可能的。
在机器 2上,安装包含 WCF 服务的 Windows 服务:
using System;
using System.Linq;
using System.ComponentModel;
using System.ServiceModel;
using System.ServiceProcess;
using System.Configuration.Install;
using System.IO;
using System.Management.Automation;
using System.Management.Automation.Runspaces;
namespace Sample.Services
{
[ServiceContract(Namespace = "http://Sample.Services")]
public interface IScriptExecutor
{
[OperationContract]
void ExecuteScript(string script);
}
/// <summary>
/// The WCF service class which will pass the script to the Windows
/// service
/// </summary>
public class ScriptExecutorService : IScriptExecutor
{
const string PATH = @"C:\test\queue.txt";
public void ExecuteScript(string script)
{
File.WriteAllText(PATH, script);
ServiceController myService =
new ServiceController("WCFScriptWindowsService");
myService.ExecuteCommand((int)MyCustomCommands.ExecuteScript);
}
}
public class ScriptWindowsService : ServiceBase
{
const string PATH = @"C:\test\queue.txt";
const string PATH_OUT = @"C:\test\out.txt";
public ServiceHost serviceHost = null;
public ScriptWindowsService()
{
ServiceName = "WCFScriptWindowsService";
}
protected override void OnCustomCommand(int command)
{
switch (command)
{
case (int)MyCustomCommands.ExecuteScript:
// Execute the PS script
var ps = PowerShell.Create();
var runspace = RunspaceFactory.CreateRunspace();
ps.Runspace = runspace;
// read the command from a repository,
// could also be a database
ps.AddCommand(File.ReadAllText(PATH));
runspace.Open();
var results = ps.Invoke().ToList();
runspace.Close();
foreach (var result in results)
{
// writing the results to a file
File.AppendAllText(PATH_OUT,
String.Format("{0}\r\n",
result.BaseObject.GetType()));
}
break;
default:
break;
}
}
public static void Main()
{
ServiceBase.Run(new ScriptWindowsService());
}
protected override void OnStart(string[] args)
{
if (serviceHost != null)
{
serviceHost.Close();
}
serviceHost =
new ServiceHost(typeof(ScriptExecutorService));
serviceHost.Open();
}
protected override void OnStop()
{
if (serviceHost != null)
{
serviceHost.Close();
serviceHost = null;
}
}
}
// Provide the ProjectInstaller class which allows
// the service to be installed by the Installutil.exe tool
[RunInstaller(true)]
public class ProjectInstaller : Installer
{
private ServiceProcessInstaller process;
private ServiceInstaller service;
public ProjectInstaller()
{
process = new ServiceProcessInstaller();
process.Account = ServiceAccount.LocalSystem;
service = new ServiceInstaller();
service.ServiceName = "WCFScriptWindowsService";
Installers.Add(process);
Installers.Add(service);
}
}
/// <summary>
/// Holds the custom commands (only one, in our case)
/// </summary>
public enum MyCustomCommands { ExecuteScript = 128 };
}
在机器 1上,修改 WCF 服务,使其调用机器 2上的 WCF 服务:
using System.ServiceModel;
namespace Sample.Services
{
[ServiceContract(Namespace = "http://Sample.Services")]
public interface IScriptProvider
{
[OperationContract]
string GetScript();
}
public class ScriptProviderService : IScriptProvider
{
public string GetScript()
{
// do some processing ...
// let's say we end up with the "Get-Service" script
var script = "Get-Service";
// make sure you add a reference to the ExecutorServiceReference
// WCF service on machine 2
var client = new WcfService1.ExecutorServiceReference
.ScriptExecutorClient();
client.ExecuteScript(script);
return script;
}
}
}
我再次强调:您不必从 Windows 服务本身执行 PS 脚本。通过在 Windows 服务中托管新的 WCF 服务,您可以轻松调用它的方法并将逻辑从OnCustomCommand
方法的覆盖移动到ScriptExecutorService
'sExecuteScript
方法。
如果我的模型假设错误,我会更新我的答案。