0

I have script which installs software (.exe file) via PowerShell

Get-ChildItem "D:\" -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 | 
Foreach { & $_ -s2 -sp"-SilentInstallation=standalone-  UpdateMaterials=yestoall -UpgradeDBIfRequired=yes"}

I need to run this script via PowerShell so I created a script for this

# copy installation script to Vagrant folder

Copy-Item -Path "C:\Users\Username\Desktop\Scripts\Installation_Ortho.ps1" -Destination "C:\VagrantBoxes\Win8"

 # Navigate to vagrant folder

 CD "C:\VagrantBoxes\Win8"

  #vagrant powershell

 vagrant.exe powershell

 # navigate to the folder which is shared with Vagrant

 CD "C:\vagrant"

Unfortunately I receive an error

 C:\VagrantBoxes\Win8> vagrant powershell
==> default: Detecting if a remote PowerShell connection can be made with the guest...
default: Creating powershell session to 127.0.0.1:55985
default: Username: vagrant

Oops, something went wrong.  Please report this bug with the details  below.
Report on GitHub: https://github.com/lzybkr/PSReadLine/issues/new

Last 0 Keys:


Exception:
System.IO.IOException: The handle is invalid.

at System.IO.__Error.WinIOError(Int32 errorCode, String maybeFullPath)
at System.Console.GetBufferInfo(Boolean throwOnNoConsole, Boolean& succeeded)
at Microsoft.PowerShell.PSConsoleReadLine.Initialize(Runspace runspace, EngineIntrinsics engineIntrinsics)
at Microsoft.PowerShell.PSConsoleReadLine.ReadLine(Runspace runspace, EngineIntrinsics engineIntrinsics)

I am looking for solution which helps me:

1) Navigate through PowerShell to a folder inside Vagrant box where script is placed;

2) Execute a PowerShell script inside Vagrant box and receive installed software in Vagrant boxe.

Mikhail R
  • 391
  • 2
  • 6
  • 17
  • why don't you use [vagrant provisioning](https://www.vagrantup.com/docs/provisioning/shell.html) you can run your PS script if you defined from your Vagrantfile something like `config.vm.provision "shell", path: "path_to_ps_script"` vagrant will even take care that your ps_script is copied over to the VM – Frederic Henri Jun 21 '16 at 13:13
  • Thank for reply, Frédéric. I am looking for automation through PowerShell as much as it possible. That's why I need commands which I can add to my script and execute them via PowerShell in Vagrant. – Mikhail R Jun 21 '16 at 13:36
  • 2
    I understand but vagrant can run the script just for you so you don't need to copy the PS script + invoke vagrant powershell + execute the script, it is done automatically for you through the provisioning – Frederic Henri Jun 21 '16 at 13:37
  • But what if I need to run 3 scripts one by one? Also I tried this command config.vm.provision "shell", path: "C:\Users\User Name\Desktop\Scripts_Ready\Install_Vagrant.cmd" And receive an error http://pastebin.com/t4CH8qrx – Mikhail R Jun 21 '16 at 13:50
  • ` config.vm.provision "shell"` must be placed within your `Vagrantfile`(this is basically a script which tells vagrant what to do when you run `vagrant` command) in the `path` argument you must set the location of your _powershell_ script that will be run when you boot your VM, you can have 3 times one for each script (that you define in path argument) `config.vm.provision "shell", path: "location of the 3 scripts"` – Frederic Henri Jun 21 '16 at 13:54
  • Yeah, thanks again - I thought that it is possible to run some command to add path to the script to Vagrant file via PowerShell. But again - I should do it manually... Which is not good. I almost build scripts which allow me to: install vagrant, install virtualbox, copy .exe files which I need to Vagrant folder. I did everything - and all the process is automated. But now I stuck on the final step - I need to automate process of installation .exe file inside Vagrant box without manual edit of Vagrant file because when I put these scripts to TeamCity it will be impossible to update Vagrant file – Mikhail R Jun 21 '16 at 14:02
  • ok I understand, I dont want to insist but you could make modification of the vagrantfile from your initial script so you have a script to do your vagrant install etc + make modif of Vagrantfile, run vagrant up and everything is taken care from there by vagrant itself. – Frederic Henri Jun 21 '16 at 14:08
  • I know that you are non insist :) I am glad that you reply. I guess it will be very complex to modify Vagrant file with many lines and uncomment specific lines with text addition... – Mikhail R Jun 21 '16 at 14:40
  • don't know your use case but you could easily have a placeholder in your Vagrantfile like `#CHANGEME` and [replace this occurrence by your provisioning lines](http://stackoverflow.com/questions/17144355/string-replace-file-content-with-powershell) might be less complex than you think and less complex that fighting the `vagrant powershell` command – Frederic Henri Jun 21 '16 at 14:51
  • For some reasons after several updates of Vagrant box 'powershell' command now works, but I keep in mind your advises. Thank you! – Mikhail R Jun 21 '16 at 15:37

0 Answers0