2009-09-28 59 views
16

我們有一個Windows服務應用程序可以接受的命令行參數,如:如何參數傳遞給Windows服務一勞永逸的安裝,而不是每次啓動

MyService -option 

到目前爲止,當我們要用參數啓動該服務,我們要麼做手工從服務屬性對話框(在開始參數盒),或者使用命令

sc start MyService -option 

我們想一個辦法安裝服務「永久」用這個參數 ,所以用戶只需要啓動/停止它,而不必每次都設置參數

BTW,添加參數在ImagePath的註冊表項不工作,同樣沒有安裝這樣的:

MyService -option /install 

更新:謝謝你的答案至今,幫助我改進的問題。
我想實現的是在同一個可執行文件中有多個服務的情況下,將參數設置爲服務級別本身(與屬性一樣)。 binpath config選項僅更新註冊表中的ImagePath條目。這不能是服務特定的。

回答

10
sc config MyService binPath= MyService.exe -option 

更新

個人化的服務參數在關鍵HKLM\SYSTEM\CurrentControlSet\Services\<serviceName>\Parameters存儲在註冊表中。我不確定參數是如何傳遞給服務的。 I 相信 SCM讀取這些值,然後當它調用StartService時,它將它們傳遞給ServiceMain回調。

+0

相當於編輯ImagePath的註冊表項。不是特定於服務本身。但是,無論如何,管理這個入口的好方法,謝謝。 – 2009-09-28 22:34:58

+0

好的嘗試,但它似乎沒有工作...(至少不喜歡與屬性或sc開始) – 2009-09-29 01:31:04

+0

StartService *做*作爲參數傳遞給ServiceMain無論配置在服務屬性的'參數'編輯框從服務.msc管理單元記錄在規範中。我相信這對你應該足夠好。 – 2009-09-29 01:35:32

6

將參數放入配置文件如何?

+0

如果您想在問題更新中闡明的相同.exe中承載多個服務,則無法正常工作。你選擇哪個配置文件? – Timbo 2016-11-17 23:00:31

1

通過ImagePath在命令行上傳遞的參數可以在main()或GetCommandLine()中訪問。您可以使用命令行參數進行安裝,然後在您的ServiceMain中,檢查是否有任何參數在lpszArgs參數中傳遞。如果沒有,請調用GetCommandLine並查看是否有任何方法通過。

+0

在Exe和服務級別進行測試都是單個服務Exe的解決方案。但它需要重寫服務,並且不能在同一個Exe中同時使用多個服務。 – 2009-09-28 22:34:24

+0

我認爲Remus關於使用Parameters註冊表項的答案是您唯一的選擇。我不認爲有什麼辦法可以獲得通過自動啓動服務的服務參數。 – Dustin 2009-09-29 18:57:24

1

Powershell的能做到這一點,但你必須使用.NET來實現它。

new-Object System.ServiceProcess.ServiceController("$ServiceName",$ComputerName)).Start("$Parameter") 
0

使用SC(服務控制)命令,它給你更多的選擇比剛開始&停止。

DESCRIPTION: 
      SC is a command line program used for communicating with the 
      NT Service Controller and services. 
    USAGE: 
     sc <server> [command] [service name] ... 

     The option <server> has the form "\\ServerName" 
     Further, help on commands can be obtained by typing: "sc [command]" 
     Commands: 
     query-----------Queries the status for a service, or 
         enumerates the status for types of services. 
     queryex---------Queries the extended status for a service, or 
         enumerates the status for types of services. 
     start-----------Starts a service. 
     pause-----------Sends a PAUSE control request to a service. 
     interrogate-----Sends an INTERROGATE control request to a service. 
     continue--------Sends a CONTINUE control request to a service. 
     stop------------Sends a STOP request to a service. 
     config----------Changes the configuration of a service (persistent). 
     description-----Changes the description of a service. 
     failure---------Changes the actions taken by a service upon failure. 
     qc--------------Queries the configuration information for a service. 
     qdescription----Queries the description for a service. 
     qfailure--------Queries the actions taken by a service upon failure. 
     delete----------Deletes a service (from the registry). 
     create----------Creates a service. (adds it to the registry). 
     control---------Sends a control to a service. 
     sdshow----------Displays a service's security descriptor. 
     sdset-----------Sets a service's security descriptor. 
     GetDisplayName--Gets the DisplayName for a service. 
     GetKeyName------Gets the ServiceKeyName for a service. 
     EnumDepend------Enumerates Service Dependencies. 

     The following commands don't require a service name: 
     sc <server> <command> <option> 
     boot------------(ok | bad) Indicates whether the last boot should 
         be saved as the last-known-good boot configuration 
     Lock------------Locks the Service Database 
     QueryLock-------Queries the LockStatus for the SCManager Database 
    EXAMPLE: 
      sc start MyService 
2

按照ServiceBase.OnStart文檔:

在控制檯輸入的參數不保存;當服務從控制面板啓動時,它們將一次性傳遞到服務。自動啓動服務時必須存在的參數可放置在服務註冊表項(HKEY_LOCAL_MACHINE \ SYSTEM \ CurrentControlSet \ Services \)的ImagePath字符串值中。您可以使用GetCommandLineArgs方法從註冊表中獲取參數,例如:string [] imagePathArgs = Environment.GetCommandLineArgs();.

+0

我測試了它,它沒有得到註冊表的參數 – 2016-11-09 10:30:24

+1

@Desolator,在我的情況下服務名是IISLogMonitor。所以我改變了字符串值_HKEY_LOCAL_MACHINE \系統\ CurrentControlSet \服務\ IISLogMonitor \ ImagePath_到_C:\ Program Files文件\ IIS日誌監視器\ PowershellScriptAsService.exe 「-LogName 」IIS日誌監視器「 -ScriptPath」 C:\ Program Files文件\ IIS日誌Monitor \ IISLogMonitor.ps1「_然後我可以得到服務的參數字符串[] serviceImagePathArgs = Environment.GetCommandLineArgs();'。你想要做什麼? – 2016-11-09 20:24:38

+0

是的,你是正確的。當我修改註冊表值時,我卸載了服務並重新安裝了它,因此它重置了鍵值。 – 2016-11-10 03:18:19

相關問題