歡迎您光臨本站 註冊首頁

Windows PowerShell 2.0之服務管理

←手機掃碼閱讀     火星人 @ 2014-03-03 , reply:0

Windows PowerShell 2.0之服務管理

當操作服務時,PowerShell傳遞System.ServiceProcess.ServiceController對象,獲取一個或多個對象的簡單方法是使用Get-Service cmdlet。當不帶參數調用時會返回當前系統中註冊的所有服務,這樣即可用這個cmdlet生成快速報告。下例獲取當前運行的所有服務:
PS C:\PowerShell> Get-Service | where {$_.Status -eq "Running"}
Status Name DisplayName
------ ---- -----------
Running ALG Application Layer Gateway Service
Running AudioSrv Windows Audio
Running BITS Background Intelligent Transfer Ser...
Running Browser Computer Browser
Running btwdins Bluetooth Service
Running CryptSvc Cryptographic Services
Running DcomLaunch DCOM Server Process Launcher
Running Dhcp DHCP Client
Running dmserver Logical Disk Manager
Running Dnscache DNS Client
Running Eventlog Event Log
Running EventSystem COM+ Event System
Running FastUserSwitchi... Fast User Switching Compatibility
Running HidServ HID Input Service
…….
可以通過服務名通配符來查詢,下例通過服務名類似DNS*的通配符查詢DNS client服務:
PS C:\PowerShell> Get-Service DNS*
Status Name DisplayName
------ ---- -----------
Running Dnscache DNS Client
需要查找相關服務時,在查詢的服務名稱中使用通配符也很有效,相關服務通常有相似的名稱。下例通過獲取所有匹配*net*通配符獲取Windows下與網路相關的服務:
PS C:\PowerShell> Get-Service *net*
Status Name DisplayName
------ ---- -----------
Stopped aspnet_state ASP.NET State Service
Stopped NetDDE Network DDE
Stopped NetDDEdsdm Network DDE DSDM
Stopped Netlogon Net Logon
Running Netman Network Connections
Stopped VMnetDHCP VMware DHCP Service
【注意】
通配符應用於服務名,而不是顯示名。這就是為什麼使用*net*這種形式的通配符獲取的服務中僅是名稱中顯示包含net字樣服務的一部分,即部分以net為服務顯示名的服務的真實名稱並不以net開頭。
1 改變服務狀態
Windows提供的控制台工具net.exe可以啟動和停止服務。由於這個程序中包含很多功能,過於複雜,而不利於PowerShell各部分松耦合的要求。例如,能夠添加和移除共享驅動器。PowerShell提供了內置cmdlet用來改變服務狀態,如啟動、停止、掛起和恢復服務,所有的命令均直接調用。啟動服務使用Start-Service:
PS C:\> Start-Service W32Time
也可以通過調用ServiceControler的Start方法來啟動服務:
PS C:\> (Get-Service W32Time).Start()
需要強調的是改變服務狀態需要管理員許可權。
停止服務可以使用Stop-Service:
PS C:\> Stop-Service W32Time
也可使用Stop方法停止服務:
PS C:\> (Get-Service W32Time).stop()
其他改變服務狀態操作的cmdlet如Restart-Service、Suspend-Service和Resume-Service與上類似。
【說明】
改變服務會影響系統的整體運行,所以需要管理員許可權。如果運行Windows Vista及其以上版本的操作系統,務必要在高級許可權的Shell中執行操作。
2 改變服務的屬性
PowerShell提供了cmdlet Set-Service允許用戶改變服務的任何屬性,包括服務名和描述。但這是比較危險的操作,執行時應格外小心,因為更改服務名后可忘記服務到底是哪一個。Set-Service可以改變服務啟動方式,下例將W32Time服務的啟動方式改變為Disabled:
PS C:\> Set-Service W32Time –StartupType Disabled
在每次啟動時自動運行某個服務的方式是將其啟動類型改為Automatic,如:
PS C:\> Set-Service W32Time –StartupType Automatic
需要強調的是因涉及系統的穩定運行,所以在啟動、關閉或者改變服務的啟動方式時一定要謹慎。有些服務是Windows操作系統正常運行所必需的,停止或禁用這些服務將會引起系統某些功能停止。
3 分析服務的依賴性
一些服務的運行經常會依賴於其他服務,服務管理控制台管理單元顯示這些依賴關係。如果需要在PowerShell進程或腳本中獲取這些信息,則可以讀取ServiceControler、DependentServices和ServicesDependedOn屬性,它們分別用於返回當前服務依賴的服務和依賴當前服務的所有服務,如:
PS C:\> (Get-Service winmgmt).DependentServices
Status Name DisplayName
------ ---- -----------
Running wscsvc Security Center
Running SharedAccess Windows Firewall/Internet Connectio...
PS C:\> (Get-Service winmgmt).ServicesDependedOn
Status Name DisplayName
------ ---- -----------
Running RPCSS Remote Procedure Call (RPC)
其中顯示WMI服務依賴於Remote Procedure Call服務,同時Security Center和Windows Firewall/Internet Connection服務依賴於它。
4 總 結
進程在操作系統中用於處理數據和進程間的交換,PowerShell的進程和服務管理機制能分析進程信息,並且直接操作進程實例指向的對象。本文講述了如何使用PowerShell統一訪問服務的方法並創建在PowerShell中操作服務的環境,這是系統管理自動化的重要步驟。

[火星人 ] Windows PowerShell 2.0之服務管理已經有661次圍觀

http://coctec.com/docs/service/show-post-2569.html