PowerShell Services Startup & Start

#For Storing Results 
 $StartupResultsArray=@()

#Create List of Services
$ListofServicestoCheck=@('RemoteRegistry','WinRM','mpssvc')

#==================Loop Through Services to Set Startmode to Automatic===================
Foreach($service in $ListofServicestoCheck){

    #Get Service Info
    $WMI = Get-WMIObject -class win32_service -namespace root\cimv2  | where-object { $_.name -eq $service } 
 

    #Check if Service Already Set to Auto Start
    If($WMI.StartMode -ne "Auto"){
        

        #If Not Set to Auto, Change it
        $Return = $WMI.changestartmode("Automatic")
 

        #Check to See if Error Returned
        if($Return.returnvalue -eq 0){
            $StartupResultsArray+="$Service `t StartMode Success"             
        }Else{
           $StartupResultsArray+="$Service `t StartMode Failure"          
        }
    }Else{

        #Message if Service Already in Automatic Startup Mode
        $StartupResultsArray+="$Service `t StartMode Already Set"         
    }
}

cls
$StartupResultsArray | Tee-Object C:\temp\MyBuildLog.txt -Append
write-host `n`n
sleep 5

 

 

#==================Loop Through Services to Check Status & Start Serivces===================
$ServiceStatusArray=@()

Foreach($serviceA in $ListofServicestoCheck){

$ServiceB=Get-Service $serviceA | Select -ExpandProperty Status 

If($ServiceB.Status -ne "Running"){Start-Service $serviceA -ErrorAction SilentlyContinue }

Sleep 5

$ServiceCheck=Get-Service $serviceA | Select -ExpandProperty Status 

If($ServiceCheck -eq "Running"){$ServiceStatusArray+="$serviceA Running"}Else{$ServiceStatusArray+="$serviceA Failed to Start"}}
 

cls
$ServiceStatusArray | Tee-Object C:\temp\MyBuildLog.txt -Append
write-host `n`n
sleep 5