Azure Windows VM启动脚本安装vs_buildtools失败,RDP执行正常
Azure Windows VM启动脚本安装VS BuildTools失败解决方法
问题场景
Azure上的Windows虚拟机启动时自动运行以下PowerShell脚本:
Start-Process cmd.exe -ArgumentList "/c whoami.exe 1> c:\whoami.log 2>&1" -wait $tempPath = "$env:TEMP" $exePath = "$tempPath\vs_buildtools.exe" Invoke-WebRequest -Uri https://aka.ms/vs/17/release/vs_BuildTools.exe -UseBasicParsing -OutFile $exePath Start-Process cmd.exe -ArgumentList "/c $exePath --quiet --add Microsoft.VisualStudio.Component.TestTools.BuildTools 1> c:\vs_buildtools.log 2>&1" -Wait
出现以下异常:
- whoami.exe正常执行,
c:\whoami.log写入nt authority\system - vs_buildtools.exe已下载到
C:\Windows\Temp目录 - BuildTools未执行任何安装操作,临时文件夹无
dd_*日志文件 c:\vs_buildtools.log报错:
The application cannot find one of its required files, possibly because it was unable to create it in the folder. Please make sure that the folder in which this application was downloaded is accessible and not read-only.
注:通过RDP手动运行脚本一切正常,添加-Verb runAs参数后问题依旧。
解决步骤
1. 更换安装包存放目录
系统临时目录C:\Windows\Temp在SYSTEM账户启动上下文下可能存在访问限制,建议将安装包下载到自定义目录,比如C:\BuildToolsSetup:
修改脚本路径相关代码:
# 创建自定义安装目录 $setupDir = "C:\BuildToolsSetup" New-Item -Path $setupDir -ItemType Directory -Force # 指定安装包路径 $exePath = "$setupDir\vs_buildtools.exe"
替换原脚本中$tempPath相关逻辑,确保目录提前创建且权限充足。
2. 手动指定安装日志目录
为VS BuildTools指定自定义日志目录,避免系统临时目录的访问问题,修改启动命令参数:
Start-Process cmd.exe -ArgumentList "/c $exePath --quiet --add Microsoft.VisualStudio.Component.TestTools.BuildTools --log $setupDir\vs_install.log 1> c:\vs_buildtools.log 2>&1" -Wait
日志会输出到自定义目录,便于排查更详细的安装错误。
3. 检查系统临时目录权限
确认C:\Windows\Temp的权限设置,确保SYSTEM账户拥有完全控制权限:
- 右键
C:\Windows\Temp→ 属性 → 安全选项卡 - 检查SYSTEM账户的权限,若缺失完全控制权限,添加并应用设置
4. 使用离线安装包
提前生成VS BuildTools离线安装包,避免启动时下载带来的文件损坏或权限问题:
- 在本地机器运行命令生成离线包:
vs_buildtools.exe --layout C:\VSBuildToolsOffline --add Microsoft.VisualStudio.Component.TestTools.BuildTools - 将离线包上传到Azure虚拟机或Azure存储账户
- 修改启动脚本,直接调用离线包进行安装
内容的提问来源于stack exchange,提问作者Warham
相关产品推荐
相关产品推荐

