Powershell Scripting

I just found out that windows is able to run ssh inside powershell and it got me thinking about writing a script to help start and stop VMs.

Currently my main workstation is a Windows VM on proxmox with gpu pass through. Would there be a way to write a script that logs on to my “host machine” over ssh, executes the VM shutdown command, the starts the next VM of my choosing? This would make it a lot easier to switch over to my Linux VM. Currently I am able to shutdown the Win10 VM in the web gui, then once that’s shutdown, access the webgui on my phone and start the Linux VM. If I knew the coding a bit better I’d love to have a script on my desktop for both VMs that issues the commands and all I have to do is execute it.

Something like:
“reboot to Linux:”
ssh user@hostname
screen qm stop 100 && qm start 101 && exit

I am not quite sure how to make it so the connection isn’t broken before the second command is issued. Possibly “screen?”

Not sure yet how to make it into a powershell script but I’ll keep looking.

Just make a shell script on the linux host. SSH into the host via a script in the VM, and start execution of the on the host script, then as the VM shuts down the host script will keep running to boot the other VM.

Powershell would work, but it takes slightly longer to boot then a bat script does, and cmd still has access to ssh, so I would recommend using a bat/cmd script to ssh into the host rather then PS.

I’m not tied to PS by any means. CMD would work fine, looking for something that is more of double click and fire away type of execution. If I have to load up putty and execute the script there on the host then that’s a few extra steps that I don’t want to worry about.

If you’re not bound to powershell, or windows… you can even actually do this via siri shortcuts (ssh into a host and run commands) on an iphone or ipad, etc.

Then you can just do “hey siri, reboot into windows” and it will kick off your shortcut and run your shell script via ssh.

Siri shortcuts is pretty powerful. :smiley:

But yeah otherwise… ssh into host, run script. The details will be in your shell script on the linux host. But yes it is certainly possible.

1 Like

Just another tip, try to avoid putting usernames and password into scripts. With SSH you have the option of using certificates (public/private key) to authenticate against a machine. That makes running stuff on the linux target a charm. You can even run a specific command on the linux machine when loggin in with a specific user using certificates.

See SSH authorized_keys and in section “format of the authorized keys file->command=‘cmd’” it is explained how to run commands using this technique.

So, my approach would be loggin in with a key and specifying the shutdown command, to be run when logging in, in the authorized_keys file.

1 Like

Been looking at it and I think a combination of writing the shutdown/start script on the host, and then using a desktop shortcut to execute and call that script from each VM will work best.

How would I go about using Siri? That has me interested…

  • Download siri shortcuts app on your iOS device
  • create a shortcut
  • pick “ssh” from the library on the left
  • fill in the blanks to connect to your host and the commands to run the shell script

I forget how to make it respond to hey siri, i think the current version is smart enough to understand what you mean when you say “hey siri name of your shortcut”. I haven’t played with it much since 2018.

What i really want is Siri shortcuts to be linked into the home app. So that i can do siri shortcuts style code in homekit. But not yet…

I tried that approach but shortcuts doesn’t recognize the key I generated for my iPhone. It only works on password based ssh. Not really ideal or worth opening up that risk. Is there a way to tell it to use a specific key for the connection?

Haven’t tried it with keys, like i said it was a while ago. That’s a shame.

Yeah seems as though it only supports password auth, not key auth. That’s unfortunate. It would be nice to have a shortcut on the homescreen as well for the same function.