Posted On January 25, 2022

PowerShell: Set Service Startup Mode

kimconnect 0 comments
blog.KimConnect.com >> Codes >> PowerShell: Set Service Startup Mode
# setServiceStartupMode.ps1
# Set auto start and restart upon failures

$computernames=@(
    'TESTWINDOWS001',
    'TESTWINDOWS002'
)

$serviceName='windows_exporter'
$startupType='Automatic'
$resets=30
$restartWaitMs=100000

function setServiceStartup{
    param(
        $serviceName='windows_exporter',
        $startupType='Automatic',
        $resets=30,
        $restartWaitMs=100000
    )
    $null=& sc.exe failure $serviceName reset= $resets actions= restart/$restartWaitMs/restart/$restartWaitMs/""/$($restartWaitMs*3)
    Set-Service -Name $serviceName -StartupType $startupType
    write-host "$env:computername now has $servicename set to automatically run and reset at $resets and restart wait-time of $restartWaitMs ms."
    if((get-service $serviceName).Status -eq 'Running'){
        write-host "$serviceName status is Running"
    }else{
        write-warning "$serviceName status is NOT Running"
    }
}

foreach ($computername in $computernames){
    invoke-command -computername $computername {
        param($setServiceStartup,$serviceName,$startupType,$resets,$restartWaitMs)
        [scriptblock]::create($setServiceStartup).invoke($serviceName,$startupType,$resets,$restartWaitMs)
    } -Args ${function:setServiceStartup},$serviceName,$startupType,$resets,$restartWaitMs
}

Set Failure Restarts

# Currently, Legacy commands must be used as PowerShell doesn't yet have such function
$servicename='TrustedInstaller'
& sc.exe failure $serviceName reset= 30 actions= restart/300000/restart/300000/""/300000

Leave a Reply

Your email address will not be published. Required fields are marked *

Related Post

Resolve Error: (59) An unexpected network error occurred

Prerequisites:   Ensure the the target machine has PowerShell Remoting Enabled (as it's Disabled by…

PowerShell: Windows Automated Disk Cleanup

##################################################################################     <#      This script is created to automate the cleanup activity. Doing so will benefit to reduce the size of disk.     This script will perform the following   1. Clear windows temp and user temp folder   2. Empty recycle bin   3. Disk Cleanup   4. Clear CBS cabinet log files   5. Clear downloaded patches   6. Clear downloaded driver   7. Clean download folder      Note:  …

JavaScript: Build a Tic Tac Toe Game (without AI)

Demo: https://blog.kimconnect.com/wp-content/projects/ticTacToeGame.html CSS Code: @import url('https://fonts.googleapis.com/css?family=Merienda');body{ font-family: 'Merienda', cursive; font-weight: bold;}#gameBoard { width: 396px; //…