Mikhail R
Mikhail R

Reputation: 411

Create Vagrant file via PowerShell

I have these lines which I would like to add to Vagrant file which I would like to create via PowerShell

Vagrant.configure("2") do |config|
config.vm.communicator = "winrm"  
config.vm.box = "Win_10_V.box"
config.vm.provider "virtualbox" do |vb|
    vb.customize ["modifyvm", :id, "--usb", "on"]
    vb.customize ["modifyvm", :id, "--usbehci", "on"]       
    vb.customize ['usbfilter', 'add', '0', '--target', :id, '--name', 'SmartCard', '--vendorid', '0x096E', '--productid', '0x0007']
    vb.gui = true
end
end

I tried this PowerShell cmdlet but it doesn't work because of multiple " symbols.

New-Item "D:\VV\Vagrantfile" -type file -force -value "Vagrant.configure("2") do |config|
config.vm.communicator = "winrm"  
config.vm.box = "W_1.box"
config.vm.provider "virtualbox" do |vb|
    vb.customize ["modifyvm", :id, "--usb", "on"]
    vb.customize ["modifyvm", :id, "--usbehci", "on"]       
    vb.customize ['usbfilter', 'add', '0', '--target', :id, '--name', 'SmartCard', '--vendorid', '0x096E', '--productid', '0x0007']
    vb.gui = true
end
end"

Here is error output

New-Item : A positional parameter cannot be found that accepts argument '2) do |config|
config.vm.communicator = winrm  
config.vm.box = Win_10_V.box
config.vm.provider virtualbox do |vb|
    vb.customize [modifyvm, :id, --usb, on]
    vb.customize [modifyvm, :id, --usbehci, on]        
    vb.customize ['usbfilter', 'add', '0', '--target', :id, '--name', 'SmartCard', '--vendorid', '0x096E', '--productid', '0x0007']
    vb.gui = true
end
end'.
At line:1 char:1
+ New-Item "D:\VV\Vagrantfile" -type file -force -value "Vagrant.configu ...
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
+ CategoryInfo          : InvalidArgument: (:) [New-Item], ParameterBindingException
+ FullyQualifiedErrorId : PositionalParameterNotFound,Microsoft.PowerShell.Commands.NewItemCommand

I know that I have positional parameters but I need them in my Vagranfile

Upvotes: 1

Views: 327

Answers (1)

Sean
Sean

Reputation: 62472

The problem is that the double-quote in the value you pass (just before the 2) is terminating the string that you're passing as the value. Re-write is using a here-string:

$value = @"
"Vagrant.configure("2") do |config|
config.vm.communicator = "winrm"  
config.vm.box = "W_1.box"
config.vm.provider "virtualbox" do |vb|
    vb.customize ["modifyvm", :id, "--usb", "on"]
    vb.customize ["modifyvm", :id, "--usbehci", "on"]       
    vb.customize ['usbfilter', 'add', '0', '--target', :id, '--name', 'SmartCard', '--vendorid', '0x096E', '--productid', '0x0007']
    vb.gui = true
end
end
"@

New-Item "D:\VV\Vagrantfile" -type file -force -value $value

This will allow you to have embedded quotes within the string.

Upvotes: 2

Related Questions