UnauthorizedAccess on post build of Visual Studio

Viewed 4462

I am trying to work on a Windows laptop and setup a post_build.ps1 after the regular build of Visual Studio in VB.NET

When I run the script on powershell command, it doesn't have any error. But when I do it through Build in Visual Studio, I have this:

powershell ./_POST_BUILD.ps1 exited with code 1

If I enable the diagnostic output, I can see through the output this:

1>  AuthorizationManager check failed. (TaskId:39)
1>      + CategoryInfo          : SecurityError: (:) [], ParentContainsErrorRecordException (TaskId:39)
1>      + FullyQualifiedErrorId : UnauthorizedAccess (TaskId:39)

What I tried:

  1. Set the policies to unrestricted with Set-ExecutionPolicy Unrestricted -scope LocalMachine and Set-ExecutionPolicy Unrestricted -scope CurrentUser
  2. Change the post build event in Visual Studio to Powershell.exe -ExecutionPolicy Unrestricted -file "$(SolutionDir)bin\debug\_POST_BUILD.ps1"
  3. Unblock file with right click -> properties -> unblock
  4. Run Visual Studio as administrator always
3 Answers

Copy on Visual Studio Powershell, run and after close VS. Reopen VS, it worked.

Set-ExecutionPolicy –ExecutionPolicy RemoteSigned
Related