1

I tried to execute these commands to install .exe file inside Vagrant box

# Copyfile from network shared folder to folder in host machine

Get-ChildItem "L:\" -Filter *.exe | Where Name -NotMatch '.*NoDB\.exe$' | % {
New-Object psobject -Property @{
    No = [int]([regex]::Match($_.Name, '(?<=CL)\d+').Value)
    Name = $_.FullName
}

} | Sort No -Descending | Select -ExpandProperty Name -First 1 | Copy-Item -Destination "C:\VagrantBoxes\Win8"

# Copy installation script to Vagrant folder which is share with Vagrant

Copy-Item -Path "C:\Users\PS\Des\Scr_Re_Win_8\Install_Ort.ps1" -Destination "C:\VagrantBoxes\Win8"

# Navigate to Vagrant machine folder

CD "C:\VagrantBoxes\Win8"

# Check if Vagrant is up

 vagrant.exe up

# Run PowerShell in Vagrant

vagrant.exe powershell

# Navigate to the folder which is shared with Vagrant

CD "C:\vagrant"

#Set policy to Unrestricted

Set-ExecutionPolicy Unrestricted -Force

# Install Chocolatey

iex ((new-object net.webclient).DownloadString('https://chocolatey.org/install.ps1'))

# Install .net 3.5

   choco install dotnet3.5 -force

 # Run Ort installation script

 .\Install_Ort.ps1

But in the end when PowerShell is activated in Vagrant box I can't execute last commands

 # Navigate to the folder which is shared with Vagrant

CD "C:\vagrant"

#Set policy to Unrestricted

Set-ExecutionPolicy Unrestricted -Force

# Install Chocolatey

iex ((new-object net.webclient).DownloadString('https://chocolatey.org/install.ps1'))

# Install .net 3.5

   choco install dotnet3.5 -force

 # Run Ort installation script

 .\Install_Ort.ps1

I see that PowerShell console is active in Vagrant but commands are not executed via PowerShell inside Vagrant box. I can manually type commands but not as part of the script.

Mikhail R
  • 391
  • 2
  • 6
  • 17
  • hey Mikhail, you probably have to put anything below `vagrant.exe powershell` into a new script and invoke the script within vagrant. But Im not familiar mit vagrant. – Martin Brandl Jun 22 '16 at 15:24
  • 1
    @MartinBrandl, indeed it should probably be the best solution, we had [discussion about using vagrant provisioner yesterday](http://stackoverflow.com/questions/37944980/run-powershell-script-installation-of-exe-file-in-silient-model-in-vagrant-bo#comment63344216_37944980) :) but Mikhail wanted to do all in PS; using the `command` option as suggested in the answer is an alternative and make sure the script will be run on the guest – Frederic Henri Jun 22 '16 at 15:34
  • @FrédéricHenri Well, then you provided the right solution for him and also explained the problem. – Martin Brandl Jun 22 '16 at 15:38
  • Martin, thanks for advice. Yes, @FrédéricHenri is right. But thanks, both of you. – Mikhail R Jun 22 '16 at 15:58

1 Answers1

2

You can't execute the commands from the same powershell script that you are currently running, it will install on your host machine.

what you want is to pass the command to the guest powershell of your VM, you can do that with -c COMMAND option like

vagrant.exe powershell -c "CD C:\vagrant"
vagrant.exe powershell -c "Set-ExecutionPolicy Unrestricted -Force"
...
Frederic Henri
  • 51,761
  • 10
  • 113
  • 139
  • Super! It works. Almost... It fails on the last step # Run Ort installation script vagrant.exe powershell -c ".\Install_Ort.ps1" The following WinRM command responded with a non-zero exit status. – Mikhail R Jun 22 '16 at 16:03
  • can you try with `InvokeExpression` to call PS and also with full path .. not sure it keeps the location after you done `cd` in the first call, if its like ssh it initiates a new connection everytime – Frederic Henri Jun 22 '16 at 17:06
  • Frédéric Henri it works! I use this cmdlet 'vagrant.exe powershell -c "C:\vagrant\Install_Ortho.ps1 | Invoke-Expression"' and it worked! Hurray! – Mikhail R Jun 23 '16 at 09:20