如何使用Vagrant shell/path provisioner配置Windows虚拟机?

7
我正在尝试使用PowerShell来配置Windows Vagrant虚拟机。 我已经确认如果我以管理员身份启动PowerShell,则我的脚本可以在我的本地机器上工作。 在Vagrant配置过程中,vagrant用户无法提升权限并运行脚本。
我知道Vagrant支持PowerShell脚本,那么如何将它们用于配置? 我最初的想法是尝试让脚本启动一个新的管理员PowerShell,但我觉得必须有更好的方法。
我正在尝试使用System.Net.WebClient下载文件并使用msiexec进行安装。
Vagrantfile:
# -*- mode: ruby -*-
# vi: set ft=ruby :
VAGRANTFILE_API_VERSION = "2"
PROVISION_WIN7_SCRIPT = "provision_win7.ps1"

Vagrant.configure(VAGRANTFILE_API_VERSION) do |config|
  config.vm.define "win7", primary: false do |win7|
  config.vm.provision "shell", :path => "#{PROVISION_WIN7_SCRIPT}"
  config.vm.box      = "win7"
  config.vm.hostname = "win7"
  config.vm.communicator = "winrm"
  config.vm.network :private_network, ip: "10.10.10.10"
  config.vm.box_url = "https://atlas.hashicorp.com/designerror/boxes/windows-7/versions/1.0/providers/virtualbox.box"   
    config.vm.provider :virtualbox do |vb|
      vb.gui = true
      vb.customize ["modifyvm", :id, "--memory", 1024]
      vb.customize ["modifyvm", :id, "--cpus", 1]
      vb.customize ["modifyvm", :id, "--vram", "32"]
      vb.customize ["modifyvm", :id, "--clipboard", "bidirectional"]
      vb.customize ["setextradata", "global", "GUI/SuppressMessages", "all" ]
  end
  end
end

编辑:

我今天再次尝试,使用当时的软件版本:Vagrant 1.6.3、Virtualbox 4.3.30。当我启动vagrant box时,没有看到任何错误输出:

Bringing machine 'win7' up with 'virtualbox' provider...
==> win7: Importing base box 'designerror/windows-7'...
==> win7: Matching MAC address for NAT networking...
==> win7: Checking if box 'designerror/windows-7' is up to date...
==> win7: Setting the name of the VM: vagrant_win7_1453859492744_23074
==> win7: Clearing any previously set network interfaces...
==> win7: Preparing network interfaces based on configuration...
    win7: Adapter 1: nat
    win7: Adapter 2: hostonly
==> win7: Forwarding ports...
    win7: 3389 => 3389 (adapter 1)
    win7: 22 => 2222 (adapter 1)
    win7: 5985 => 55985 (adapter 1)
==> win7: Running 'pre-boot' VM customizations...
==> win7: Booting VM...
==> win7: Waiting for machine to boot. This may take a few minutes...
==> win7: Machine booted and ready!
==> win7: Checking for guest additions in VM...
==> win7: Setting hostname...
==> win7: Configuring and enabling network interfaces...
==> win7: Mounting shared folders...
    win7: /vagrant => /Users/dstark/Projects/itbestprac-pres/vagrant
==> win7: Running provisioner: shell...
    win7: Running: c:\tmp\vagrant-shell.ps1

在我的vagrant虚拟机中,位于c:\tmp下有一个vagrant-shell.ps1和vagrant-elevated-shell.ps1文件,如果我手动以vagrant用户身份执行由vagrant生成的提权shell脚本,则可以安装成功。因此,我认为我的脚本没问题。
如果我添加config.winrm.username和config.winrm.password,我仍然无法提升权限以运行脚本。在事件查看器中也没有任何提示。

如果您要下载文件,最好的方式是使用 Invoke-WebRequest,例如:Invoke-WebRequest -Uri "http://stackoverflow.com" -OutFile index.html。下载完成后,您可以使用 msiexec 安装它。 - Soheil Hashemi
你有收到任何特定的错误吗? - Anthony Mastrean
通常情况下,您需要将 config.winrm.usernameconfig.winrm.password 设置为具有管理员访问权限的用户。 - Eris
@Eris 我在重新访问这个问题时尝试添加了这个,但似乎对我的 Vagrant 版本 1.6.3 没有帮助 -- 我将尝试更新到最新版本。 - Dan Stark
1个回答

2
可能您需要在提供行中添加privileged标志:
config.vm.provision "shell", path: "scripts/provision.ps1", privileged: true

在您的情况下:

config.vm.provision "shell", :path => "#{PROVISION_WIN7_SCRIPT}", privileged: true

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接