Example #1
0
    public static void InstallService(Conf conf)
    {
        var depend = conf.Dependencies.AddUniq(Consts.NECESSARY_DEPENDENCY, '/');

        // add "[svc]" to DisplayName to avoid "ServiceName == DisplayName" (which cases error in win7)
        var scArgs = $"create \"{conf.ServiceName}\" binPath= \"{Libs.BinPath}\" start= auto "
                     + $"DisplayName= \"[svc] {conf.DisplayName}\" depend= \"{depend}\"";

        if (conf.User.Length > 0)
        {
            var obj = (conf.Domain.Length > 0) ? $"{conf.Domain}\\{conf.User}" : conf.User;
            scArgs = $"{scArgs} obj= \"{obj}\" password= \"{conf.Password}\"";
        }

        Libs.Exec("sc", scArgs);

        var sc = GetServiceController(conf.ServiceName);

        if (sc == null)
        {
            Libs.Abort($"Failed to install Service \"{conf.ServiceName}\"");
        }

        var msg = $"Installed Service \"{conf.ServiceName}\"";

        Console.WriteLine(msg);
        AddLog("INFO", msg, false);
        AddLog("INFO", "");

        SetSvcDescription(conf);

        sc.StartService();
    }
Example #2
0
    private static void SetSvcDescription(Conf conf)
    {
        var    description = $"{conf.Description} @<{Libs.GetCwd()}>";
        var    scArgs      = $"description \"{conf.ServiceName}\" \"{description}\"";
        string err;

        Libs.Exec("sc", scArgs);
        try
        {
            var mngObj = GetServiceManagementObjectByName(conf.ServiceName);
            if (mngObj != null && mngObj["Description"].ToString() == description)
            {
                return;
            }

            err = "unknown reason";
        }
        catch (Exception ex)
        {
            err = ex.ToString();
        }

        err = $"Failed to set description for Service \"{conf.ServiceName}\": {err}";
        AddLog("ERROR", err);
        Libs.Abort($"{err}\r\nPlease run `svc remove` to remove the Service");
    }
Example #3
0
    public static ServiceController CreateSvc(Conf conf)
    {
        var scArgs = $"create \"{conf.ServiceName}\" binPath= \"{Libs.BinPath}\" start= auto "
                     + $"DisplayName= \"{conf.DisplayName}\"";

        if (conf.User.Length > 0)
        {
            var obj = (conf.Domain.Length > 0) ? $"{conf.Domain}\\{conf.User}" : conf.User;
            scArgs = $"{scArgs} obj= \"{obj}\" password= \"{conf.Password}\"";
        }

        Libs.Exec("sc", scArgs);

        return(GetServiceController(conf.ServiceName));
    }
Example #4
0
    public static ServiceController CreateSvc(Conf conf)
    {
        var depend = conf.Dependencies.AddUniq(Conf.NECESSARY_DEPENDENCY, '/');

        var scArgs = $"create \"{conf.ServiceName}\" binPath= \"{Libs.BinPath}\" start= auto "
                     + $"DisplayName= \"{conf.DisplayName}\" depend= \"{depend}\"";

        if (conf.User.Length > 0)
        {
            var obj = (conf.Domain.Length > 0) ? $"{conf.Domain}\\{conf.User}" : conf.User;
            scArgs = $"{scArgs} obj= \"{obj}\" password= \"{conf.Password}\"";
        }

        Libs.Exec("sc", scArgs);

        return(GetServiceController(conf.ServiceName));
    }
Example #5
0
    public static void RemoveService(this ServiceController sc)
    {
        var name = sc.ServiceName;

        sc.StopService();

        Libs.Exec("sc", $"delete \"{name}\"");

        if (GetServiceController(name) != null)
        {
            Libs.Abort($"Failed to remove Service \"{name}\"");
        }

        var msg = $"Removed Service \"{name}\"";

        AddLog("INFO", "");
        AddLog("INFO", msg);
        Console.WriteLine(msg);
    }
Example #6
0
    public static string SetSvcDescription(Conf conf)
    {
        var cwd         = Path.GetFullPath(".");
        var description = $"{conf.Description} @<{cwd}>";
        var scArgs      = $"description \"{conf.ServiceName}\" \"{description}\"";

        Libs.Exec("sc", scArgs);
        try
        {
            var mngObj = GetServiceManagementObjectByName(conf.ServiceName);
            if (mngObj == null || mngObj["Description"].ToString() != description)
            {
                return("unknown");
            }

            return(null);
        }
        catch (Exception ex)
        {
            return(ex.ToString());
        }
    }
Example #7
0
 public static bool DeleteSvc(string serviceName)
 {
     Libs.Exec("sc", $"delete \"{serviceName}\"");
     return(GetServiceController(serviceName) == null);
 }