Unable to modify Visual Studio Configuration in command-line - visual-studio

I am trying to developp a script to automate the installation of DFIR-ORC and Visual Studio. I already installed Visual Studio by command-line using the following:
.\vs_Community(1).exe --addProductLang En-us --add Microsoft.VisualStudio.Workload.NativeDesktop --includeRecommended --wait --quiet
Now I want to modify the configuration with .vsconfig from the dfir-orc repo. So I use the following commmand:
.\vs_Community(1).exe modify --installPath "C:\Program Files (x86)\Microsoft Visual Studio\2019\Community\" --config ".\vsconfig"
But I have an error saying: The 'env' option in unknown. If it can help, you'll find the content of the .vsconfig file here: https://github.com/DFIR-ORC/dfir-orc/blob/main/.vsconfig
Do you have any idea ?
Thanks !

Related

Visual Studio Pre-Build Variables (TextTransform.exe)

I need to use TextTransform and I have currently this pre-build Event, which it's working:
"%ProgramFiles(x86)%\Microsoft Visual Studio\2019\Enterprise\Common7\IDE\TextTransform.exe" -a !!BuildConfiguration!$(Configuration) "$(ProjectDir)Properties\AssemblyInfo.tt"
But I wanted to replace the \2019\Enterprise\ with variables according to the user version/edition, tried to google and search here but didn't found anything.
Found a solution, in this link have some really useful varaibles:
Link to all Visual Studio $ variables
Then I'm using:
MSBuildStartupDirectory
Which is:
C:\Program Files (x86)\Microsoft Visual Studio\2019\Enterprise\Common7\IDE
Result:
"$(MSBuildStartupDirectory)\TextTransform.exe" -a !!BuildConfiguration!$(Configuration) "$(ProjectDir)Properties\AssemblyInfo.tt"

How to install the VS2019 CodeAnalyis tools using vs_BuildTools.exe

We're upgrading our build agents from VS2017 to VS2019. We use CodeAnalysis in our CI pipeline, so we install the CodeAnalysis workload using:
C:\TEMP\vs_buildtools.exe --quiet --wait --norestart --nocache `
--add Microsoft.VisualStudio.Component.Static.Analysis.Tools
This creates a directory called C:\Program Files (x86)\Microsoft Visual Studio\2017\BuildTools\MSBuild\Microsoft\VisualStudio\v15.0\CodeAnalysis
When I try this with VS2019, I don't get such a directory. I've checked other possible locations by searching for Microsoft.CodeAnalysis.Targets as well.
The docs seem to say it's in Microsoft.VisualStudio.Workload.VisualStudioExtensionBuildTools, but that doesn't work either. I've also tried Microsoft.VisualStudio.ComponentGroup.ArchitectureTools.Managed and Microsoft.Component.CodeAnalysis.SDK but no luck. Anyone know how to install this from the command line?
This worked for me
--add Microsoft.VisualStudio.Component.Roslyn.LanguageServices

The “GetReferenceNearestTargetFrameworkTask” task was not found

Given: My local system references MSBuild at "C:\Program Files (x86)\Microsoft Visual Studio\2017\Professional\MSBuild\15.0\Bin\MSbuild.exe". My automated build server (Jenkins) does not have this version of MSBuild so I used chocolatey to download microsoft-build-tools.
Problem: My visual studio project compiles successfully locally but won't compile successfully on my automated build server (Jenkins) due to a ReferenceAssembly error:
C:\Program Files (x86)\Microsoft Visual Studio\2017\BuildTools\MSBuild\15.0\Bin\Microsoft.Common.CurrentVersion.targets(1179,5): error MSB3644: The reference assemblies for framework ".NETFramework,Version=v4.6.2" were not found. To resolve this, install the SDK or Targeting Pack for this framework version or retarget your application to a version of the framework for which you have the SDK or Targeting Pack installed. Note that assemblies will be resolved from the Global Assembly Cache (GAC) and will be used in place of reference assemblies. Therefore your assembly may not be correctly targeted for the framework you intend.
I've searched online and found a possible solution https://developercommunity.visualstudio.com/content/problem/137779/the-getreferencenearesttargetframeworktask-task-wa.html where the powershell script modifies Microsoft Build Tools installer:
Start-Process "C:\Program Files (x86)\Microsoft Visual Studio\Installer\vs_installer.exe" -ArgumentList 'modify --installPath "C:\Program Files (x86)\Microsoft Visual Studio\2017\BuildTools" --quiet --add Microsoft.VisualStudio.Component.NuGet.BuildTools --add Microsoft.Net.Component.4.6.2.TargetingPack --norestart --force' -Wait -PassThru
I ran the script and I could see that it modifies the installer to select the specified target 4.6.2. However, the target package 4.6.2 is not present in the path "C:\Program Files (x86)\Reference Assemblies\Microsoft\Framework.NETFramework". I think it successfully downloaded the package one time but I deleted it to see if it was reproducible in my build server (Jenkins).
I don't want to manually paste the v4.6.2 target package from my local system to the build server. Any ideas on how to resolve this reference assembly problem?
For "GetReferenceNearestTargetFrameworkTask":
You can use powershell to call installer.exe, and install the components you lack.
Also, you can start vs installer in your server and modify your build tools to install packages below:
Any ideas on how to resolve this reference assembly problem?
If they are unchecked, check them and click modify button to install them by vs installer.exe.
And if they are checked, uncheck them and click modify to remove completely. After that, check them to reinstall the 4.6.2 framework.
Update:
After my check the modify in the Get Tools and Features can't help resolve assembly while they should.This option can only work to install new framework(which not once installed) but not repair the 4.6.2 once installed but now broken.
Download this developer pack manually and run it can help resolve the missing packages.
In addition:I delete the v4.6.2 folder in C:\Program Files (x86)\Reference Assemblies\Microsoft\Framework\.NETFramework and get it back by run the pack to repair.

The "GetReferenceNearestTargetFrameworkTask" task was not found

I have a problem with autocompletion in VS 2017 Community.
Previously I had VS 2017 Enterprise from school, but the key expired so I moved to Community.
Before, everything works great, but now it doesn't work at all.
I found a solution on Stack Overflow here but it doesn't work, so found another solution at GitHub here.
And I got this:
Build FAILED.
C:\Program Files (x86)\Microsoft Visual Studio\2017\Community\MSBuild\15.0\Bin\Microsoft.Common.CurrentVersion.targets(1601,5): error MSB4036:
The "GetReferenceNearestTargetFrameworkTask" task was not found. Check the following:
1.) The name of the task in the project file is the same as the name of the task class.
2.) The task class is "public" and implements the Microsoft.Build.Framework.ITask interface.
3.) The task is correctly declared with in the project file, or in the *.tasks files located in the
"C:\Program Files (x86)\Microsoft Visual Studio\2017\Community\MSBuild\15.0\Bin" directory.
0 Warning(s)
1 Error(s)
Time Elapsed 00:00:00.11
Do you know what to do next?
I had the same error message but for a different problem.
So, I'm using MSBuild to automate build/deploy process for Azure Functions. Everything was working fine until I updated both Visual Studio 2017 and Visual Studio Build Tools 2017 at which point I started getting this error. To be precise I got this error only when using MSBuild, building project manually from VS was working fine.
In my case I already had everything set as per #Programmer's answer.
But as I was using MSBuild it turned out that I also had to install NuGet targets and build tasks which are part of Visual Studio Build Tools.
Follow these to fix that error. This applies to VS 2017:
Fix 1:
1.Install Nuget PackageManager from here.
2.Restart Visual Studio.
If the problem is still there, continue below
Fix 2:
1.Download and start/run the Visual Studio Installer again.
2.While the Visual Studio Installer is still running, go to the "Individual Components" tab
3.Tick the "NuGet package manager" check-box that is under "Code tools" option.
4.Click Install to install it.
Screenshot of where this is located:
That should fix the error you see in this question. Restart Visual Studio and test the auto-completion function. If it's not working, see the answers from this question as that is a whole different issue.
I was seeing this issue with msbuild 15.6.82 on a build environment that does not have VisualStudio 2017, only VS Build Tools.
Here's a PowerShell script that resolves this issue, it pretty much does the equivalent of previous answers in the VS Installer, but silently and waiting for completion.
Start-Process "C:\Program Files (x86)\Microsoft Visual Studio\Installer\vs_installer.exe" -ArgumentList 'modify --installPath "C:\Program Files (x86)\Microsoft Visual Studio\2017\BuildTools" --quiet --add Microsoft.VisualStudio.Component.NuGet.BuildTools --add Microsoft.Net.Component.4.5.TargetingPack --norestart --force' -Wait -PassThru
I'm working with a headless build server on server core, resolved by installing chocolatey package choco install visualstudio2017-workload-webbuildtools from here:
https://chocolatey.org/packages?q=msbuild
I'm reporting an answer from a Microsoft techician
This usually indicates one of two things:
A failed VS installation. If that's the case, I would recommend running a repair on your VS install.
You have msbuild assemblies in the GAC. If that's the case, please ungac them.
Livar Cunha [MSFT]
I got the same problem in Visual Studio 2019, I solved it by simply install unity package

MS-Build 2017 "Microsoft.WebApplication.targets " is missing

I'm trying to upgrade our buildserver (jenkins) from Visual Studio 2015 to 2017. We're building via MS-Build. I've downloaded and installed MS-Buld tools as described in this answer. If I compile my projects I get an error that Microsoft.WebApplication.targets was not found.
Detailed error:
error MSB4226: The imported project "C:\Program Files (x86)\Microsoft Visual Studio\2017\BuildTools\MSBuild\Microsoft\VisualStudio\v15.0\WebApplications\Microsoft.WebApplication.targets"
I performed a find in the MS-Build directory:
PS C:\Program Files (x86)\MSBuild> dir -Recurse -Filter "Microsoft.WebApplication.targets"
Directory: C:\Program Files (x86)\MSBuild\Microsoft\VisualStudio\v11.0\WebApplications
Mode LastWriteTime Length Name
---- ------------- ------ ----
-a---- 11.01.2012 00:23 19654 Microsoft.WebApplication.targets
Directory: C:\Program Files (x86)\MSBuild\Microsoft\VisualStudio\v12.0\WebApplications
Mode LastWriteTime Length Name
---- ------------- ------ ----
-a---- 22.07.2013 01:25 19995 Microsoft.WebApplication.targets
Directory: C:\Program Files (x86)\MSBuild\Microsoft\VisualStudio\v14.0\WebApplications
Mode LastWriteTime Length Name
---- ------------- ------ ----
-a---- 06.07.2015 21:55 20118 Microsoft.WebApplication.targets
Directory: C:\Program Files (x86)\MSBuild\Microsoft\VisualStudio\WebApplications
Mode LastWriteTime Length Name
---- ------------- ------ ----
-a---- 22.07.2013 01:25 19995 Microsoft.WebApplication.targets
As can be seen the file exists for old compiler versions, but for v15.0 it is missing.
I also have installed Visual Studio 2017 on the build-server. The project succeeds to build if I compile with Visual Studio 2017.
Any hints how to solve this problem? One possible solution is to generate a symbolic link from C:\Program Files (x86)\MSBuild\Microsoft\VisualStudio\v15.0\WebApplications point to the old v14.0 files (located under C:\Program Files (x86)\MSBuild\Microsoft\VisualStudio\v14.0\WebApplications).
Thx
Looks like you are missing the workload for "Web development build tools": Microsoft.VisualStudio.Workload.WebBuildTools.
You can install it by downloading the build tools installer from here (VS2017) or here (VS2019) then running
vs_buildtools.exe --add Microsoft.VisualStudio.Workload.WebBuildTools
or opening vs_buildtools.exe and selecting the "Web development build tools" component in the GUI:
The accepted answer above is technically correct just incomplete, a bit confusing, at least for me, and needed one extra step to get Visual Build Professional working.
vs_buildtools.exe does not technically exist. I had to create a question asking where that file was. It was not obvious that vs_buildtools.exe is vs_buildtools_*******.exe and is the actual installation program. Additionally the the "-add ***" mentioned above is just a shorthand. One can go to the GUI and check both workflow tasks. That does the same thing. I wound up having the vs_buildtools file, though not needing it.
VSB Pro still did not build my project. I got thrown the same error above. The secret sauce was simply to copy the WebApplicationsfolder in C:\Program Files (x86)\Microsoft Visual Studio\2017\BuildTools\MSBuild\Microsoft\VisualStudio\v15.0 to C:\Program Files (x86)\MSBuild\Microsoft\VisualStudio\v15.0. I then rebuilt the project and all was fine. I placed the WebApplication.targets file at the expected location.
Why Microsoft could not have done that is beyond me. Also, why the separate installation. VS17 installs an enormous amount of stuff. Build Tools is implied, like always. Whatever.
I am posting this answer as hopefully it will help others. I fell into the trap and lost quite a bit of time with my project not building. I hope that this answer clarifies things a bit.
I had a similar issue after having upgraded from Visual Studio 2015 to 2017. When I try to load the web application project, it throwed me the error message:
The imported project "C:\Program Files (x86)\Microsoft Visual Studio\2017\Enterprise\MSBuild\Microsoft\VisualStudio\v14.0\WebApplications\Microsoft.WebApplication.targets" was not found. Also, tried to find "WebApplications\Microsoft.WebApplication.targets" in the fallback search path(s) for $(VSToolsPath) - "C:\Program Files (x86)\MSBuild\Microsoft\VisualStudio\v15.0" . These search paths are defined in "C:\Users\xxx\AppData\Local\Microsoft\VisualStudio\15.0_558e146f\devenv.exe.config". Confirm that the path in the declaration is correct, and that the file exists on disk in one of the search paths.
The solution to this error I found here.
In my case, the .csproj file contained the following lines:
<PropertyGroup>
<VisualStudioVersion Condition="'$(VisualStudioVersion)' == ''">14.0</VisualStudioVersion>
<VSToolsPath Condition="'$(VSToolsPath)' == ''">$(MSBuildExtensionsPath32)\Microsoft\VisualStudio\v14.0</VSToolsPath>
</PropertyGroup>
After replacing the v14.0 by v$(VisualStudioVersion) in the VSToolPath tag, the project could be loaded.
I also replaced the v14.0 by v10.0 in the VisualStudioVersion tag, as the solution in the above link shows. But for me it also worked by leaving it at 14.0.
Here is how these lines should look in the end:
<PropertyGroup>
<VisualStudioVersion Condition="'$(VisualStudioVersion)' == ''">10.0</VisualStudioVersion>
<VSToolsPath Condition="'$(VSToolsPath)' == ''">$(MSBuildExtensionsPath32)\Microsoft\VisualStudio\v$(VisualStudioVersion)</VSToolsPath>
</PropertyGroup>
If you don't have these lines at all in your .csproj, then you have to add them manually right BEFORE this line:
<Import Project="$(VSToolsPath)\Web\Microsoft.Web.Publishing.targets" Condition="'$(VSToolsPath)' != ''" />
In my case (slightly different error message but same problem) it was this line:
<Import Project="$(VSToolsPath)\WebApplications\Microsoft.WebApplication.targets" Condition="'$(VSToolsPath)' != ''" />
It seems that projects created with Visual Studio versions since 2011 contain the lines with the VSToolsPath redefinition, while older files did not. Visual Studio never added them automatically when upgrading to a newer VS version, which is why you should add them if they are not there.
Source of this information: https://developercommunity.visualstudio.com/content/problem/27735/project-fails-to-load-with-error-regarding-microso.html?childToView=123664#comment-123664
(click on Show more comments to see the full discussion thread - unfortunately I cannot directly link to comments in this "more" section.)
It is easy to install the 2017 build tools, with the WebBuildTools option already included, using chocolatey. Once you have installed it, type the following in an admin command prompt:
choco install visualstudio2017buildtools -y
For VS 2019, it's similar:
choco install visualstudio2019buildtools -y
For VS 2019, I fixed this by copying:
C:\Program Files (x86)\Microsoft Visual Studio\2019\Enterprise\MSBuild\Microsoft\VisualStudio\v16.0
to
C:\Program Files (x86)\MSBuild\Microsoft\VisualStudio
Copy WebApplications folder from
C:\Program Files (x86)\MSBuild\Microsoft\VisualStudio\v14.0
to
C:\Program Files (x86)\MSBuild\Microsoft\VisualStudio\v15.0
Or in your build script add the following line before compiling
Copy-Item "C:\Program Files (x86)\MSBuild\Microsoft\VisualStudio\v14.0\WebApplications" "C:\Program Files (x86)\MSBuild\Microsoft\VisualStudio\v15.0\WebApplications" -Recurse -Force
Be sure to run it with elevated permissions
how to create VS 2017 pro, asp.net MVC application and build it with Jenkins?
use vs 2017 pro in both dev machine and build server
Check that VisualStudio is set to 15
Import Project="$(MSBuildExtensionsPath32)\Microsoft\VisualStudio\v15.0\WebApplications\Microsoft.WebApplication.targets" Condition="false"
set the msbuild tool
to C:\Program Files (x86)\Microsoft Visual Studio\2017\Professional\MSBuild\15.0\Bin\MSBuild.exe in jenkins global configuration manager
With jenkins, execute windows command batch:
retore nugets: "C:\Program Files (x86)\Jenkins\tools\nuget\NuGet.exe" restore "C:\Program Files (x86)\Jenkins\workspace\theapp_build\theapp.sln"
With jenkins, execute windows command batch:
MSBuild Build File: theapp.sln
Command Line Arguments: /nologo /t:restore /t:rebuild /p:Configuration="Debug" /p:VisualStudioVersion=15.0
There can be multiple issues that cause this error, but it is generally due to missing files (example of my error below).
Invalid project `C:\xxxxx\Web.Mvc.csproj`. The imported project "C:\Program Files\dotnet\sdk\5.0.403\Microsoft\VisualStudio\v16.0\WebApplications\Microsoft.WebApplication.targets" was not found. Confirm that the expression in the Import declaration "C:\Program Files\dotnet\sdk\5.0.403\Microsoft\VisualStudio\v16.0\WebApplications\Microsoft.WebApplication.targets" is correct, and that the file exists on disk.
For myself, I am building a .NET 4.8 solution via powershell using the dotnet command, running against VS2019. The failing projects where created in VS2010/15/17.
As such to address the missing files, I simply created a symlink between the files it was looking for and the location it expected.
mklink /J "C:\Program Files\dotnet\sdk\5.0.403\Microsoft\VisualStudio" "C:\Program Files (x86)\Microsoft Visual Studio\2019\Professional\MSBuild\Microsoft\VisualStudio"
This resolved the issue and now I can build solutions programmatically (this should also work for other instances, providing you use the correct file paths to make the symlink).
For Visual Studio 2022 use the following commands with chocolatey:
choco install visualstudio2022buildtools
choco install visualstudio2022-workload-webbuildtools
see also: https://community.chocolatey.org/packages/visualstudio2022-workload-webbuildtools
I just use Visual Studio Installer to update Visual Studio Community 2017, then the problem is solved.

Resources