详细描述
利用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