【问题标题】:C# Implementaion of New-Service cmdlet新服务 cmdlet 的 C# 实现
【发布时间】:2013-02-14 17:32:09
【问题描述】:

我需要安装一个可执行文件作为服务,我需要使用用 C# 编写的 PowerShell cdmlet 来完成。本质上,我需要创建所需的注册表项等来定义服务(包括要运行的可执行文件 - 一种 srvany.exe)。

我还需要能够定义此服务的登录身份(凭据)。

TIA,汉斯

到目前为止我尝试了什么。我一直在研究 ServiceProcessInstaller 和 ServiceProcessInstaller 类,但它们错过了定义“外部”可执行文件的可能性。

    public partial class MyServiceInstaller : Installer
    {
        public MyServiceInstaller()
        {
            IDictionary saveState = null;
            this.Installers.Clear();
            ServiceProcessInstaller spi = new ServiceProcessInstaller();
            ServiceInstaller si = new ServiceInstaller();
            spi.Account = ServiceAccount.LocalSystem;
            spi.Username = null;
            spi.Password = null;
            si.ServiceName = "MyService";
            si.DisplayName = "MyService";
            si.StartType = ServiceStartMode.Automatic;
            si.Description = "MyService - I wish....";

            spi.Installers.Add(si);
            this.Installers.Add(spi);
            this.Install(saveState);
        }
    }

因为找不到添加可执行路径(服务映像路径)的方法,所以卡在这里

【问题讨论】:

  • 还有what have you tried?您在编写代码时遇到了哪些特殊困难?我在你的问题中只能看到I need to ...,但我在任何地方都没有看到here's the code I tried but ...
  • 欢迎来到 SO。请查看FAQAbout 页面,看看这里最欢迎什么样的问题。

标签: c# powershell service windows-services


【解决方案1】:

嗯,我有以下代码工作,我只是想知道是否有更“本机”的方式来做到这一点。

                Command psc = new Command("New-Service");
                psc.Parameters.Add("Name", svcName);
                psc.Parameters.Add("BinaryPathName", svcExec);
                psc.Parameters.Add("DisplayName", svcName);
                psc.Parameters.Add("Description", svcDesc);
                psc.Parameters.Add("StartupType", "Automatic");
                WriteVerbose("Verifying service account");
                if (Account == null) { WriteVerbose("- Using LocalSystem account"); }
                else { psc.Parameters.Add("Credential", crd); }
                WriteVerbose("Installing service");
                Pipeline pipeline = Runspace.DefaultRunspace.CreateNestedPipeline();
                pipeline.Commands.Add(psc);
                Collection<PSObject> results = pipeline.Invoke();

【讨论】:

    【解决方案2】:

    感谢Brad Bruce 我找到了我需要的东西:How do I install a C# Windows service without creating an installer?

    我对 IntegratedServiceInstaller 类做了一个小调整,对“SINST.Install(state)”的调用将产生 3 行输出

    Installing service 'ServiceName'...
    Service 'ServiceName' has been successfully installed.
    Creating EventLog source 'ServiceName' in log Application...
    

    调用 SINST.Uninstall(null) 时类似的行会写入控制台

    我通过将输出重定向到 Stream.Null 来抑制此输出

        System.IO.StreamWriter sw = new System.IO.StreamWriter(Stream.Null);
        System.IO.TextWriter tmp = Console.Out;
        Console.SetOut(sw);
        try { SINST.Install(state); }
        catch (Exception Ex) { Console.SetOut(tmp); Console.WriteLine(Ex.Message); }
        finally { Console.SetOut(tmp); }
    

    【讨论】:

      猜你喜欢
      • 2016-01-17
      • 1970-01-01
      • 1970-01-01
      • 2010-12-16
      • 2021-01-15
      • 2017-11-01
      • 2011-02-09
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多