I am working in a script that is generating an error that I have not been able to find a solution. I am using PowerShell version 7.4.1 and 5.1. Both versions produce the same error.
Import-Module SqlServer
# Succeeds with no error.
$Test = [Microsoft.SqlServer.Management.Smo.Database]::New()
$TypeDefinition = @'
using System.Runtime;
namespace MyNamespace
{
public class MyDatabase
{
public Microsoft.SqlServer.Management.Smo.Database Database;
public MyDatabase(Microsoft.SqlServer.Management.Smo.Database SmoDatabase)
{
this.Database = SmoDatabase;
}
}
}
'@
$ReferencedAssemblies = @(
[AppDomain]::CurrentDomain.GetAssemblies().where({$_.ManifestModule.Name -eq 'Microsoft.SqlServer.Smo.dll'}).Location
)
$TypeParameters = @{
TypeDefinition = $TypeDefinition
ReferencedAssemblies = $ReferencedAssemblies
}
# Generates Error
Add-Type @TypeParameters
This code results in error:
error CS1701: Assuming assembly reference 'System.Runtime, Version=6.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a' used by 'Microsoft.SqlServer.Smo' matches identity 'System.Runtime, Version=8.0.0.0, Culture=neutral, ublicKeyToken=b03f5f7f11d50a3a' of 'System.Runtime', you may need to supply runtime policy
How do I correct this error?
Microsoft.SqlServer.Smo.dllare you using?