PowerCLI script to start vCenter default services

I had some problems recently in my home lab where my vCenter server didn’t start all necessary services when rebooted resulting in some backup jobs failing. See separate post about the issue.

Instead of fixing the problem, why not create a script that checks if the default services was started. Typically, the following services are not required:
vmcam
vmware-imagebuilder — unless the Imagebuilder feature has been enabled
vmware-netdumper
vmware-rbd-watchdog
vmware-vcha — unless vCenter High Availability has been enabled

For the script to work, SSH needs to be enabled on the vCenter Server
In the vCenter Server Appliance Management Interface (https://<vcenter IP>:5480) click Access, and click Edit.
Edit the access settings for the vCenter Server Appliance and enable Enable SSH login.

and the Posh-SSH module needs to be installed on the client you’re running the script from, a simple
Install-Module -Name Posh-SSH
will do it.

# Start-VCSA-services.ps1
# v1.0 Niclas Borgstrom
#
# Ensure Posh-SSH module is imported
Import-Module -Name Posh-SSH -ErrorAction Stop

# Parameters - Replace these with actual values
$VCServer = "IP Address or FQDN to vCenter"
$Username = "root" # useraccount to use to access vCenter VAMI
$Password = "<password>" # Password for the above user account

# Services to check and potentially start - Started by default
$ServicesToCheck = @("applmgmt", "lookupsvc", "lwsmd", "observability", "observability-vapi", "pschealth", "vc-ws1a-broker", "vlcm", "vmafdd", "vmcad", "vmdird", "vmware-analytics", "vmware-certificateauthority", "vmware-certificatemanagement", "vmware-cis-license", "vmware-content-library", "vmware-eam", "vmware-envoy", "vmware-envoy-hgw", "vmware-envoy-sidecar", "vmware-infraprofile", "vmware-perfcharts", "vmware-pod", "vmware-postgres-archiver", "vmware-rhttpproxy", "vmware-sca", "vmware-sps", "vmware-stsd", "vmware-topologysvc", "vmware-trustmanagement", "vmware-updatemgr", "vmware-vapi-endpoint", "vmware-vdtc", "vmware-vmon", "vmware-vpostgres", "vmware-vpxd", "vmware-vpxd-svcs", "vmware-vsan-health", "vmware-vsm", "vsphere-ui", "vstats", "vtsdb", "wcp", "vmware-hvc")


try {
    # Establish an SSH session to the vCenter server
    $SSHSession = New-SSHSession -ComputerName $VCServer -Credential (New-Object -TypeName PSCredential -ArgumentList $Username, (ConvertTo-SecureString $Password -AsPlainText -Force))

    # Check the status of all services
    Write-Output "Checking service statuses on $VCServer..."
    $ServiceStatusCommand = "com.vmware.service-control --status"
    $ServiceStatusResult = Invoke-SSHCommand -SessionId $SSHSession.SessionId -Command $ServiceStatusCommand

    # Output the raw service status for debugging
    Write-Output "Raw Service Status Output:"
    Write-Output ($ServiceStatusResult.Output -join "`n")

    # Parse the service status output
    $ServiceStatusOutput = $ServiceStatusResult.Output -join "`n" # Combine output lines into a single string

    # Extract the services under the "Stopped:" section
    Write-Output "Parsing the 'Stopped:' section..."
    $StoppedServices = @() # Initialize an empty array for stopped services
    if ($ServiceStatusOutput -match "Stopped:\s*(.*)") {
        # Match everything after "Stopped:"
        $StoppedServicesRaw = $Matches[1] -replace "\r|\n", " " # Replace newlines with spaces
        $StoppedServices = $StoppedServicesRaw -split '\s+' | Where-Object { $_ -ne "" } # Split services by whitespace
        Write-Output "Services found in 'Stopped:' section: $($StoppedServices -join ', ')"
    } else {
        Write-Output "No services found in the 'Stopped:' section."
    }

    # Check if the required services are in a stopped state and start them if necessary
    foreach ($Service in $ServicesToCheck) {
        if ($StoppedServices -contains $Service) {
            Write-Output "Service '$Service' is stopped. Starting it now..."
            $StartServiceCommand = "com.vmware.service-control --start $Service"
            $StartServiceResult = Invoke-SSHCommand -SessionId $SSHSession.SessionId -Command $StartServiceCommand

            # Output the result of the start service command
            Write-Output "Start Service Output for '$Service':"
            Write-Output ($StartServiceResult.Output -join "`n")
        } else {
            Write-Output "Service '$Service' is already running."
        }
    }
}
catch {
    Write-Error "An error occurred: $_"
}
finally {
    # Clean up and remove the SSH session
    if ($SSHSession) {
        Remove-SSHSession -SessionId $SSHSession.SessionId
        Write-Output "SSH session to $VCServer closed."
    }
}

Leave a Reply