entity-frameworkpowershellvisual-studio-2017executionpolicypackage-manager-console

Entity Framework PowerShell script cannot be loaded by Visual Studio because its operation is blocked by software restriction policies


When I load Package Manager Console within Visual Studio 2017 v15.9.6 in a project that uses Entity Framework I receive the following error:

\packages\EntityFramework.6.2.0\tools\init.ps1 cannot be loaded because its operation is blocked by software restriction policies, such as those created by using Group Policy. At line:1 char:45 + ... rgs+=$_}; & 'C:\Bitbucket\project-path\packages\EntityFramework.6.2. ... + ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ + CategoryInfo : SecurityError: (:) [], PSSecurityException + FullyQualifiedErrorId : UnauthorizedAccess

This prevents me from running commands specific to Entity Framework like "Update-Database" or "Add-Migration".

Here are things I have tried:

  1. Reinstall Visual Studio
  2. In Group Policy Editor, for both Computer Configuration and User Configuration, I have enabled the setting "Turn on Script Execution" that is located at "Administrative Templates\Windows Components\Windows PowerShell". The setting for both configurations has the "Execution Policy" set to "Allow all scripts". screenshot of script execution setting

  3. I've tried locally in the Package Manager Console setting the "Process" scope to both "Bypass" and "Unrestricted", and then manually loading the Entity Framework init.ps1 script. I receive the same error.

  4. I've also tried modifying registry keys for PowerShell to set the ExecutionPolicy to "Unrestricted" in a few places. Those places are at:
    • HKLM\Software\Microsoft\PowerShell\1\ShellIds\Microsoft.PowerShell
    • HKLM\Software\Microsoft\PowerShell\1\ShellIds\Microsoft.SqlServer.Management.PowerShell.sqlps120
    • HKLM\Software\Microsoft\PowerShell\1\ShellIds\Microsoft.SqlServer.Management.PowerShell.sqlps140
  5. I've restarted multiple times in between doing all of the above.

When I run the command "Get-ExecutionPolicy -List", here are my results"

Scope         ExecutionPolicy
----- -----------------------
MachinePolicy       Undefined
UserPolicy       Unrestricted
Process             Undefined
CurrentUser      Unrestricted
LocalMachine     Unrestricted

After all of the above I expected to be able to just load Visual Studio as normal, launch Package Manager Console, and not receive any errors when it tries to run the Entity Framework init.ps1 script. I must be missing something or doing something incorrectly. What do I need to do to get Visual Studio to work as expected?

Some more information is that this is a computer joined to a company domain, but no other developers have this issue. There isn't a group policy set by an administrator that is trickling down preventing me from running scripts.


Solution

  • The error may be occurring because of an invalid certificate from Microsoft. Check in Control Panel => Internet Options => Content => Certificates on the Untrusted Publishers tab. Remove Microsoft Corporation from this list.

    Sounds bizarre but it worked for me.

    Tks https://github.com/NuGet/Home/issues/7158