详细描述
利用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. Arduino从基础到实践第三章练习题

    先写在这里,还没经过测试. 1. LED两端往中间移动,到中间后向两边返回. // adr301.ino , , , , , , , , , }; ); ; ; unsigned long chang ...

  2. 这篇blog只是为了发一张图链到UOJ的博客去..

    UOJ卖萌表情,萌萌哒VFK如图.

  3. Python、Lua和Ruby——脚本大P.K.

    转自Python.Lua和Ruby--脚本大P.K. Python versus Lua Versus Ruby Python.Lua和Ruby--脚本大P.K. Tom Gutschmidt 著 赖 ...

  4. linux RWT

    http://www.cnblogs.com/qlwy/archive/2011/06/26/2121919.html#undefined

  5. android 自定义命名空间

    一.统一的用户界面是可以使得应用程序更友好.要做到用户界面的统一,我们就必须用到风格(style)和主题(theme).自定义一个View的方法步骤如下:1.首先,在values文件夹下定义一个att ...

  6. 【转】linux(Ubuntu)配置svn仓库,搭建svn服务器

    原文网址:http://blog.1v2d.com/322.html 在家里搞了好久,终于搞出来,并且在线上已经成功搭建成功,在这感谢一个博主的文章,本篇文章也主要是转载他的内容,写的非常好,而且非常 ...

  7. (转载)shell变量基础—shell自定义变量

    (转载)http://see.xidian.edu.cn/cpp/html/1494.html 一.Shell定义变量需要遵循的规则 Shell编程中,使用变量无需事先声明,同时变量名的命名须遵循如下 ...

  8. YUV像素和ycbcr

    一幅彩色图像的基本要素是什么? 说白了,一幅图像包括的基本东西就是二进制数据,其容量大小实质即为二进制数据的多少.一幅1920x1080像素的YUV422的图像,大小是1920X1080X2=4147 ...

  9. hdu 4405概率dp

    #include <cstdio> #include <cstring> #include <iostream> #include <cmath> #i ...

  10. openStack云平台虚拟桌面galera mysql 3节点集群实例实战 调试完成

    被复制节点: /usr/local/mysql/bin/mysqld_safe --wsrep_cluster_address=gcomm:// >/dev/null &复制节点01: ...