51

C# Windows サービスをインストールするには、InstallUtil を使用する必要があります。サービスのログオン資格情報 (ユーザー名とパスワード) を設定する必要があります。これらはすべて静かに行う必要があります。

このようなことをする方法はありますか:

installutil.exe myservice.exe /customarg1=username /customarg2=password
4

5 に答える 5

73

上記の投稿よりもはるかに簡単で、インストーラーに余分なコードを追加しない方法は、次を使用することです。

installUtil.exe /username=ドメイン\ユーザー名 /password=パスワード /unattended C:\My.exe

使用するアカウントが有効であることを確認してください。そうでない場合は、「アカウント名とセキュリティ ID の間のマッピングが行われませんでした」という例外が表示されます。

于 2010-05-19T08:54:30.247 に答える
54

私の同僚(ブルース・エディ)にブラボー。彼は、このコマンドライン呼び出しを行う方法を見つけました。

installutil.exe /user=uname /password=pw myservice.exe

これは、インストーラー クラスで OnBeforeInstall をオーバーライドすることによって行われます。

namespace Test
{
    [RunInstaller(true)]
    public class TestInstaller : Installer
    {
        private ServiceInstaller serviceInstaller;
        private ServiceProcessInstaller serviceProcessInstaller;

        public OregonDatabaseWinServiceInstaller()
        {
            serviceInstaller = new ServiceInstaller();
            serviceInstaller.StartType = System.ServiceProcess.ServiceStartMode.Automatic;
            serviceInstaller.ServiceName = "Test";
            serviceInstaller.DisplayName = "Test Service";
            serviceInstaller.Description = "Test";
            serviceInstaller.StartType = ServiceStartMode.Automatic;
            Installers.Add(serviceInstaller);

            serviceProcessInstaller = new ServiceProcessInstaller();
            serviceProcessInstaller.Account = ServiceAccount.User; 
            Installers.Add(serviceProcessInstaller);
        }

        public string GetContextParameter(string key)
        {
            string sValue = "";
            try
            {
                sValue = this.Context.Parameters[key].ToString();
            }
            catch
            {
                sValue = "";
            }
            return sValue;
        }


        // Override the 'OnBeforeInstall' method.
        protected override void OnBeforeInstall(IDictionary savedState)
        {
            base.OnBeforeInstall(savedState);

            string username = GetContextParameter("user").Trim();
            string password = GetContextParameter("password").Trim();

            if (username != "")
                serviceProcessInstaller.Username = username;
            if (password != "")
                serviceProcessInstaller.Password = password;
        }
    }
}
于 2008-09-26T15:36:15.783 に答える
4

InstallUtil.exeStartupType=Manual に設定します

サービスを自動起動する場合は、次を使用します。

sc config MyServiceName start= auto

(「=」の後にはスペースが必要です)

于 2013-07-02T12:22:30.620 に答える
0

いいえ、installutil はそれをサポートしていません。

もちろん、インストーラーを作成した場合。カスタム アクションを使用すると、それを MSI の一部として、または installutil 経由で使用できます。

于 2008-09-26T15:05:58.757 に答える
-2

ServiceProcessInstaller::Account = ServiceAccount.Userを使用して、サービスを強制的にユーザーとして実行することもできます 。

サービスのインストール中に、「[ドメイン\]ユーザー、パスワード」を尋ねるポップアップが表示されます。

public class MyServiceInstaller : Installer
{
    /// Public Constructor for WindowsServiceInstaller
    public MyServiceInstaller()
    {
        ServiceProcessInstaller serviceProcessInstaller = new ServiceProcessInstaller();
        ServiceInstaller serviceInstaller = new ServiceInstaller();

        //# Service Account Information
        serviceProcessInstaller.Account = ServiceAccount.User; // and not LocalSystem;
     ....
于 2009-10-23T14:31:37.910 に答える