SQL Server: SMO error "Cannot convert value Version140" (CompatibilityLevel)
PowerShell error when setting compatibility level 140 through SMO: cause and workarounds.
In PowerShell, setting a database to compatibility level 140 (SQL Server 2017) fails with:
SqlDatabase: Cannot convert value "Version140" to type "Microsoft.SqlServer.Management.Smo.CompatibilityLevel"
Cause
The CompatibilityLevel enumeration of the loaded SMO library does not know the Version140 value: the SMO or SqlServer module version in use is older than this compatibility level.
Solutions
- Update the
SqlServermodule (or the SMO library) to a recent version:
Update-Module SqlServer
- Or bypass SMO with T-SQL:
ALTER DATABASE [AppDb] SET COMPATIBILITY_LEVEL = 140;