为什么在使用Vagrant配置Windows 7虚拟机时Chocolate无法安装?

为什么在使用Vagrant配置Windows 7虚拟机时Chocolate无法安装?,windows,vagrant,chocolatey,Windows,Vagrant,Chocolatey,我正在尝试在Windows 8.1主机上使用、和自动创建Windows 7开发环境 我已经使用Packer创建了基本的Windows7SP1框,并且正在尝试使用Vagrant up创建带有Vagrant的虚拟机。我想做的第一件事是安装Chocolate,这样我就可以轻松地配置其他软件 不过。我这样做是不成功的。我在我的流浪者档案里试过各种咒语。这是内容 # -*- mode: ruby -*- # vi: set ft=ruby : Vagrant.configure(2) do |config

我正在尝试在Windows 8.1主机上使用、和自动创建Windows 7开发环境

我已经使用Packer创建了基本的Windows7SP1框,并且正在尝试使用
Vagrant up
创建带有Vagrant的虚拟机。我想做的第一件事是安装Chocolate,这样我就可以轻松地配置其他软件

不过。我这样做是不成功的。我在我的流浪者档案里试过各种咒语。这是内容

# -*- 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

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

==> 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,Chocolate安装就可以了

我在网上搜索了这个错误,它似乎是一个错误。事实上,如果我在虚拟机上手动安装PowerShell 4并运行
vagrant provision
来重新配置它,那么这些选项就会起作用

但如果是这样的话,为什么我可以手动安装巧克力,而不是通过流浪者?我想使用Chococolly升级PowerShell,因为以自动化方式升级PowerShell要容易得多

如果不先将PowerShell升级到新版本,我如何指示Vagrant在我的虚拟机上安装Chocolate


解决方法尝试1:

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


解决方案尝试2:

我将Powershell 2解决方案脚本应用于install-chocolate.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'))

这样巧克力就可以安装了。但是,在重新启动机器之前,我无法从Vagrant Provisioniers或cmd line或PowerShell windows发出
choco
语句。但是,一旦重新启动,这些设置就变得可用,这将导致设置失败。我的理解是,我不需要重新启动机器来使用巧克力。我只需要重新启动shell。检查PATH变量时,我发现Chocolate没有添加到解释问题的路径中。

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

出现了一个错误,但不幸的是,PowerShell正在吞下它并复制其他问题-您可以在调用安装Chocolate之前添加
$env:ChocolatedBug
,并查看它是否有助于指出错误发生的位置


如果我不得不猜测的话,这可能与Chocolate试图安装.NETFramework4.0时出错有关。在我的流浪报告中,我倾向于将.NET4安装的步骤与巧克力分离。该repo位于。

在安装Chocolate之前,我已在install-chocolate.ps1文件中添加了
$env:chocolatedbug='true'
,不幸的是,我在vagrant输出中没有看到任何其他错误信息。我还尝试了
$env:ChocolatelyEnvironmentDebug='true'
。我还确认在基本映像上安装了.NET 4.0和.NET 4.5。Chocolate不会尝试安装这些,因为它们已经存在,对吗?您也可以将所有输出重定向到stdout,这样您就不会得到错误,但您会看到实际的错误。我尝试在
Start Transaction/Stop Transaction
中包装该命令。我还尝试通过2>&1重定向输出。这两个选项都没有向我显示更多信息,我通过Vagrant在控制台中收到相同的错误消息。我会尝试给你一些你可以很快尝试的东西。您是否有机会验证在使用上述回购协议时发生的情况?我能够在上述回购协议中重现此行为。Lee Holmes解决方案可能应升级为Vagrant powershell Provisionier。我可以直接从guest powershell控制台安装Chocolate,没问题。
==> 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:
$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'))