Restart-WindowsService.ps1
<#PSScriptInfo
.VERSION 1.0.0 .GUID 4bc2cff2-107f-4da4-bc12-21e952b0f745 .AUTHOR saw-friendship .COPYRIGHT saw-friendship .TAGS Windows WMI Remote Service Restart #> <# .DESCRIPTION Restart Windows Service over Wmi on local and remote system .LINK https://sawfriendship.wordpress.com/ .EXAMPLE Restart-WindowsService -Name winr*,term* -ComputerName dc-01,dc-02 PSComputerName Action Name DisplayName -------------- ------ ---- ----------- DC-01 None TermService Службы удаленных рабочих столов DC-01 Restart TermServLicensing Лицензирование удаленных рабочих столов DC-01 Restart WinRM Служба удаленного управления Windows... DC-02 None TermService Службы удаленных рабочих столов DC-02 Restart WinRM Служба удаленного управления Windows... .EXAMPLE C:\Users\a.timoshenko\Desktop\Restart-WindowsService.ps1 -Name winr*,term* -ComputerName dc-01,dc-02 -PassThru ExitCode : 0 Name : TermServLicensing ProcessId : 11988 StartMode : Auto State : Running Status : OK ExitCode : 0 Name : WinRM ProcessId : 728 StartMode : Auto State : Running Status : OK ExitCode : 0 Name : WinRM ProcessId : 940 StartMode : Auto State : Running Status : OK .EXAMPLE Get-ADComputer -Filter {name -like 'rd*'} | select DNSHostName | C:\Users\a.timoshenko\Desktop\Restart-WindowsService.ps1 -Name winr* PSComputerName Action Name DisplayName -------------- ------ ---- ----------- RDBROKER-01 Restart WinRM Служба удаленного управления Windows (WS-Management) RDBROKER-02 Restart WinRM Служба удаленного управления Windows (WS-Management) RDHOST-01 Restart WinRM Служба удаленного управления Windows (WS-Management) RDHOST-02 Restart WinRM Служба удаленного управления Windows (WS-Management) RDPROFILES Restart WinRM Служба удаленного управления Windows (WS-Management) RDWEB-01 Restart WinRM Служба удаленного управления Windows (WS-Management) #> [CmdletBinding()] param( [string[]]$Name = @('*'), [Parameter(ValueFromPipelineByPropertyName=$true)][alias("DNSHostName")]$ComputerName = '.', [PSCredential]$Credential, [switch]$PassThru ) BEGIN { $LocalHosts = @( '.', 'localhost', $env:COMPUTERNAME, ($env:COMPUTERNAME,(Get-WmiObject -Class Win32_ComputerSystem).Domain -join '.') ) [string]$Filter = 'Name like "' + ( @($Name | % {$_ -replace @('\*','%')}) -join '" or Name like "' ) + '"' [string]$ErrorVariable = 'WmiRequestError' } PROCESS { Foreach ($Comp in $ComputerName) { $param = @{ 'ComputerName' = $Comp 'Class' = 'Win32_Service' 'Filter' = $Filter 'ErrorVariable' = $ErrorVariable } if ($Credential -and ($Comp -notin $LocalHosts)) { $param.Credential = $Credential } try{ Get-WmiObject @param | % { [string]$Action = 'None' $InvokeStop = $_ | Invoke-WmiMethod -Name StopService -ErrorVariable $ErrorVariable if ($InvokeStop.ReturnValue -eq 0) { $Action = 'Stop' } Start-Sleep -s 1 $InvokeStart = $_ | Invoke-WmiMethod -Name StartService -ErrorVariable $ErrorVariable if ($InvokeStart.ReturnValue -eq 0 -and $Action -eq 'None') { $Action = 'Start' } elseif ($InvokeStart.ReturnValue -eq 0 -and $Action -eq 'Stop') { $Action = 'Restart' } if ($PassThru) { if ($Action -eq 'Start' -or $Action -eq 'Restart') {$_} } else { $_ | Select-Object -Property PSComputerName,@{'Name'='Action';'Expression'={$Action}},Name,DisplayName } } } catch { $WmiRequestError break } } } END {} |