How to remove the uninstall msi using MSBUILD - asp.net-mvc-2

Can anyone help me to get the custom script (i.e. using Target) sample to uninstall the msi instance which is already installed on a server using MSBUILD.
A sample code example will be more helpful.
Thanks & Regards,
Santosh Kumar Patro

The below mentioned MSBUILD script helped me to uninstall the msi:
<Target Name="UnInstallMSI">
<Message Text="UnInstallation of MSI Started..." Importance="high"></Message>
<Exec Command='msiexec.exe /x "$(MSILocation)" /qn /l*vx "$(LogFile)"' ContinueOnError="false" />
<Message Text="UnInstallation of MSI Completed Successfully..." Importance="high"></Message>
</Target>
Thanks & Regards,
Santosh Kumar Patro

Related

VS2017 delete NuGet files in post build event

What is the order of execution of project post build events related to a NuGet package copying its files? I have a NuGet package that copies unnecessary files to my output directory (there are both DLLs and EXEs, in my case I don't need the latter). The usual idea of
del /f /q "$(TargetDir)directory\*.exe"
doesn't work, very probably it's called before VS (MSBuild) actually copies those files.
very probably it's called before VS (MSBuild) actually copies those files.
According to the info in the comment:
the <Target Name="CopyNativeBinaries" AfterTargets="Build">, this is
from the package targets file.
This import MSBuild target would executed copy task after build completed, which cause delete command line in the post-build to fail. That is the reason why CopyNativeBinaries target comes four steps after PostBuildEvent.
To resolve this issue, we could convert the post-build event command line to the target with the order after target CopyNativeBinaries:
Detail Steps:
Remove the post-build event.
Unload your project. Then at the very end of the project, just before the end-tag, place below scripts:
<Target Name="DeleteFile" AfterTargets="CopyNativeBinaries">
<ItemGroup>
<FileToDelete Include="$(TargetDir)directory\*.exe"/>
</ItemGroup>
<Exec Command="del /F /Q "#(FileToDelete)""/>
</Target>
After this setting, the DeleteFile will executed after the target CopyNativeBinaries.
This works for me (MSBUILD v15)
<Target Name="BeforeClean">
<Exec Command="del /F /Q $(TargetDir)*.exe"/>
<Exec Command="del /F /Q $(TargetDir)*.example"/>
</Target>

Building Windows 8 Phone App on Command Line

I'm currently porting an existing cross platform framework to Windows Phone 8.
The build process is fully automated and we are using a rock solid CI system.
I can build and deploy Windows Phone 8 samples from Visual Studio (Express 2012),
but now I need to integrate that into our build scripts.
Did anybody ever successfully build (and deploy) a Win Phone 8 app via the commandline?
(Or ant, make, scons, whatever...)
If yes, how?
Any hints are welcome.
I used the following bat file to build WP7 code (+ant automation on top of it). It may be helpful for you.
build.bat
C:\Windows\Microsoft.NET\Framework\v4.0.30319\MSBuild.exe build.xml /t:BuildAndCopy /p:Revision=123
where build.xml is something like (build itself goes under BuildAll target)
<Project xmlns="http://schemas.microsoft.com/developer/msbuild/2003">
<Import Project="C:\Program Files (x86)\MSBuild\MSBuildCommunityTasks\MSBuild.Community.Tasks.Targets"/>
<PropertyGroup>
<Major>1</Major>
<Minor>0</Minor>
<Build>0</Build>
<Revision>x</Revision>
<OutputPath>Build\</OutputPath>
<OutputPathDebug>..\Build\Debug\</OutputPathDebug>
<OutputPathRelease>..\Build\</OutputPathRelease>
</PropertyGroup>
<Target Name="BuildAll" DependsOnTargets="Clean; Version">
<msbuild Projects="SomeApp.sln" Properties="Configuration=Release;OutputPath=$(OutputPathRelease)"/>
<msbuild Projects="SomeApp.sln" Properties="Configuration=Debug;OutputPath=$(OutputPathDebug)"/>
</Target>
<Target Name="Clean">
<RemoveDir Directories="$(OutputPath)" Condition="Exists('$(OutputPath)')"/>
<MakeDir Directories="$(OutputPath)" Condition="!Exists('$(OutputPath)')"/>
</Target>
<Target Name="Version">
<Message Text="Version: $(Major).$(Minor).$(Build).$(Revision)"/>
<XmlUpdate
Namespace=""
XmlFileName="WindowsPhone\Properties\WMAppManifest.xml"
XPath="//App[#Version]//#Version"
Value="$(Major).$(Minor).$(Build).$(Revision)"/>
</Target>
<ItemGroup>
<AppFiles Include="$(OutputPath)\**\*.xap"/>
</ItemGroup>
<Target Name="BuildAndCopy" DependsOnTargets="BuildAll">
<Copy
SourceFiles="#(AppFiles)"
DestinationFiles="#(AppFiles->'\\fs\Public\projects\mobile\SomeAppWP\$(Major).$(Minor).$(Build).$(Revision)\%(RecursiveDir)%(Filename)%(Extension)')"
/>
<Copy
SourceFiles="#(AppFiles)"
DestinationFiles="#(AppFiles->'\\fs\Public\projects\mobile\SomeAppWP\latest\%(RecursiveDir)%(Filename)%(Extension)')"
/>
</Target>
</Project>
It should be easy. I'll assume you have the following file structure
$/WP8App/SampleApp.sln
You will have to use the paths and solution file in your project. Execute the following instructions, and make sure to update solution file and path:
Open a command prompt
Execute: %VS110COMNTOOLS%vsvars32.bat
Execute: %VS110COMNTOOLS%..\..\VC\WPSDK\WP80\vcvarsphoneall.bat
Execute: MSBuild "WP8App/SampleApp.sln" /t:rebuild
/p:Configuration=Release /p:Platform="Any CPU" /v:d
Please notice:
The computer where you're going to run these commands should have the development environment for WP8 already set up.
If your application uses native libraries (such as PlayerFramework http://playerframework.codeplex.com/ which are installed into VS, these libraries should also be installed before running the commands)
I have used this process on VS2012 Pro and VS2012 Premium and Jenkins as Build Server.
Good luck,
Herb
Thanks to Sergei's MSBuild.exe hint I was able to build a simple sample via the command line.
https://github.com/AndreasOM/wp8-directx-commandline
There is a build.bat included, which should be enough to get you started to build for "windows phone 8" with the build system of your choice.
It's fairly hacky at the moment,
but once I get the current project done
I will clean it up.
Hint:
Never define a "CL" environment variable when working with "CL.exe" ;)

Setting PATH environment variable on ant build.xml works on cygwin but not on cmd or PowerShell

I was trying to set PATH enviroment variable on an ant build.xml through this answer.
It works on cygwin but not on cmd or PowerShell.
Some information:
Apache Ant 1.6.5 (I know there is a newer version (1.8.4), but for internal reasons I have to use this older version)
Powershell v2.0
cmd v6.1.7601
cygwin 2.774
Windows 7
You may need to use the exec task a little differently on Windows/cmd environments.
Let's use the windows command set as an example. set will print environment variables. A normal exec task running a set command might look like:
<exec executable="set" outputproperty="set.output">
<env key="MY_VAR" value="MY_VAL"/>
<echo message="${set.output}"/>
</exec>
But using this form of the exec task should throw an IOException: The system cannont find the file specified.
When running ant under windows cmd shell, the exec task can also be invoked via cmd, like this:
<exec executable="cmd" outputproperty="set.output">
<arg line="/c set"/>
<env key="MY_VAR" value="MY_VAL"/>
<echo message="${set.output}"/>
</exec>
This is the equivalent command; the actual command executed is cmd /c set, which is running set in a cmd sub-process.
The reason why this is necessary is only a little complicated and is due to the way that the commands are located by Win32 ::CreateProcess. The ant exec docs briefly explain this.
Note that I haven't tried either of these using PowerShell, so I have no experience which, if either, will work.
In my own ant build scripts I typically have two versions of each target that require special handling for windows platforms, with an isWindows test that looks like this:
<target name="check-windows">
<condition property="isWindows">
<os family="windows"/>
</condition>
</target>
Then I can switch between versions of the same task using:
<target name="my-target-notwindows" depends="check-windows" unless="isWindows>
...
</target>
<target name="my-target-windows" depends="check-windows" if="isWindows>
...
</target>
<target name="my-target" depends="my-target-notwindows,my-target-windows">
...
</target>
Unfortunatelly was an ant bug related to 1.6.5 version. I was able to update to 1.8.4 and everything works fine.

How can I add an AfterBuild event to a project when installing my NuGet package?

I have a nuget package which adds an executable that I need to run after a project builds each time.
I can manually add this by adding a section to each project file like so:
<Target Name="AfterBuild">
<PropertyGroup>
<PathToOutputExe>..\bin\Executable.exe</PathToOutputExe>
<PathToOutputJs>"$(MSBuildProjectDirectory)\Scripts\Output.js"</PathToOutputJs>
<DirectoryOfAssemblies>"$(MSBuildProjectDirectory)\bin\"</DirectoryOfAssemblies>
</PropertyGroup>
<AspNetCompiler Condition="'$(MvcBuildViews)'=='true'" VirtualPath="temp" PhysicalPath="$(ProjectDir)" />
<Exec Command="$(PathToOutputExe) $(PathToOutputJs) $(DirectoryOfAssemblies)" />
</Target>
How can I add this to a project when I install the nuget package? (i.e. using the DTE $project object in the Install.ps1 file)
I would greatly appreciate any help on this.
Thanks
Richard
As of NuGet 2.5, by convention, if you add a targets file at build\{packageid}.targets (note 'build' is at the same level as content and tools), NuGet will automatically add an Import to the .targets file in the project. Then you don't need handle anything in install.ps1. The Import will be automatically removed on uninstall.
Also, I think the recommended way to do what you want would to create a separate target that is configured to run after the standard "Build" target:
<?xml version="1.0" encoding="utf-8" ?>
<Project xmlns="http://schemas.microsoft.com/developer/msbuild/2003">
<Target Name="NuGetCustomTarget" AfterTargets="Build">
<PropertyGroup>
<PathToOutputExe>..\bin\Executable.exe</PathToOutputExe>
<PathToOutputJs>"$(MSBuildProjectDirectory)\Scripts\Output.js"</PathToOutputJs>
<DirectoryOfAssemblies>"$(MSBuildProjectDirectory)\bin\"</DirectoryOfAssemblies>
</PropertyGroup>
<AspNetCompiler Condition="'$(MvcBuildViews)'=='true'" VirtualPath="temp" PhysicalPath="$(ProjectDir)" />
<Exec Command="$(PathToOutputExe) $(PathToOutputJs) $(DirectoryOfAssemblies)" />
</Target>
</Project>
Here's a script that adds a afterbuild target. It also user the NugetPowerTools mentioned above.
$project = Get-Project
$buildProject = Get-MSBuildProject
$target = $buildProject.Xml.AddTarget("MyCustomTarget")
$target.AfterTargets = "AfterBuild"
$task = $target.AddTask("Exec")
$task.SetParameter("Command", "`"PathToYourEXe`" $(TargetFileName)")
The hard part was getting the quotes right. This is what it looks like in my powertools script.
You can use the MSBuild Api directly, as in this blog post.
Add-Type -AssemblyName 'Microsoft.Build, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a'
$msbProject = [Microsoft.Build.Evaluation.ProjectCollection]::GlobalProjectCollection.GetLoadedProjects($project.FullName) | Select-Object -First 1
# use $msbProject to add/change AfterBuild target
Or you could use NugetPowerTools which adds Powershell command Get-MSBuildProject to do much the same thing.
Also, see this forum post for more details on adding the new target.

MSBuild. Check if windows service is installed

I'm new to msbuild and currently I'm trying to create msbuild script that will deploy my C# windows service to remote test server.
I'm thinking about using sc.exe utility for this purpose. Reading about it I didn't find a way to check whether windows service is installed on a remote server. If the service is installed then I need to stop it and update necessary files, otherwise I need to register the service.
P.S. For release builds I plan to use WiX to create MSI package.
You need MSBuild Comminity Tasks.
In latest build exists an example in MSBuild.Community.Tasks.v1.2.0.306\Source\Services.proj.
It will solve first part of your question:
<PropertyGroup>
<MSBuildCommunityTasksPath>$(MSBuildProjectDirectory)\MSBuild.Community.Tasks\bin\Debug</MSBuildCommunityTasksPath>
</PropertyGroup>
<Import Project="$(MSBuildProjectDirectory)\MSBuild.Community.Tasks\MSBuild.Community.Tasks.Targets"/>
<Target Name="Test">
<CallTarget Targets="DoesServiceExist" />
<CallTarget Targets="GetServiceStatus" />
<CallTarget Targets="ServiceControllerStuff" />
</Target>
<Target Name="DoesServiceExist">
<ServiceQuery ServiceName="MSSQLServer123" MachineName="127.0.0.1" >
<Output TaskParameter="Exists" PropertyName="Exists" />
<Output TaskParameter="Status" PropertyName="ServiceStatus" />
</ServiceQuery>
<Message Text="MSSQLServer Service Exists: $(Exists) - Status: $(ServiceStatus)"/>
</Target>
<Target Name="GetServiceStatus">
<ServiceQuery ServiceName="MSSQLServer" MachineName="127.0.0.1">
<Output TaskParameter="Status" PropertyName="ResultStatus" />
</ServiceQuery>
<Message Text="MSSQLServer Service Status: $(ResultStatus)"/>
</Target>
<Target Name="ServiceControllerStuff">
<ServiceController ServiceName="aspnet_state" MachineName="127.0.0.1" Action="Start" />
<ServiceController ServiceName="aspnet_state" MachineName="127.0.0.1" Action="Stop" />
</Target>
Those MSBuild task is just a wrapper around .Net class ServiceController. Take a look for documentation to understand how it works and how you can configure it in details.
Second part includes installing service. For that purpose sc.exe suits very well.
A complete solution is posted here. May help future visitors.
Update: Link updated as the other blogging service went down.