westlife
Advanced Member | Редактировать | Профиль | Сообщение | Цитировать | Сообщить модератору Код: # Проверка состояния служб Function Check-State-Service { [CmdletBinding( SupportsShouldProcess = $false )] [OutputType([string],[bool])] Param( [Parameter( Mandatory = $true, Position = 0 )] [Alias( 'Name' )] [ValidateNotNullOrEmpty()] [string] $ServiceName , [Parameter( Mandatory = $true, Position = 1, ParameterSetName = 'State' )] [ValidateSet( 'DelayedAuto', 'Automatic', 'Manual', 'Disabled' )] [string] $Need , [Parameter( Mandatory = $false, Position = 2, ParameterSetName = 'State' )] [ValidateSet( 'DelayedAuto', 'Automatic', 'Manual', 'Disabled' )] [string] $Default # Параметр по умолчанию, Нужен только для информации, истории. , [Parameter( Mandatory = $false, ParameterSetName = 'Exist' )] [switch] $CheckExist , [Parameter( Mandatory = $false )] [ValidateSet( 'Result', 'Value', 'Bool' )] [string] $Return = 'Result' ) # Также добавляем тип ServiceProcess из .NET к текущему сеансу, для управления службами. if ( -not ( 'System.ServiceProcess.ServiceController' -as [type] )) { Add-Type -AssemblyName 'System.ServiceProcess' -ErrorAction Stop } [psobject] $isStartupType = $null [int] $isDelayedValue = 0 [string] $ColorSVC = 'Red' [string] $StateSVC = '#Red#Ошибка #' [bool] $ResultState = $false $OFS = ' ' $Service = [System.ServiceProcess.ServiceController]::new($ServiceName) # Если контроллер служб получил пустые данные. if ( -not $Service.Name ) { [string] $StateSVC = "#DarkGray#Отсутствует#" if ( $CheckExist ) { if ( $Return -eq 'Result' ) { $StateSVC } else { $false } Return } if ( $Return -eq 'Result' ) { "$StateSVC" } elseif ( $Return -eq 'Value' ) { $isStartupType } else { $ResultState } Return } elseif (( -not $Service.ServiceType ) -or ( $Service.ServiceType -like '*Driver*' )) { $StateSVC = "#DarkGray#Не служба #" if ( $CheckExist ) { if ( $Return -eq 'Result' ) { $StateSVC } else { $false } Return } if ( $Return -eq 'Result' ) { "$StateSVC" } elseif ( $Return -eq 'Value' ) { $isValue } else { $ResultState } Return } elseif ( $CheckExist ) { $StateSVC = "#Green#Существует#" if ( $Return -eq 'Result' ) { $StateSVC } else { $true } Return } $Service.Close() # Указываем раздел служб в реестре. [string] $ServicePath = 'SYSTEM\CurrentControlSet\Services' try { [int] $isStartupType = [Microsoft.Win32.Registry]::LocalMachine.OpenSubKey( "$ServicePath\$ServiceName",'ReadSubTree','QueryValues').GetValue('Start',0) } catch {} try { [int] $isDelayedValue = [Microsoft.Win32.Registry]::LocalMachine.OpenSubKey( "$ServicePath\$ServiceName",'ReadSubTree','QueryValues').GetValue('DelayedAutoStart',0) } catch {} if (( $isStartupType -eq 2 ) -and ( $isDelayedValue -eq 1 )) { $isStartupType = 1 } if ( 1 -eq $isStartupType ) { [string] $isStartupType = 'DelayedAuto' } elseif ( 2 -eq $isStartupType ) { [string] $isStartupType = 'Automatic' } elseif ( 3 -eq $isStartupType ) { [string] $isStartupType = 'Manual' } elseif ( 4 -eq $isStartupType ) { [string] $isStartupType = 'Disabled' } if ( $isStartupType -eq $Need ) { $ColorSVC = 'Green' ; $ResultState = $true } else { $ColorSVC = 'Yellow' } if ( 'DelayedAuto' -eq $isStartupType ) { $StateSVC = "#$ColorSVC#Авто-Отлож #" } elseif ( 'Automatic' -eq $isStartupType ) { $StateSVC = "#$ColorSVC#Авто #" } elseif ( 'Manual' -eq $isStartupType ) { $StateSVC = "#$ColorSVC#Вручную #" } elseif ( 'Disabled' -eq $isStartupType ) { $StateSVC = "#$ColorSVC#Отключена #" } if ( $Return -eq 'Result' ) { [string] "$StateSVC" } elseif ( $Return -eq 'Value' ) { [string] $isStartupType } else { [bool] $ResultState } } if ( -not ( 'System.ServiceProcess.ServiceController' -as [type] )) { Add-Type -AssemblyName 'System.ServiceProcess' -ErrorAction Stop } [System.ServiceProcess.ServiceController]::GetServices() | Sort-Object -Property DisplayName | Format-Table -Property ` @{ Name = 'Отобр. Имя' ; e = { "{0}" -f $_.DisplayName.PadRight(70,' ').Substring(0,70) }}, @{ Name = 'Имя' ; e = { $_.Name }}, @{ Name = 'Тип запуска' ; e = { Check-State-Service $_.Name -Default Automatic -Need Disabled -Return Value }} -AutoSize | | Всего записей: 1390 | Зарегистр. 12-12-2006 | Отправлено: 16:01 28-11-2019 | Исправлено: westlife, 16:06 28-11-2019 |
|