| Server IP : 123.56.80.60 / Your IP : 216.73.216.78 Web Server : Apache/2.4.54 (Win32) OpenSSL/1.1.1s PHP/7.4.33 mod_fcgid/2.3.10-dev System : Windows NT iZhx3sob14hnz7Z 10.0 build 14393 (Windows Server 2016) i586 User : SYSTEM ( 0) PHP Version : 7.4.33 Disable Function : NONE MySQL : OFF | cURL : ON | WGET : OFF | Perl : OFF | Python : OFF | Sudo : OFF | Pkexec : OFF Directory : C:/Windows/diagnostics/system/WindowsUpdate/ |
Upload File : |
# Copyright © 2012, Microsoft Corporation. All rights reserved.
#*================================================================================
# Functions
# Servicer
# SvcStateChange
# Get-ServiceStart
# WaitFor-ServiceStatus
# Check-ServiceRunning
# Fix-Service
#*================================================================================
#Servicer
#*================================================================================
function Servicer {
param($SrvcName = $(throw "need name of service"), $state = $("Need state Running or Stopped"))
$z = get-service #initialize
$Svc = Get-Service $SrvcName
if($Svc.status -eq $state)
{write-debug "no change needed"}
if($Svc.status -ne $state)
{
switch($state)
{
"Running" {Start-Service $SrvcName;
if((SvcStateChange -test $Svc -goal 'Running' -til 9))
{ #Write-Debug "Service $SrvcName did start"
return $true}
}
"Stopped" {Stop-Service -Force $SrvcName;
if((SvcStateChange -test $Svc -goal 'Stopped' -til 9))
{ #Write-Debug "Service $SrvcName did stop"
return $true}
}
default {Get-Service $SrvcName;
return $false
}
}
}
}
#*================================================================================
#SvcStateChange
#*================================================================================
Function SvcStateChange {
param($test, $goal, $til)
$atEnd = $false
$range = 1..$til
while(-not ($test.status -eq $goal) -and (-not $atEnd))
{
$range | foreach-object { #Write-Debug $_
sleep ($_ * 1.1);
$test.refresh()
if($test.status -eq $goal)
{continue}
}
$atEnd = $true
}
if($test.status -eq $goal)
{
return $true
}
else
{
return $false
}
}
#*================================================================================
#Get-ServiceStartup
#*================================================================================
function Get-ServiceStartup([Object]$objService)
{
$path = "HKLM:\SYSTEM\CurrentControlSet\Services\" + $objService.Name
if(Test-path $path)
{
$StartupType = (Get-ItemProperty -Path $path -ea silentlycontinue).start
}
Switch($StartupType)
{
0 { $TypeName = "Boot" }
1 { $TypeName = "System" }
2 { $TypeName = "Automatic" }
3 { $TypeName = "Manual" }
4 { $TypeName = "Disabled" }
default { $TypeName = "Error" }
}
Switch($StartupType)
{
0 { $TypeDescription = "Loaded by kernel loader. Components of the driver stack for the boot (startup) volume must be loaded by the kernel loader." }
1 { $TypeDescription = "Loaded by I/O subsystem. Specifies that the driver is loaded at kernel initialization." }
2 { $TypeDescription = "Loaded by Service Control Manager. Specifies that the service is loaded or started automatically." }
3 { $TypeDescription = "The service does not start until the user starts it manually, such as by using Services or Devices in Control Panel. " }
4 { $TypeDescription = "Specifies that the service should not be started." }
default { $TypeDescription = "There was an error retrieving information about this service." }
}
return $TypeName
}
#*================================================================================
#WaitFor-ServiceStatus
#*================================================================================
function WaitFor-ServiceStatus([string]$serviceName=$(throw "No service name is specified"),$serviceStatus=$(throw "No service status is specified"))
{
$Service = Get-Service | Where-Object {$_.Name -eq $ServiceName}
if($Service -ne $null)
{
[TimeSpan]$timeOut = New-Object TimeSpan(0,0,0,5,0)
$Service.WaitForStatus($serviceStatus, $timeOut)
}
}
#*================================================================================
#IsServiceAutoandRunning
#*================================================================================
function IsServiceNotAutoandRunning()
{
param($servicename)
$objService = Get-Service | Where-Object {$_.Name -eq $servicename}
if($objService -ne $null)
{
$ServiceStatus = $objService.Status
$StartupType = Get-ServiceStartup $objService
if($ServiceStatus -ne "Running" -or $StartupType -ne "Automatic") { $svcproblem = $true }
}
return $svcproblem
}
#*================================================================================
#Check-ServiceRunning
#*================================================================================
function Check-ServiceRunning($ServiceName)
{
$RunningStatus = $false
$Service = Get-Service | Where-Object {$_.Name -eq $ServiceName}
if( $Service -ne $null)
{
$RunningStatus = ($Service.Status -eq "Running")
}
else
{
[string]$DebugString = 'Check-ServiceRunning Warning: Service ' + $ServiceName + ' not found.'
Write-Debug $DebugString
}
return $RunningStatus
}
#*================================================================================
#Fix-Service
#*================================================================================
function Fix-Service($ServiceName)
{
$Service = Get-Service | Where-Object {$_.Name -eq $ServiceName}
if ($Service -ne $null)
{
Set-Service -Name $ServiceName -StartupType Automatic -ErrorAction Stop
Start-Service $ServiceName -ErrorAction Stop
WaitFor-ServiceStatus $ServiceName 'Running'
}
else
{
[string]$DebugString = 'Fix-Service Warning: Service ' + $ServiceName + ' not found.'
Write-Debug $DebugString
}
}
#*================================================================================
#Fix-Service with debug in a file
#*================================================================================
function Fix-ServiceWithDebugFile($ServiceName,$debugFile,$message)
{
trap [Exception] {
[string]$strerror = ("$message"+($_.Exception.Message))
$strerror >> $debugFile
continue;
}
$Service = Get-Service | Where-Object {$_.Name -eq $ServiceName}
if ($Service -ne $null)
{
Set-Service -Name $ServiceName -StartupType Automatic
Start-Service $ServiceName
WaitFor-ServiceStatus $ServiceName 'Running'
("$message : $ServiceName "+( (get-service $servicename).status ) ) >> $debugFile
}
else
{
[string]$DebugString = 'Fix-Service Warning: Service ' + $ServiceName + ' not found.'
$DebugString >> $debugFile
}
}
#*================================================================================
#SetServiceRunning
#*================================================================================
function SetServiceRunning()
{
Param($svc,[switch]$nostop,$scriptname)
sc.exe sdset $svc "D:(A;;CCLCSWRPWPDTLOCRRC;;;SY)(A;;CCDCLCSWRPWPDTLOCRSDRCWDWO;;;BA)(A;;CCLCSWLOCRRC;;;AU)(A;;CCLCSWRPWPDTLOCRRC;;;PU)"
sc.exe config $svc start= auto
if(!$nostop)
{
&{
Servicer $svc "Stopped"
} trap [Exception]{
[string]$str = ($DC_Strings.ID_ERROR_MSG_SERVICE).replace("%ServiceName%",$svc)
$str | convertto-xml | update-diagreport -id $scriptname -name "$str" -verbosity informational
}
}
&{
Fix-Service $svc
} trap [Exception]{
[string]$str = ($DC_Strings.ID_ERROR_MSG_SERVICE).replace("%ServiceName%",$svc)
$str | convertto-xml | update-diagreport -id $scriptname -name "$str" -verbosity informational
}
}