Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/visual-studio/8.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 如何在Windows上的旧MSBuild安装文件夹上安装MSBuild?_C#_Visual Studio_Powershell_Msbuild - Fatal编程技术网

C# 如何在Windows上的旧MSBuild安装文件夹上安装MSBuild?

C# 如何在Windows上的旧MSBuild安装文件夹上安装MSBuild?,c#,visual-studio,powershell,msbuild,C#,Visual Studio,Powershell,Msbuild,我正在写一个powershell脚本来自动在TeamCity上构建.NET项目。第一步是安装msbuild。以下是我到目前为止得到的脚本: #下载msbuild $Url='1https://aka.ms/vs/15/release/vs_buildtools.exe' $msBuildInstaller=([System.Environment]::ExpandEnvironmentVariables(“%TEMP%\vs_BuildTools.exe”)) 调用WebRequest-输出文件

我正在写一个powershell脚本来自动在TeamCity上构建.NET项目。第一步是安装msbuild。以下是我到目前为止得到的脚本:

#下载msbuild
$Url='1https://aka.ms/vs/15/release/vs_buildtools.exe'
$msBuildInstaller=([System.Environment]::ExpandEnvironmentVariables(“%TEMP%\vs_BuildTools.exe”))
调用WebRequest-输出文件“$msBuildInstaller”$Url
#安装msbuild
$installDir=“$Env:UserProfile\msbuild”
$installDirCache=“$Env:UserProfile\msbuild\cache”
$installDirShared=“$Env:UserProfile\msbuild\shared”
启动进程-文件路径“$msBuildInstaller”-动词RunAs-Wait-PassThru-ArgumentList"--添加Microsoft.VisualStudio.Workload.MSBuildTools--添加Microsoft.VisualStudio.Workload.NetCoreBuildTools--添加Microsoft.VisualStudio.Workload.ManagedDesktopBuildTools--添加Microsoft.VisualStudio.Workload.WebBuildTools--添加Microsoft.Net.Component.4.7.1.TargetingPack--norestart--quiet--force--pathinstall=$installDir--path cache=$installDirCache--path shared=$installDirShared”
使用此脚本,我可以在
$installDir
处安装msbuild。但是,当我在Windows资源管理器中删除
$installDir
并再次运行该脚本时,它不起作用


如何使其工作?

对于
VS2017
VS2019
,它实际上调用
vsinstaller(VS installer.exe)
来安装指定的组件和工作负载,VS2017的
构建工具(VS_buildtools.exe)也是如此

当您通过
vs_buildtools.exe
安装生成工具时,它将调用
vs installer.exe
为您安装这些软件包,在此过程中,它不仅下载软件包和文件,还将注册一些注册表项,并将有关生成工具软件包的信息存储在相关的xx.config文件中

但是当我在窗口资源管理器中删除$installDir并运行 再说一遍,它不起作用

因此,您执行的delete操作实际上删除了构建工具的部分(并非全部),当您第二次运行脚本时,安装程序将通过读取注册表或xx.config文件中的信息,发现您要安装的VS构建工具的版本之前已经安装

在这种情况下,VS installer无法安装构建工具,这就是脚本无法工作的原因

我怎样才能让它工作

您最初的目的是
自动生成.NET项目
,因此您只需确定是否安装了生成工具包,如果未安装,请安装它。(您可以组合和来生成此逻辑。)


您不需要手动删除生成工具包来执行测试。如果您想卸载它,只需选择。手动删除文件不是推荐的方法,在大多数情况下,这是不需要的。因此,脚本不工作是预期行为。

如果您有特殊原因手动删除文件夹,请告诉我要测试,这是删除,不是卸载:)谢谢。我使用卸载开关,一切都很好。