Skip to content

Instantly share code, notes, and snippets.

@phuysmans
Forked from thinkerbot/run.bat
Created May 7, 2020 08:00
Show Gist options
  • Save phuysmans/24ae885a836054a0cc892f48432b326e to your computer and use it in GitHub Desktop.
Save phuysmans/24ae885a836054a0cc892f48432b326e to your computer and use it in GitHub Desktop.
Execute a powershell script via ssh
# Notes:
# 1) The input format must be specified to prevent ssh from hanging
# 2) The full path to the script is required
# 3) An SSH server must be running on the Windows box, naturally. WinSSHD
# is free for personal, non-commercial use and worked for this example.
# See http://www.bitvise.com/winsshd
#
# You have to set the execution policy for scripts so that powershell has
# permissions to run the script. To do so, search for 'Powershell' under
# the Start menu. Then right-click and 'Run as administrator' so that you
# have permissions to set permissions. Then:
#
# PS> Set-ExecutionPolicy RemoteSigned
#
# As a result you can run scripts you write but not unsigned ones from
# remote sources. See http://technet.microsoft.com/en-us/library/ee176949.aspx
#
# To execute via ssh:
#
# $ ssh user@host '"C:\path\to\run.bat"'
#
# Note the inner quotes are superfluous here but required if the path has
# a space in it.
powershell -InputFormat None -F "C:\script.ps1"
# As per the bat file, put this at "C:\script.ps1"
echo "hello world"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment