Powershell: Start all services with startuptype: Automatic on remote server

param ($servername)
$ignorelist = "MapsBroker","RemoteRegistry","sppsvc","WbioSrvc","BITS","ShellHWDetection","TrustedInstaller"

if ($servername.length -lt 2) { $Servername = Read-Host -Prompt 'Input the servername' }

# Check for name is IP
$checkIP = [bool]($servername -as [ipaddress])


# Check DNS resolve
if (!($checkIP)) { 
    $DNScheck = Resolve-DnsName $servername -ErrorAction SilentlyContinue

    if ($DNScheck.length -lt 1) {
        Write-host "Unable to resolve DNS name" -ForegroundColor Red
        Exit 1}
    }

Invoke-Command -ComputerName $servername -ArgumentList $ignorelist -ScriptBlock { $NotRunningServices = Get-Service | Where-Object { $_.Starttype -like "Automatic*" -and $_.status -notlike "Running" }


foreach ($NotRunningService in $NotRunningServices) {

    if (!($args.Contains($NotRunningService.name))) {
    Start-service $NotRunningService.name -ErrorAction SilentlyContinue
    sleep -Milliseconds 500
    if ((Get-service -Name $NotRunningService.name).status.value__ -ne 4 ) { Write-host "Failed to start" $NotRunningService.name -ForegroundColor Red }
    else { Write-host "Succeded starting" $NotRunningService.name -ForegroundColor Green }

    }

}  }

Leave a Reply

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

This site uses Akismet to reduce spam. Learn how your comment data is processed.