InstallUtilを使用して、Windowsサービスのログオンユーザー名/パスワードをサイレントに設定する

StackOverflow https://stackoverflow.com/questions/140054

質問

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

次のような方法があります:

installutil.exe myservice.exe /customarg1=username /customarg2=password
役に立ちましたか?

解決 2

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

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;
        }
    }
}

他のヒント

上記の投稿よりはるかに簡単な方法で、インストーラーに余分なコードはありません。次を使用します:

  

installUtil.exe / username = domain \ username / password = password / unattended C:\ My.exe

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

InstallUtil.exe はStartupType = Manualを設定します

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

sc config MyServiceName start = auto

(「=」の後にスペースが必要であることに注意してください)

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

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

また、次のコマンドを使用して、ユーザーとしてサービスを実行することもできます。 ServiceProcessInstaller :: Account = ServiceAccount.User ;

" [domain \] user、password"を尋ねるポップアップサービスのインストール中に表示されます。

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;
     ....
ライセンス: CC-BY-SA帰属
所属していません StackOverflow
scroll top