Function: vCenter Host System Service Toggle

by Simon Sparks · 23 March 2026

To use this function add it to the class library file named PluginVCHostSystemService.ts

GitHub Repository: https://github.com/SimonSparksUK/Orchestrator

Filename: PluginVCHostSystemService.ts

Public Function: ServiceStart

public ServiceStart(objVcHostSystem: VcHostSystem, strServiceName: string): boolean {

    let blnReturn: boolean = this.ServiceToggle(objVcHostSystem, strServiceName, "Start");

    return blnReturn;
}
TypeScript

Public Function: ServiceStop

public ServiceStop(objVcHostSystem: VcHostSystem, strServiceName: string): boolean {

    let blnReturn: boolean = this.ServiceToggle(objVcHostSystem, strServiceName, "Stop");

    return blnReturn;
}
TypeScript

Public Function: ServiceRestart

public ServiceRestart(objVcHostSystem: VcHostSystem, strServiceName: string): boolean {

    let blnReturn: boolean = this.ServiceToggle(objVcHostSystem, strServiceName, "Restart");

    return blnReturn;
}
TypeScript

Private Function: ServiceToggle

private ServiceToggle(objVcHostSystem: VcHostSystem, strServiceName: string, strServiceStatus: "Start" | "Stop" | "Restart"): boolean {

    try {
        let objVcHostConfigManager: VcHostConfigManager = objVcHostSystem.configManager;

        let objVcHostServiceSystem: VcHostServiceSystem = objVcHostConfigManager.serviceSystem;

        let objVcHostService: VcHostService = this.ServiceGetByName(objVcHostServiceSystem, strServiceName);

        this.objLogger.info(`Attempting to ${strServiceStatus} Service on host: '${objVcHostSystem.name}'...`);

        if (objVcHostService.running === true && strServiceStatus === "Restart") {
            objVcHostServiceSystem.restartService(strServiceName);
        } else if (objVcHostService.running === true && strServiceStatus === "Stop") {
            objVcHostServiceSystem.stopService(strServiceName);
        } else if (objVcHostService.running === false && strServiceStatus === "Start") {
            objVcHostServiceSystem.startService(strServiceName);
        }

        this.objLogger.info(`Succesfully ${strServiceStatus}ed Service on host: '${objVcHostSystem.name}'.`);

        return true;
    }
    catch (objException) { 

        this.objLogger.info(`Failed to ${strServiceStatus} Service on host: '${objVcHostSystem.name}'.`);

        return false;
    }
}
TypeScript

Discover more from Cloud Build Tools

Subscribe to get the latest posts sent to your email.

You may also like