详细描述
利用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. leetcode power(x,n)

    class Solution { public: double pow(double x, int n) { double a=1; if(n==0)return 1; if(x==1)return ...

  2. bzoj 1051: [HAOI2006]受欢迎的牛 tarjan缩点

    1051: [HAOI2006]受欢迎的牛 Time Limit: 10 Sec  Memory Limit: 162 MBSubmit: 2092  Solved: 1096[Submit][Sta ...

  3. iOS出现 Undefined symbols for architecture armv7 std::basic_string<char, std::char_traits<char>

    Undefined symbols for architecture i386: “_OBJC_CLASS_$_XXX”, referenced from: objc-class-ref in XXX ...

  4. Feel Good

    poj2796:http://poj.org/problem?id=2796 题意:给出一个长度为n(n<100000)的序列,求出一个子序列,使得这个序列中的最小值乘以这个序列的和的值最大. ...

  5. FTP配置和用户设置权限

    http://www.cnblogs.com/xcxc/archive/2013/01/25/2876749.html ---------------------------------------- ...

  6. 【BZOJ2434-[Noi2011]】阿狸的打字机(AC自动机(fail树)+离线+树状数组)

    Description 阿狸喜欢收藏各种稀奇古怪的东西,最近他淘到一台老式的打字机.打字机上只有28个按键,分别印有26个小写英文字母和'B'.'P'两个字母. 经阿狸研究发现,这个打字机是这样工作的 ...

  7. mysql 服务意外停止1067错误解决办法小结

    今天在配置服务器时安装mysql5.5总是无法安装,查看日志错误提示为1067错误,下面来看我的解决办法 事件类型: 错误 事件来源: Service Control Manager 事件种类: 无  ...

  8. 算法-KMP模式匹配算法

    1朴素算法:逐个比较 2 主要是解决多余比较的麻烦,通过处理比较字符串是否含有重复的字符的问题.

  9. 【HDOJ】1811 Rank of Tetris

    并查集+拓扑排序.使用并查集解决a = b的情况. #include <iostream> #include <cstdio> #include <cstring> ...

  10. 数学(概率):HNOI2013 游走

    [题目描述] 一个无向连通图,顶点从1编号到N,边从1编号到M. 小Z在该图上进行随机游走,初始时小Z在1号顶点,每一步小Z以相等的概率随机选 择当前顶点的某条边,沿着这条边走到下一个顶点,获得等于这 ...