New-Service

https://docs.microsoft.com/en-us/powershell/module/microsoft.powershell.management/new-service?view=powershell-5.1

PS C:\Program Files\PowerShell\6.0.0-beta.6> New-Service -Name LISA_41_Dev_Batch -DisplayName LISA_41_Dev_Batch -BinaryP

athName D:\ChuckLu\GitForSvn\02.source_4.1.0.0\LISA.Batch\LISA.Batch.MonitorService\bin\Debug\LISA.Batch.MonitorService.
exe

Status Name DisplayName
------ ---- -----------
Stopped LISA_41_Dev_Batch LISA_41_Dev_Batch

PS C:\Program Files\PowerShell\6.0.0-beta.6> New-Service -Name LISA_50_Dev_Batch -DisplayName LISA_50_Dev_Batch -BinaryP
athName D:\ChuckLu\Git\Edenred\LISA_5.0.0.0\LISA.Batch\LISA.Batch.MonitorService\bin\Debug\LISA.Batch.MonitorService.exe

Status Name DisplayName
------ ---- -----------
Stopped LISA_50_Dev_Batch LISA_50_Dev_Batch

PS C:\Program Files\PowerShell\6.0.0-beta.6> cd D:\ChuckLu\Git\Edenred\LISA_5.0.0.0\LISA.FileExchange\LISA.FileExchange.
Service\bin\Debug
PS D:\ChuckLu\Git\Edenred\LISA_5.0.0.0\LISA.FileExchange\LISA.FileExchange.Service\bin\Debug> New-Service -Name LISA_50_
Dev_FileExchange -DisplayName LISA_50_Dev_FileExchange -BinaryPathName .\LISA.FileExchange.Service.exe

Status Name DisplayName
------ ---- -----------
Stopped LISA_50_Dev_Fil... LISA_50_Dev_FileExchange

记得加上startup type

-StartupType
New-Service -Name LISA_50_Dev_FileExchange -DisplayName LISA_50_Dev_FileExchange -BinaryPathName .\LISA.FileExchange.Service.exe -StartupType Manual New-Service -Name LISA_60_Dev_Batch -DisplayName LISA_60_Dev_Batch -BinaryPathName  .\LISA.Batch.MonitorService.exe -StartupType Manual

Get-Service

PS C:\Users\clu> help get-service

NAME
Get-Service

SYNTAX
Get-Service [[-Name] <string[]>] [-ComputerName <string[]>] [-DependentServices] [-RequiredServices] [-Include <str
ing[]>] [-Exclude <string[]>] [<CommonParameters>]

Get-Service -DisplayName <string[]> [-ComputerName <string[]>] [-DependentServices] [-RequiredServices] [-Include <
string[]>] [-Exclude <string[]>] [<CommonParameters>]

Get-Service [-ComputerName <string[]>] [-DependentServices] [-RequiredServices] [-Include <string[]>] [-Exclude <st
ring[]>] [-InputObject <ServiceController[]>] [<CommonParameters>]

ALIASES
gsv

REMARKS
Get-Help cannot find the Help files for this cmdlet on this computer. It is displaying only partial help.
-- To download and install Help files for the module that includes this cmdlet, use Update-Help.
-- To view the Help topic for this cmdlet online, type: "Get-Help Get-Service -Online" or
go to http://go.microsoft.com/fwlink/?LinkID=113332.

https://msdn.microsoft.com/powershell/reference/5.1/microsoft.powershell.management/Get-Service

Example

Get-Service -DisplayName FileExchange4.3_SourceCode

Get-Service | Where-Object {$_.name.Contains("LBS")} | Select name,DisplayName

Get-Service | Where-Object{$_.name.Contains("LFXSVC_")} | Select name,DisplayName

Get-Service | Where-Object {$_.name.Contains("LISA")} | Select Status,Name,DisplayName

Start-Service

PS C:\Users\clu> help start-service

NAME
Start-Service

SYNTAX
Start-Service [-InputObject] <ServiceController[]> [-PassThru] [-Include <string[]>] [-Exclude <string[]>] [-WhatIf
] [-Confirm] [<CommonParameters>]

Start-Service [-Name] <string[]> [-PassThru] [-Include <string[]>] [-Exclude <string[]>] [-WhatIf] [-Confirm] [<Co
mmonParameters>]

Start-Service -DisplayName <string[]> [-PassThru] [-Include <string[]>] [-Exclude <string[]>] [-WhatIf] [-Confirm]
[<CommonParameters>]

ALIASES
sasv

REMARKS
Get-Help cannot find the Help files for this cmdlet on this computer. It is displaying only partial help.
-- To download and install Help files for the module that includes this cmdlet, use Update-Help.
-- To view the Help topic for this cmdlet online, type: "Get-Help Start-Service -Online" or
go to http://go.microsoft.com/fwlink/?LinkID=113406.

https://msdn.microsoft.com/powershell/reference/5.1/microsoft.powershell.management/Start-Service

Example

Start-Service -Name "eventlog"

Start-Service -DisplayName FileExchange4.3_SourceCode

Restart-Service

https://docs.microsoft.com/en-us/powershell/module/microsoft.powershell.management/restart-service?view=powershell-6

The Restart-Service cmdlet sends a stop message and then a start message to the Windows Service Controller for a specified service.

If a service was already stopped, it is started without notifying you of an error.

You can specify the services by their service names or display names, or you can use the InputObject parameter to pass an object that represents each service that you want to restart.

Stop-Service

PS C:\Users\clu> help stop-service

NAME
Stop-Service

SYNTAX
Stop-Service [-InputObject] <ServiceController[]> [-Force] [-NoWait] [-PassThru] [-Include <string[]>] [-Exclude <s
tring[]>] [-WhatIf] [-Confirm] [<CommonParameters>]

Stop-Service [-Name] <string[]> [-Force] [-NoWait] [-PassThru] [-Include <string[]>] [-Exclude <string[]>] [-WhatIf
] [-Confirm] [<CommonParameters>]

Stop-Service -DisplayName <string[]> [-Force] [-NoWait] [-PassThru] [-Include <string[]>] [-Exclude <string[]>] [-W
hatIf] [-Confirm] [<CommonParameters>]

ALIASES
spsv

REMARKS
Get-Help cannot find the Help files for this cmdlet on this computer. It is displaying only partial help.
-- To download and install Help files for the module that includes this cmdlet, use Update-Help.
-- To view the Help topic for this cmdlet online, type: "Get-Help Stop-Service -Online" or
go to http://go.microsoft.com/fwlink/?LinkID=113414.

https://msdn.microsoft.com/powershell/reference/5.1/microsoft.powershell.management/Stop-Service

Example

Stop-Service -DisplayName FileExchange4.3_SourceCode

Stop-Service -DisplayName FileExchange4.3_SourceCode

通过任务管理器,强制终结服务

找到对应的exe,在进程中直接干掉。

Set-Service

PS C:\Users\clu> help set-service

NAME
Set-Service

SYNTAX
Set-Service [-Name] <string> [-ComputerName <string[]>] [-DisplayName <string>] [-Description <string>] [-StartupTy
pe <ServiceStartMode> {Boot | System | Automatic | Manual | Disabled}] [-Status <string> {Running | Stopped | Pause
d}] [-PassThru] [-WhatIf] [-Confirm] [<CommonParameters>]

Set-Service [-ComputerName <string[]>] [-DisplayName <string>] [-Description <string>] [-StartupType <ServiceStartM
ode> {Boot | System | Automatic | Manual | Disabled}] [-Status <string> {Running | Stopped | Paused}] [-InputObject
<ServiceController>] [-PassThru] [-WhatIf] [-Confirm] [<CommonParameters>]

ALIASES
None

REMARKS
Get-Help cannot find the Help files for this cmdlet on this computer. It is displaying only partial help.
-- To download and install Help files for the module that includes this cmdlet, use Update-Help.
-- To view the Help topic for this cmdlet online, type: "Get-Help Set-Service -Online" or
go to http://go.microsoft.com/fwlink/?LinkID=113399.

https://msdn.microsoft.com/powershell/reference/5.1/microsoft.powershell.management/Set-Service

Example

Set-Service -Name LBS_MONITOR -DisplayName Batch4.3_SourceCode

Delete Service

https://docs.microsoft.com/en-us/powershell/module/microsoft.powershell.management/new-service?view=powershell-5.1

To delete a service, use Sc.exe, or use the Get-WmiObject cmdlet to get the Win32_Service object that represents the service and then use the Delete method to delete the service. The object that Get-Service returns does not have a delete method.

PS C:\> sc.exe delete TestService     删除service之后,需要关闭任务管理器或service查看器,否则new-service的时候会出错
- or -
PS C:\> (Get-WmiObject win32_service -Filter "name='TestService'").delete()

This example shows two ways to delete the TestService service. The first command uses the delete option of Sc.exe. The second command uses the Delete method of the Win32_Service objects that Get-WmiObject returns.

Example

Get-WmiObject win32_service -Filter "name='LBS_MONITOR'" 加个括号,再调用delete

执行失败

__GENUS : 2
__CLASS : __PARAMETERS
__SUPERCLASS :
__DYNASTY : __PARAMETERS
__RELPATH :
__PROPERTY_COUNT : 1
__DERIVATION : {}
__SERVER :
__NAMESPACE :
__PATH :
ReturnValue : 2
PSComputerName :

执行成功

__GENUS : 2
__CLASS : __PARAMETERS
__SUPERCLASS :
__DYNASTY : __PARAMETERS
__RELPATH :
__PROPERTY_COUNT : 1
__DERIVATION : {}
__SERVER :
__NAMESPACE :
__PATH :
ReturnValue : 0
PSComputerName :

Remarks

PowerShell的命令,可以使用tab进行补全。

输入start-s然后tab键,两次tab后,会出现Start-Service。

然后空格,再输入服务的开头字母,再进行tab,可以通过自动补全,找到对应的服务

安装服务http://www.cnblogs.com/chucklu/p/7150420.html

https://social.technet.microsoft.com/wiki/contents/articles/36875.powershell-managing-service-s-using-wmi-cim-and-get-service-cmdlet.aspx

CIM

https://technet.microsoft.com/en-us/itpro/powershell/windows/cimcmdlets/get-ciminstance

Get-CimInstance -ClassName Win32_Service

Get-CimInstance -ClassName Win32_Service -Filter "name like 'L%'" | select Name,DisplayName,StartMode,State,StartName,SystemName,Description |Format-Table -AutoSize

批量停止service

Get-Service | Where-Object {$_.name.Contains("LISA_43")} | Stop-Service

PS C:\Users\clu\Desktop> Get-Service | Where-Object {$_.name.Contains("SQL2014")} | Select Status,Name,DisplayName

Status Name DisplayName
------ ---- -----------
Running MSOLAP$SQL2014 SQL Server Analysis Services (SQL2014)
Running MSSQL$SQL2014 SQL Server (SQL2014)
Running MSSQLFDLauncher$SQL2014 SQL Full-text Filter Daemon Launcher (SQL2014)
Running ReportServer$SQL2014 SQL Server Reporting Services (SQL2014)
Stopped SQLAgent$SQL2014 SQL Server Agent (SQL2014)

PS C:\Users\clu\Desktop> Get-Service | Where-Object {$_.name.Contains("SQL2014")} | Select Status,Name,DisplayName | Res
tart-Service
WARNING: Waiting for service 'SQL Server Reporting Services (SQL2014) (ReportServer$SQL2014)' to stop...
WARNING: Waiting for service 'SQL Server Reporting Services (SQL2014) (ReportServer$SQL2014)' to stop...
WARNING: Waiting for service 'SQL Server Reporting Services (SQL2014) (ReportServer$SQL2014)' to start...

最新文章

  1. sql server 日期
  2. 双十一来了,别让你的mongodb宕机了
  3. codeforces 724B Batch Sort(暴力-列交换一次每行交换一次)
  4. Nginx 支持 WAF 防护功能实战
  5. golang AES/ECB/PKCS5 加密解密 url-safe-base64
  6. [oracle] listener.ora 、sqlnet.ora 、tnsnames.ora
  7. 倒水问题 (codevs 1226) 题解
  8. Linux编程学习笔记 -- Process
  9. 【转贴】gdb中的信号(signal)相关调试技巧
  10. iptables 添加,删除,查看,修改
  11. sql server 2000 下载地址
  12. 【ecos学习1】wmware运行redboot[方法一]--脚本实现配置
  13. windows phone 页面传值(7)
  14. EXCEL 两人的建立Y轴
  15. Android的ViewAnimator及其子类ViewSwitcher-android学习之旅(三十三)
  16. nodejs中处理回调函数的异常
  17. 第2章 Java基本语法(下): 流程控制--项目(记账本)
  18. [13]Windows 内核情景分析 --- 网络通信
  19. 纯MATLAB版本 SIFT代码
  20. VBScript操作SFTP

热门文章

  1. (2016北京集训十三)【xsy1532】网络战争 - 最小割树+树上倍增+KD树
  2. 05004_Linux的其他命令和权限命令
  3. word break相关问题的解法
  4. TreeMap集合怎样依照Value进行排序
  5. bzoj1934: [Shoi2007]Vote 善意的投票(显然最小割)
  6. Linux下离线安装MySQL
  7. Conditionals
  8. python-logging写日志编码问题
  9. fixed说明
  10. C#中Request.Cookies 和 Response.Cookies 的区别分析