Fixes what Get-sqmDbOwnerRisk finds: removes unexpected db_owner members and replaces the membership with db_datareader, db_datawriter, and a custom db_execute role that is granted EXECUTE on every user stored procedure in the database.
Execution Flow
Synopsis
Re-detects db_owner membership live per database (it does not trust a possibly stale object piped in from Get-sqmDbOwnerRisk). Databases with nothing to fix are skipped entirely - no role is created, no permission is touched. For databases with unexpected members, the -ExecuteRoleName role (default db_execute) is created once if missing and granted EXECUTE on every user stored procedure; then each offending login is dropped from db_owner and added to db_datareader, db_datawriter, and the execute role. Each login is processed in its own try/catch, so one failing login does not stop the rest.
Requires dbatools. Needs sysadmin or ALTER ANY ROLE + ALTER ANY USER on the target database(s). Full -WhatIf/-Confirm support (ConfirmImpact = 'High') - always dry-run first with -WhatIf before running for real.