zoukankan      html  css  js  c++  java
  • Restart-ServiceEx.psm1

    详细描述
    利用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:psmodulesRestart-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
  • 相关阅读:
    魔控(电脑遥控器)
    百度网盘不限速下载网页版
    2019计算机科学与技术实训认识以及总结
    压缩文件破解
    废旧手机改造第二弹之电脑扩展屏幕和变成复制屏幕
    废旧手机改造之家居监控器
    关于志愿填报的一点点东西(大佬对计算机专业认识)
    html恶搞之无限弹窗
    java重点知识点整理
    推荐一个学java的网站
  • 原文地址:https://www.cnblogs.com/edward2013/p/3502057.html
Copyright © 2011-2022 走看看