Item: Delete-CI-Environment (Module) #2
Modified: 9/4/2020 12:29 PM by gdivis@inedo.local
##AH:UseTextMode
module Delete-CI-Environment<$DatabaseName, $RootDiskPath>
{
    try
    {
        Windows::Stop-Service $DatabaseName
        (
            FailIfServiceDoesNotExist: false
        );
    }
    catch
    {
        Log-Warning Failed to stop the $DatabaseName service.;
    }

    try
    {
        Windows::Stop-Service ${DatabaseName}_Web
        (
            FailIfServiceDoesNotExist: false
        );
    }
    catch
    {
        Log-Warning Failed to stop the ${DatabaseName}_Web service.;
    }

    try
    {
        Windows::Ensure-Service
        (
            Name: $DatabaseName,
            Exists: false
        );
    }
    catch
    {
        Log-Warning Failed to delete the $DatabaseName service.;
    }

    try
    {
        Windows::Ensure-Service
        (
            Name: ${DatabaseName}_Web,
            Exists: false
        );
    }
    catch
    {
        Log-Warning Failed to delete the ${DatabaseName}_Web service.;
    }

    try
    {
        with retry = 2
        {
            try
            {
                Ensure-Directory $RootDiskPath
                (
                    Exists: false
                );
            }
            catch
            {
                Sleep 5;

                throw;
            }
        }
    }
    catch
    {
        Log-Warning Failed to delete the $RootDiskPath directory.;
    }

    try
    {
        Execute-DatabaseStatement
        (
            Connection: CI,
            Statement: DROP DATABASE IF EXISTS $DatabaseName
        );
    }
    catch
    {
        Log-Warning Failed to drop the $DatabaseName database.;
    }
}