详细描述
利用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. iPhone手机VPN设置

    如果iPhone,iPad游戏或软件服务器在国外不能用,就需要设置VPN了. 如果是为了解除公司上网策略限制,或者为了上Google,Facebook,都可以通过设置VPN实现. 要使用VPN需要到V ...

  2. 【 UVALive - 4287】Proving Equivalences (SCC缩点)

    题意: 给出N个命题,要求你证明这N个命题的等价性 比如有4个命题a,b,c,d,我们证明a<->b, b<->c,c<->d,每次证明都是双向的,因此一共用了6次 ...

  3. Nginx缓存配置及nginx ngx_cache_purge模块的使用

    ngx_cache_purge模块的作用:用于清除指定url的缓存 下载地址:http://labs.frickle.com/files/ngx_cache_purge-2.3.tar.gz   1. ...

  4. (转载)php获取mysql版本的几种方法小结

    (转载)http://www.jb51.net/article/13930.htm 查询当前连接的MYSQL数据库的版本,可以用下面SQL语句来实现 select VERSION(); 当前$res= ...

  5. (转载)Mysql中,SQL语句长度限制

    (转载)http://qjoycn.iteye.com/blog/1288435 今天发现了一个错误:Could not execute JDBC batch update 最后发现原因是SQL语句长 ...

  6. Linux学习笔记26——信号量

    一 信号量的基本概念 信号量:它是一个特殊变量,只允许对它进行等待和发送信号这两种操作. 假设有一个信号量变量sv P(sv):用于等待,如果sv的值大于零,就给它减去1,如果它的值等于零,就挂起该进 ...

  7. [IoLanguage]Io Tutorial[转]

    Io Tutorial   Math   Io> 1+1 ==> 2 Io> 2 sin ==> 0.909297 Io> 2 sqrt ==> 1.414214 ...

  8. 定时器NSTimer的用法

        //时间间隔     NSTimeInterval activeTimeInterval = NETWORK_SEND_ACTIVE_TIME;     NSTimeInterval othe ...

  9. Poj 3580-SuperMemo Splay

    题目:http://poj.org/problem?id=3580   SuperMemo Time Limit: 5000MS   Memory Limit: 65536K Total Submis ...

  10. shiro能做什么,做j2ee时候要考虑什么

    转载: http://jinnianshilongnian.iteye.com/blog/2018398,         感谢原作者 <跟我学Shiro>PDF完结版下载 博客分类: 跟 ...