C# Windows サービスをインストールするには、InstallUtil を使用する必要があります。サービスのログオン資格情報 (ユーザー名とパスワード) を設定する必要があります。これらはすべて静かに行う必要があります。
このようなことをする方法はありますか:
installutil.exe myservice.exe /customarg1=username /customarg2=password
C# Windows サービスをインストールするには、InstallUtil を使用する必要があります。サービスのログオン資格情報 (ユーザー名とパスワード) を設定する必要があります。これらはすべて静かに行う必要があります。
このようなことをする方法はありますか:
installutil.exe myservice.exe /customarg1=username /customarg2=password
上記の投稿よりもはるかに簡単で、インストーラーに余分なコードを追加しない方法は、次を使用することです。
installUtil.exe /username=ドメイン\ユーザー名 /password=パスワード /unattended C:\My.exe
使用するアカウントが有効であることを確認してください。そうでない場合は、「アカウント名とセキュリティ ID の間のマッピングが行われませんでした」という例外が表示されます。
私の同僚(ブルース・エディ)にブラボー。彼は、このコマンドライン呼び出しを行う方法を見つけました。
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.exeStartupType=Manual に設定します
サービスを自動起動する場合は、次を使用します。
sc config MyServiceName start= auto
(「=」の後にはスペースが必要です)
いいえ、installutil はそれをサポートしていません。
もちろん、インストーラーを作成した場合。カスタム アクションを使用すると、それを MSI の一部として、または installutil 経由で使用できます。
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;
....