mirror of
https://github.com/actions/runner-images.git
synced 2026-01-06 18:19:54 +08:00
Adjust services handling in Finalize-VM and helpers (#8771)
This commit is contained in:
committed by
GitHub
parent
286fb24a63
commit
495f740eda
@@ -92,8 +92,7 @@ function Install-Binary
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
function Stop-SvcWithErrHandling
|
function Stop-SvcWithErrHandling {
|
||||||
{
|
|
||||||
<#
|
<#
|
||||||
.DESCRIPTION
|
.DESCRIPTION
|
||||||
Function for stopping the Windows Service with error handling
|
Function for stopping the Windows Service with error handling
|
||||||
@@ -104,36 +103,26 @@ function Stop-SvcWithErrHandling
|
|||||||
.PARAMETER StopOnError
|
.PARAMETER StopOnError
|
||||||
Switch for stopping the script and exit from PowerShell if one service is absent
|
Switch for stopping the script and exit from PowerShell if one service is absent
|
||||||
#>
|
#>
|
||||||
Param
|
Param (
|
||||||
(
|
|
||||||
[Parameter(Mandatory, ValueFromPipeLine = $true)]
|
[Parameter(Mandatory, ValueFromPipeLine = $true)]
|
||||||
[string] $ServiceName,
|
[string] $ServiceName,
|
||||||
[switch] $StopOnError
|
[switch] $StopOnError
|
||||||
)
|
)
|
||||||
|
|
||||||
Process
|
Process {
|
||||||
{
|
|
||||||
$service = Get-Service $ServiceName -ErrorAction SilentlyContinue
|
$service = Get-Service $ServiceName -ErrorAction SilentlyContinue
|
||||||
if (-not $service)
|
if (-not $service) {
|
||||||
{
|
|
||||||
Write-Warning "[!] Service [$ServiceName] is not found"
|
Write-Warning "[!] Service [$ServiceName] is not found"
|
||||||
if ($StopOnError)
|
if ($StopOnError) {
|
||||||
{
|
|
||||||
exit 1
|
exit 1
|
||||||
}
|
}
|
||||||
|
} else {
|
||||||
}
|
|
||||||
else
|
|
||||||
{
|
|
||||||
Write-Host "Try to stop service [$ServiceName]"
|
Write-Host "Try to stop service [$ServiceName]"
|
||||||
try
|
try {
|
||||||
{
|
|
||||||
Stop-Service -Name $ServiceName -Force
|
Stop-Service -Name $ServiceName -Force
|
||||||
$service.WaitForStatus("Stopped", "00:01:00")
|
$service.WaitForStatus("Stopped", "00:01:00")
|
||||||
Write-Host "Service [$ServiceName] has been stopped successfuly"
|
Write-Host "Service [$ServiceName] has been stopped successfuly"
|
||||||
}
|
} catch {
|
||||||
catch
|
|
||||||
{
|
|
||||||
Write-Error "[!] Failed to stop service [$ServiceName] with error:"
|
Write-Error "[!] Failed to stop service [$ServiceName] with error:"
|
||||||
$_ | Out-String | Write-Error
|
$_ | Out-String | Write-Error
|
||||||
}
|
}
|
||||||
@@ -141,8 +130,7 @@ function Stop-SvcWithErrHandling
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
function Set-SvcWithErrHandling
|
function Set-SvcWithErrHandling {
|
||||||
{
|
|
||||||
<#
|
<#
|
||||||
.DESCRIPTION
|
.DESCRIPTION
|
||||||
Function for setting the Windows Service parameter with error handling
|
Function for setting the Windows Service parameter with error handling
|
||||||
@@ -152,32 +140,33 @@ function Set-SvcWithErrHandling
|
|||||||
|
|
||||||
.PARAMETER Arguments
|
.PARAMETER Arguments
|
||||||
Hashtable for service arguments
|
Hashtable for service arguments
|
||||||
|
|
||||||
|
.PARAMETER StopOnError
|
||||||
|
Switch for stopping the script and exit from PowerShell if one service is absent
|
||||||
#>
|
#>
|
||||||
|
|
||||||
Param
|
Param (
|
||||||
(
|
|
||||||
[Parameter(Mandatory, ValueFromPipeLine = $true)]
|
[Parameter(Mandatory, ValueFromPipeLine = $true)]
|
||||||
[string] $ServiceName,
|
[string] $ServiceName,
|
||||||
[Parameter(Mandatory)]
|
[Parameter(Mandatory)]
|
||||||
[hashtable] $Arguments
|
[hashtable] $Arguments,
|
||||||
|
[switch] $StopOnError
|
||||||
)
|
)
|
||||||
|
|
||||||
Process
|
Process {
|
||||||
{
|
|
||||||
$service = Get-Service $ServiceName -ErrorAction SilentlyContinue
|
$service = Get-Service $ServiceName -ErrorAction SilentlyContinue
|
||||||
if (-not $service)
|
if (-not $service) {
|
||||||
{
|
Write-Warning "[!] Service [$ServiceName] is not found"
|
||||||
Write-Warning "[!] Service [$ServiceName] is not found"
|
if ($StopOnError) {
|
||||||
|
exit 1
|
||||||
|
}
|
||||||
|
} else {
|
||||||
|
try {
|
||||||
|
Set-Service $serviceName @Arguments
|
||||||
|
} catch {
|
||||||
|
Write-Error "[!] Failed to set service [$ServiceName] arguments with error:"
|
||||||
|
$_ | Out-String | Write-Error
|
||||||
}
|
}
|
||||||
|
|
||||||
try
|
|
||||||
{
|
|
||||||
Set-Service $serviceName @Arguments
|
|
||||||
}
|
|
||||||
catch
|
|
||||||
{
|
|
||||||
Write-Error "[!] Failed to set service [$ServiceName] arguments with error:"
|
|
||||||
$_ | Out-String | Write-Error
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -104,10 +104,8 @@ $servicesToDisable = @(
|
|||||||
'StorSvc'
|
'StorSvc'
|
||||||
)
|
)
|
||||||
|
|
||||||
$servicesToDisable | ForEach-Object {
|
$servicesToDisable | Stop-SvcWithErrHandling
|
||||||
Stop-Service -Name $_
|
$servicesToDisable | Set-SvcWithErrHandling -Arguments @{StartupType = "Disabled"}
|
||||||
Set-Service -Name $_ -StartupType Disabled -ErrorAction Ignore
|
|
||||||
} | Out-Null
|
|
||||||
|
|
||||||
# Disable scheduled tasks
|
# Disable scheduled tasks
|
||||||
$allTasksInTaskPath = @(
|
$allTasksInTaskPath = @(
|
||||||
|
|||||||
Reference in New Issue
Block a user