vagrant powershell elevated

283 views
Skip to first unread message

Ozan Cansel

unread,
Nov 14, 2018, 8:04:39 AM11/14/18
to Vagrant
Hello, I want to run my powershell script dynamically with elevated privileges. For example, on the host, I want to run vagrant powershell -c "any command here". I know it can be done with provisioning with elevated privilege but I need to run it on after 10 minutes from machine is up. Is it possible ? I searched, googled but I could not found. On winrm-plugin documents supports it with vagrant winrm -c -e "<cmd>" but when I installed that plugin vagrant suggests to uninstall it. Could you help me about it ?

Alvaro Miranda Aguilera

unread,
Nov 15, 2018, 3:42:37 PM11/15/18
to vagra...@googlegroups.com
hello

vagrant ssh  

or
vagrant powershell will open a session as the vagrant user

or run a command with -c

you will have a way to run the command as admin

in linux . vagrant ssh -c 'sudo <sommand>' does the trick

in windows I am familiar only with `runas` but not sure whats the proper powershell way.


vagrant will use the vagrant user 

alvaro.

On Wed, Nov 14, 2018 at 2:04 PM Ozan Cansel <ozanc...@gmail.com> wrote:
Hello, I want to run my powershell script dynamically with elevated privileges. For example, on the host, I want to run vagrant powershell -c "any command here". I know it can be done with provisioning with elevated privilege but I need to run it on after 10 minutes from machine is up. Is it possible ? I searched, googled but I could not found. On winrm-plugin documents supports it with vagrant winrm -c -e "<cmd>" but when I installed that plugin vagrant suggests to uninstall it. Could you help me about it ?

--
This mailing list is governed under the HashiCorp Community Guidelines - https://www.hashicorp.com/community-guidelines.html. Behavior in violation of those guidelines may result in your removal from this mailing list.
 
GitHub Issues: https://github.com/mitchellh/vagrant/issues
IRC: #vagrant on Freenode
---
You received this message because you are subscribed to the Google Groups "Vagrant" group.
To unsubscribe from this group and stop receiving emails from it, send an email to vagrant-up+...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/vagrant-up/4227b10b-3ebf-49f4-b188-88bb2b9567cd%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.


--
Alvaro

Ozan Cansel

unread,
Nov 16, 2018, 12:34:09 AM11/16/18
to Vagrant
My all guests are Windows. In powershell, I can invoke commands but Start-Process destroys after closing session [it is normal]. Invoke-Wmi or Diagnostics.Process::Start runs a process detached but there is a problem also. 'vagrant powershell -c "<cmd>"' doesn't run as elevated or privileged so my process doesn't run as elevated. 

At last, I achieved but in a ugly way. I am writing my solution which might help someone who suffered from the same problem.

When I want to run a script as elevated,
I insert the snippet to beginning of the VagrantFile, after I provision the  guest with '--provision-with <myscriptname>' option. After provisioning I revert my file.

Before run : 

Vagrant.configure(2) do |config|
config.vm.box = "win10_1607"
config.vm.network :forwarded_port, guest: 5985, host: 5985, id: "winrm", auto_correct: true
config.winrm.guest_port = 5985
config.vm.provider "virtualbox" do |vb|
vb.memory = "2048"
end
config.vm.network "private_network", ip: "192.168.50.11"
end

On running, I am adding my script :

$runElevatedScript = <<-SCRIPT
$start = new-object System.Diagnostics.ProcessStartInfo
$start.FileName = "some.exe"
$start.Arguments = "--silent"
$start.Verb = "runas"
[Diagnostics.Process]::Start($start)
SCRIPT

Vagrant.configure(2) do |config|
config.vm.box = "win10_1607"
config.vm.network :forwarded_port, guest: 5985, host: 5985, id: "winrm", auto_correct: true
config.winrm.guest_port = 5985
config.vm.provider "virtualbox" do |vb|
vb.memory = "2048"
end
#Provision here
config.vm.provision "myscriptname", type: "shell", inline: $runElevatedScript
end

I provision at here with 'vagrant provision --provision-with myscriptname'

After I revert my VagrantFile

Vagrant.configure(2) do |config|
config.vm.box = "win10_1607"
config.vm.network :forwarded_port, guest: 5985, host: 5985, id: "winrm", auto_correct: true
config.winrm.guest_port = 5985
config.vm.provider "virtualbox" do |vb|
vb.memory = "2048"
end
config.vm.network "private_network", ip: "192.168.50.11"
end




15 Kasım 2018 Perşembe 23:42:37 UTC+3 tarihinde Alvaro Miranda Aguilera yazdı:
Reply all
Reply to author
Forward
0 new messages