详细描述
利用WMI的Win32_Service类重启指定计算机上的服务.
Restart-ServiceEx cmdlet 通过WMI的Win32_Service类向指定计算机(ComputerName)的Windows服务控制器, 为每个指定的服务发送一个停止消息, 再接着发送一个启动消息. 如果服务已经停, 那么将会被直接启动. 你可以通过服务名称(Name)或显示名称(DisplayName)来指定需要重新启动的服务.
 
语法
 Restart-ServiceEx { [-Name] <string[]> | [-DisplayName] <string[]> } [-ComputerName] <string[]>
 
示例
C:PS> import-module c:\ps\modules\Restart-ServiceEx.psm1
C:PS> Restart-ServiceEx AdobeARMservice,DcomLaunch -ComputerName server01 Stopping 'Adobe Acrobat Update Service' on SERVER01.
'Adobe Acrobat Update Service' on SERVER01 was stopped successfully!
Starting 'Adobe Acrobat Update Service' on server01.
'Adobe Acrobat Update Service' on SERVER01 was started successfully! Stopping 'DCOM Server Process Launcher' on SERVER01
WARNING: Service Cannot Accept Control

Restart-ServiceEx.psm1

function Restart-ServiceEx {
[CmdletBinding(DefaultParameterSetName="Name")]
param(
[Parameter(ParameterSetName="Name",Position=0,ValueFromPipeline=$true)]
[string[]]$Name,
[Parameter(ParameterSetName="DisplayName",Position=0,ValueFromPipeline=$true)]
[string[]]$DisplayName,
[string[]]$ComputerName=$env:COMPUTERNAME
) # create list of clear text error messages
$ErrorCode = 'Success,Not Supported,Access Denied,Dependent Services Running,Invalid Service Control'
$ErrorCode += ',Service Cannot Accept Control, Service Not Active, Service Request Timeout'
$ErrorCode += ',Unknown Failure, Path Not Found, Service Already Running, Service Database Locked'
$ErrorCode += ',Service Dependency Deleted, Service Dependency Failure, Service Disabled'
$ErrorCode += ',Service Logon Failure, Service Marked for Deletion, Service No Thread'
$ErrorCode += ',Status Circular Dependency, Status Duplicate Name, Status Invalid Name'
$ErrorCode += ',Status Invalid Parameter, Status Invalid Service Account, Status Service Exists'
$ErrorCode += ',Service Already Paused' $Services = @()
switch($PSCmdlet.ParameterSetName) {
"Name" {
$Name | % {
$s = Get-WmiObject win32_service -ComputerName $ComputerName -Filter "Name=`"$_`""
if($s) { $Services += $s }
}
if(!$Services) { return "Can not found any service of the name '{0}'" -f $($Name -join "','") }
}
"DisplayName" {
$DisplayName | % {
$s = Get-WmiObject win32_service -ComputerName $ComputerName -Filter "DisplayName=`"$_`""
if($s) { $Services += $s }
}
if(!$Services) { return "Can not found any service of the displayname '{0}'" -f $($DisplayName -join "','") }
}
} for($i=0; $i -lt $Services.length; $i++) {
if($Services[$i].Started) {
Write-Host $("Stopping '{0}' on {1}" -f $Services[$i].DisplayName,$Services[$i].__SERVER) -NoNewline
$rv = $Services[$i].StopService().ReturnValue
if ($rv) { Write-Host "";Write-Warning $("{0}`n" -f $ErrorCode.Split(',')[$rv]);continue }
do {
Write-Host "." -NoNewline
Start-Sleep -s 10
$service = Get-WmiObject win32_service -ComputerName $Services[$i].__SERVER -Filter "DisplayName=`"$($Services[$i].DisplayName)`""
}while($service.State -ne "Stopped")
Write-Host $("`n'{0}' on {1} was stopped successfully!" -f $Services[$i].DisplayName,$Services[$i].__SERVER) Write-Host $("Starting '{0}' on {1}" -f $Services[$i].DisplayName,$Services[$i].__SERVER) -NoNewline
$rv = $Services[$i].StartService().ReturnValue
if ($rv) { Write-Host "";Write-Warning $("{0}`n" -f $ErrorCode.Split(',')[$rv]);continue }
do {
Write-Host "." -NoNewline
Start-Sleep -s 10
$service = Get-WmiObject win32_service -ComputerName $Services[$i].__SERVER -Filter "DisplayName=`"$($Services[$i].DisplayName)`""
}while($service.State -ne "Running")
Write-Host $("`n'{0}' on {1} was started successfully!`n" -f $Services[$i].DisplayName,$Services[$i].__SERVER)
}
else {
Write-Host $("Starting '{0}' on {1}" -f $Services[$i].DisplayName,$Services[$i].__SERVER) -NoNewline
$rv = $Services[$i].StartService().ReturnValue
if ($rv) { Write-Host "";Write-Warning $("{0}`n" -f $ErrorCode.Split(',')[$rv]);continue }
do {
Write-Host "." -NoNewline
Start-Sleep -s 10
$service = Get-WmiObject win32_service -ComputerName $Services[$i].__SERVER -Filter "DisplayName=`"$($Services[$i].DisplayName)`""
}while($service.State -ne "Running")
Write-Host $("`n'{0}' on {1} was started successfully!`n" -f $Services[$i].DisplayName,$Services[$i].__SERVER)
}
}
} Export-ModuleMember -function Restart-ServiceEx

Restart-ServiceEx.psm1的更多相关文章

  1. OpenGL ES 3.0: 图元重启(Primitive restart)

    [TOC] 背景概述 在OpenGL绘制图形时,可能需要绘制多个并不相连的图形.这样的情况下这几个图形没法被当做一个图形来处理.也就需要多次调用 DrawArrays 或 DrawElements. ...

  2. 关于点击Invalidate Caches/Restart禁止插件后,重新加载--Android Studio

    1:47:27 Plugin Error Problems found loading plugins: Plugin "Google Analytics Uploader" wa ...

  3. Tomcat重启脚本restart.sh停止脚本stop.sh

    Tomcat重启脚本restart.sh停止脚本stop.sh Tomcat本身提供了 startup.sh(启动)shutdown.sh(关闭)脚本,我们在部署中经常会出现死进程形象,无法杀掉进程需 ...

  4. ubuntu14.04服务版/etc/init.d/smbd restart无效的解决方法

    刚装的ubuntu14.04配置完smbd发现service或者/etc/init.d/smbd restart都不显示任何输出,也没起作用 echo $?输出1,查看脚本发现 if init_is_ ...

  5. shell来start、stop、restart应用程序模板

    这里使用shell中的case语法: case分支语句格式如下: case $变量名 in 模式1) 命令列表 ;; 模式2) 命令列表 ;; *) ;; esac case行尾必须为单词“in”,每 ...

  6. 虚拟机service network restart没有反应解决方法

      一般我们新copy的虚拟机或新克隆的虚拟机第一次启动时都会出现没有ip地址的情况: [root@zejin243 network-scripts]# ifconfig lo        Link ...

  7. ADB not responding. If you'd like to retry, then please manually kill "adb.exe" and click 'Restart'

    ADB not responding. If you'd like to retry, then please manually kill "adb.exe" and click ...

  8. configs for postgresql restart and postgresql reload

    -- configs requiring postgresql restart select name, setting, context from pg_settings where context ...

  9. 把Nginx加为系统服务(service nginx start/stop/restart)

    1.编写脚本,名为nginx #!/bin/sh # # nginx - this script starts and stops the nginx daemon # # chkconfig: - ...

  10. ADB not responding. You can wait more,or kill"abd.exe" process manually and click 'Restart'

    在使用Android Studio进行开发的过程中,有时候编译运行时,会出现如下提示: ADB not responding. You can wait more,or kill"abd.e ...

随机推荐

  1. Matlab norm 用法小记

    Matlab norm 用法小记 matlab norm (a) 用法以及实例 norm(A,p)当A是向量时norm(A,p)   Returns sum(abs(A).^p)^(1/p), for ...

  2. Swift入门系列--Swift官方文档(2.2)--中文翻译--About Swift 关于Swift

    About Swift 关于Swift 官方文档的翻译,仅供参考,本人英语本就不好,边学边翻译,不喜勿喷. Swift is a new programming language for iOS, O ...

  3. KeilC51常用功能模块使用说明

    本文档包括单片机系统中常用到的时钟中断.通讯及键盘扫描等模块(见所附源程序)的说明.这些模块使用前后台系统模型.为达到最大的灵活性, 需要在用户工程中定义config.h文件, 在其中定义各模块可选参 ...

  4. TCP协议的3次握手与4次挥手过程详解

    1.前言 尽管TCP和UDP都使用相同的网络层(IP),TCP却向应用层提供与UDP完全不同的服务.TCP提供一种面向连接的.可靠的字节流服务. 面向连接意味着两个使用TCP的应用(通常是一个客户和一 ...

  5. div+css页面居中代码

    普通div和html混写可以这样写,设置 text-align:center, margin:0px auto 最简单的方法就是 把你的body这样设置: <body style="t ...

  6. oracle的存储过程语法(转)

    1.ORA-00942: table or view does not exist 指的你要操作的表尚未存在,需要先create出来先. 2.ORA-00922: missing or invalid ...

  7. 修改表增加字段默认值default

    对个生产库的表增加1个字段.字段类型是INT型, 表数据有2千万条, alter table table_name add xxoo number(4) default  0 ; 因此 不仅要修改字典 ...

  8. HDU-1236 排名

    http://acm.hdu.edu.cn/showproblem.php?pid=1236 学会怎样按字典序排序的模板. 排名 Time Limit: 2000/1000 MS (Java/Othe ...

  9. tomcat 配置内存相关

    今天早上 ,tomcat 网站页面上出现报错问题.最后还是一位同事解决的,这里记录一下. 1.看了一下页面,他说是内存溢出. 首先找到 双击 Tomw.exe 出现如下图 然后需要配置堆栈大小

  10. js 中 json对象 与 json字符串 间相互转换

    在数据传输过程中,json是以文本,即字符串的形式传递的,而JS操作的是JSON对象,所以,JSON对象和JSON字符串之间的相互转换是关键 JSON字符串:  var str1 = '{ " ...