如何在主机上的 vagrant up 期间正确使用 powershell 脚本?
How to use a powershell script during vagrant up on host correctly?
我正在尝试在 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.run 中的 None 有效。希望你们中的任何人都知道如何解决这个问题!!
我怎样才能得到这份工作?
谢谢..
我找到了解决办法。我用它来克服 vagrant 的限制,将额外的网卡添加到不同的第二个 public 交换机。您可以使用它来执行任何 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: $_"
}
我正在尝试在 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.run 中的 None 有效。希望你们中的任何人都知道如何解决这个问题!!
我怎样才能得到这份工作?
谢谢..
我找到了解决办法。我用它来克服 vagrant 的限制,将额外的网卡添加到不同的第二个 public 交换机。您可以使用它来执行任何 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: $_"
}