0

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?

2
  • What version of Microsoft.SqlServer.Smo.dll are you using? Commented Feb 22, 2024 at 11:05
  • It is the version that is packages with Microsoft's SqlServer module version 22.2.0. The dll version is 17.100.13.0. Commented Feb 22, 2024 at 14:41

1 Answer 1

0

Apparently C# compilation warnings result in an error with the Add-Type cmdlet. The reference from Here offers the solution. Changing the $TypeParameters as below resolves the problem.

$TypeParameters = @{
    TypeDefinition = $TypeDefinition
    ReferencedAssemblies = $ReferencedAssemblies
    WarningAction = 'Ignore'
    IgnoreWarnings = $true
}
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.