使用Vagrant配置Windows 7 VM时,为什么Chocolatey无法安装?

时间:2016-01-07 22:52:51

标签: windows vagrant chocolatey

我正尝试在Windows 8.1主机上使用Packer (v.0.8.6)Vagrant (v.1.8.1)VirtualBox (v.5.0.10)Chocolatey (v.0.9.9.11)自动创建Windows 7开发环境。

我已经设法使用Packer创建基本的Windows 7 SP1框,我正在尝试使用vagrant up创建具有Vagrant的虚拟机。我想要做的第一件事就是安装Chocolatey,以便我可以轻松配置其他软件。

然而。我没有成功。我在Vagrantfile中尝试了各种咒语。以下是内容。

# -*- mode: ruby -*-
# vi: set ft=ruby :
Vagrant.configure(2) do |config|
  config.vm.box = "windows_7"
  config.vm.provider "virtualbox" do |vb|
    # Display the VirtualBox GUI when booting the machine
    vb.gui = true
  end

  # option 1
  # config.vm.provision "shell", "inline": "iex ((new-object net.webclient).DownloadString('https://chocolatey.org/install.ps1'))"

  # option 2
  # config.vm.provision "shell", "path": "./scripts/install-chocolatey.cmd"

  # option 3
  # config.vm.provision "shell", path: "./scripts/install-chocolatey.ps1"
end

# ./scripts/install-chocolatey.cmd
@powershell -NoProfile -ExecutionPolicy Bypass -File "%systemdrive%\vagrant\scripts\install-chocolatey.ps1"

# ./scripts/install-chocolatey.ps1
iex ((new-object net.webclient).DownloadString('https://chocolatey.org/install.ps1'))

我收到的任何这些选项的错误是:

==> default: Everything is Ok
==> default:
==> default: Files: 76
==> default: Size:       4893948
==> default: Compressed: 1806765
==> default: out-lineoutput : The OS handle's position is not what FileStream expected. Do n
==> default: ot use a handle simultaneously in one FileStream and in Win32 code or another F
==> default: ileStream. This may cause data loss.
==> default:     + CategoryInfo          : NotSpecified: (:) [out-lineoutput], IOException
==> default:     + FullyQualifiedErrorId : System.IO.IOException,Microsoft.PowerShell.Comma
==> default:    nds.OutLineOutputCommand
==> default:

但是,如果我从虚拟机上的Cmd或PowerShell窗口手动运行选项1,2或3,那么chocolatey安装正常。

我在网上搜索了此错误,似乎是PowerShell 2 and 3 bug。实际上,如果我在虚拟机上手动安装PowerShell 4并运行vagrant provision来重新配置它,这些选项可以正常工作。

但如果是这种情况,为什么我可以手动安装巧克力而不是通过Vagrant?我想使用巧克力来升级PowerShell,因为以自动方式更容易实现。

如何指示Vagrant在我的虚拟机上安装Chocolatey而无需先将PowerShell升级到更新的版本?

解决方法尝试1:

我尝试将VirtualBox从5.0.10降级到4.3.28以查看是否存在问题(根据建议)。但是,我无法在我的主机上运行4.3.28,因为我在尝试创建新虚拟机时收到错误,即使是开箱即用,也没有打包器或流浪汉。重新安装5.0.10解决了该特定问题。

解决方法尝试2:

我将Powershell 2变通办法脚本defined here应用于我的install-chocolatey.ps1脚本。完整的内容然后变成:

$bindingFlags = [Reflection.BindingFlags] "Instance,NonPublic,GetField"
$objectRef = $host.GetType().GetField("externalHostRef", $bindingFlags).GetValue($host)

$bindingFlags = [Reflection.BindingFlags] "Instance,NonPublic,GetProperty"
$consoleHost = $objectRef.GetType().GetProperty("Value", $bindingFlags).GetValue($objectRef, @())

[void] $consoleHost.GetType().GetProperty("IsStandardOutputRedirected", $bindingFlags).GetValue($consoleHost, @())
$bindingFlags = [Reflection.BindingFlags] "Instance,NonPublic,GetField"
$field = $consoleHost.GetType().GetField("standardOutputWriter", $bindingFlags)
$field.SetValue($consoleHost, [Console]::Out)
$field2 = $consoleHost.GetType().GetField("standardErrorWriter", $bindingFlags)
$field2.SetValue($consoleHost, [Console]::Out)

iex -Debug ((new-object net.webclient).DownloadString('https://chocolatey.org/install.ps1'))

这允许安装Chocolatey。但是,在重新启动计算机之前,我无法从Vagrant配置程序或cmd行或PowerShell窗口发出choco语句。重新启动后,这些可用,但这会导致配置失败。据我所知,我不需要重启机器才能使用Chocolatey。我只需要重新启动shell。检查PATH变量我发现没有将Chocolatey添加到PATH中,这可以解释这个问题。

1 个答案:

答案 0 :(得分:1)

因此,当存在对一个流(例如stdout)的日志,然后是另一个流(例如stderr)的日志时,您会看到PowerShell错误消息,通常涉及将输出写入文件路径。

发生错误,但遗憾的是PowerShell正在吞下它并重现其他问题 - 您可以在安装Chocolatey之前添加WATCHOS_DEPLOYMENT_TARGET,看看它是否有助于指出错误发生的位置。

如果我不得不猜测它可能与Chocolatey试图安装.NET Framework 4.0并遇到错误有关。在我的流浪者回购中,我倾向于将.Net 4安装的步骤与Chocolatey分开。该回购是vagrant-windows-puppet

相关问题