【问题标题】:How to use a powershell script during vagrant up on host correctly?如何在主机上正确使用powershell脚本?
【发布时间】:2020-10-07 11:17:23
【问题描述】:

我正在尝试在 vagrant up 特权和提升权限期间执行 powershell 脚本。我在 Windows 机器上使用 Vagrant 2.2.10。该脚本与 Vagrantfile 位于同一目录中。我设法获得了正确的触发器,但无法使用 trigger.run 正确执行我的 powershell 脚本。

这是我的 Vagrantfile 的相关部分:

ENV['VAGRANT_EXPERIMENTAL'] = "typed_triggers"

Vagrant.configure("2") do |config|
override.trigger.before :'VagrantPlugins::HyperV::Action::StartInstance', type: :action do |trigger|
          trigger.ruby do |env, machine|
            machine.ui.info("------------------ Configure Node with Powershell Script -------------------")            
            trigger.run = {
              #privileged: "true"
              #powershell_elevated_interactive: "true"
              inline: <<-SHELL
              echo hhhhhhh > 123.log
              Write-Host "test"
              SHELL
           }            
           trigger.run = { path: './test.ps1'}
           trigger.run = { inline: "./test.ps1 -a 'test1' -b 'test2" }
           trigger.run = { privileged: "true",  powershell_elevated_interactive: "true", path: "powershell test.ps1", args: ["-a test1", "-b test2 "] }
           #trigger.run = { privileged: "true",  powershell_elevated_interactive: "true", :path => "test.ps1", :args => "testarg1, testarg2"} 
              
           machine.ui.info("------------------ Configuration of Node finished  -------------------") 
          end
        end 
    
      end 

在 vagrant.log 中我找不到任何可以提供帮助的具体错误:

 INFO trigger: Firing trigger for action VagrantPlugins::HyperV::Action::StartInstance on guest server1
 INFO interface: info: Running action triggers before VagrantPlugins::HyperV::Action::StartInstance ...
 INFO interface: info: ==> server1: Running action triggers before VagrantPlugins::HyperV::Action::StartInstance ...
==> server1: Running action triggers before VagrantPlugins::HyperV::Action::StartInstance ...
DEBUG trigger: Running trigger 69600289-3cee-4dde-a9fa-2ac9bec33409...
 INFO interface: info: Running trigger...
 INFO interface: info: ==> server1: Running trigger...
==> server1: Running trigger...
 INFO interface: info: ------------------ Configure Node with Powershell Script -------------------
 INFO interface: info: ==> server1: ------------------ Configure Node with Powershell Script -------------------
==> server1: ------------------ Configure Node with Powershell Script -------------------
DEBUG provisioner: Provisioner defined: 
 INFO interface: info: ------------------ Configuration of Node finished  -------------------
 INFO interface: info: ==> server1: ------------------ Configuration of Node finished  -------------------
==> server1: ------------------ Configuration of Node finished  -------------------

上述 trigger.runs 均无效。希望你们中的任何人都知道如何解决这个问题!

我怎样才能得到这份工作?

谢谢..

【问题讨论】:

  • 为什么要为此使用触发器而不是配置器?
  • 来宾机器上没有使用provisoner吗?我想在主机上的 vagrantfile 的同一目录中执行一个脚本。或者配置器可以在本地使用吗?

标签: powershell vagrant hyper-v


【解决方案1】:

我找到了解决方案。我使用它来克服 vagrant 的限制,并将额外的网卡添加到不同的第二个公共交换机。您可以使用它来执行任何 powershell 脚本:

ENV['VAGRANT_EXPERIMENTAL'] = "typed_triggers"

Vagrant.configure("2") do |config|  
    # Set ImageName
    config.vm.box = "REPO/YOURIMAGE"    
    config.vm.box_version  ="YOURVERSION"
 
    # Set network to Hyper-V Switch (define an external switch in hyperv and insert here)
    config.vm.network "public_network", bridge: "WAN - FIRST"
    
    # Add Additional network card and assign a different second public network
    # THIS DOES NOT WORK ON HYPER-V DUE LIMITATIONS OF VAGRANT 
    #config.vm.network "public_network", bridge: "WAN - SECOND"

    # WORKAROUND for all LIMITATIONS OF VAGRANT (execute a powershell script to handle hyper-v actions before startup of instance)
    # will be executed before the hyper-Instance will be started  
    secSwitch ='WAN - SECOND'

    config.vm.provider :hyperv do |vb, override|

    # Set vmname
    vb.vmname = "testmachine"
    # Execute a powershell script elevated and privileged (!!)
      override.trigger.before :'VagrantPlugins::HyperV::Action::StartInstance', type: :action do |trigger|
        trigger.run = { inline: "./hyperv-config-node.ps1 -VmName testmachine -SwitchName \"'#{secSwitch}'\" " }
      end
    end 
    # WORKAROUND (END)
end

powershell 脚本 hyperv-config-node.ps1 如下所示:

param (
    [parameter (Mandatory=$true)]
    [string]$VmName,
    [parameter (Mandatory=$true)]
    [string]$SwitchName
)

try {
  Write-Host "------------------ Configure Node with Powershell Script : $VmName -------------------"
  Write-Host "VM-Machine: $VmName"
  Write-Host "Add Switch: $SwitchName"
  $currentPrincipal = New-Object Security.Principal.WindowsPrincipal([Security.Principal.WindowsIdentity]::GetCurrent())
  Write-Host "IsAdmin: " $currentPrincipal.IsInRole([Security.Principal.WindowsBuiltInRole]::Administrator)
  $vm = Hyper-V\Get-VM -Name $VmName -ErrorAction "stop" 
  Hyper-V\Add-VMNetworkAdapter $vm -Switch $SwitchName  
  Write-Host "------------------ Configuration of Node finished  -------------------"
}
catch {
  Write-Host "Failed to set VM's Second Nic: $_"
}

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2022-10-14
    • 2019-12-03
    • 2014-05-23
    • 2022-01-08
    • 2016-03-21
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多