AfterPublish script doesn't run when I publish a web app

蹲街弑〆低调 提交于 2019-11-28 12:08:57

I saw a comment from a MS developer stating that publishing that goes to the 'File System' does not call MSDeployPublish target. You can see that here.

https://social.msdn.microsoft.com/Forums/vstudio/en-US/60b196e9-28e1-4d9d-b464-d57e42353754/vs2012-afterpublish-for-web-application?forum=msbuild

http://sedodream.com/PermaLink,guid,b352f04a-2449-4cbb-8125-7acdef9552e1.aspx

Digging around in the *.targets files there were a few targets I could place in the AfterTargets attribute that accomplished running after the publish had copied to the temporary obj output folder. I could never find one that ran after copying to the actual folder defined in the publish profile.

Here is what I think you would need:

  <Target Name="BackUpRMAToIDrive" AfterTargets="CopyAllFilesToSingleFolderForPackage">
    <Exec Command="C:\deg\bat\backupRMA.cmd" />
  </Target>

I'm not sure what you are backing up though - the original source or the published source. At the time of this Exec, if you need the path to the 'published files', you can pass in a parameter to your script via $(WebProjectOutputDir)\$(WPPAllFilesInSingleFolder). This would be something like c:\{path to *.csproj}\obj\{build configuration}\Package\PackageTmp.

When you run the cmd file manually, the script surely runs with administrator privileges, or enough privileges to succesfully copy the files to the destination (in the I: unit in this case).

The thread that executes the script from Visual Studio probably has very restricted privileges, due to security reasons.

Your best bet, is run the script from powershell, passing the following argument to it:

-ExecutionPolicy Unrestricted

Your XML will look like this:

<Target Name="CustomPostPublishActions" AfterTargets="MSDeployPublish">
  <PropertyGroup>
    <PowerShellExe Condition=" '$(PowerShellExe)'=='' "> 
      %WINDIR%\System32\WindowsPowerShell\v1.0\powershell.exe
    </PowerShellExe>
    <ScriptLocation Condition=" '$(ScriptLocation)'=='' ">
      C:\deg\bat\backupRMA.cmd
    </ScriptLocation>
  </PropertyGroup>
  <Exec Command="$(PowerShellExe) -NonInteractive -executionpolicy Unrestricted 
                 -command &quot;&amp; invoke-command -scriptblock { 
                          &amp;&apos;$(ScriptLocation)&apos; 
                          }
                          &quot;"/>  
</Target>

You can also check this MSDN link for similar commands:

http://www.asp.net/web-forms/tutorials/deployment/advanced-enterprise-web-deployment/running-windows-powershell-scripts-from-msbuild-project-files

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!