jborean93 / pypsexec

Remote Windows execution like PsExec on Python
MIT License
119 stars 38 forks source link

Edit Registry values using either powershell or cmd #25

Closed aaslam-1945 closed 4 years ago

aaslam-1945 commented 4 years ago

Hi Jordan,

In my use case i want to edit, add or delete registry values on a remote Windows 10 machine using your wonderful package. Here is my syntax but its not working.

stdout, stderr, rc = conn.run_executable("cmd.exe",arguments='/c reg add "HKLM:\SOFTWARE" /v hsg')

or

Something like this New-Item -Path "HKLM:\SOFTWARE" -Name "hsg" in Powershell.exe

or using a .reg file

stdout, stderr, rc = conn.run_executable("regedit.exe",arguments='/s "/localhost location/regFile.reg"')

Any help in this regard would be highly appreciated.

jborean93 commented 4 years ago

The issue you have here is the backslash, when used in a python string it's used as a string escape character, e.g. \n is a newline, \t is a table, etc.

You have 2 ways of getting around this

  1. Just double up on the backslash to escape the escaping behaviour, "HKLM:\\SOFTWARE"
  2. Put an r in front of the string to stop the escaping behaviour, r"HKLM:\SOFTWARE"

Considering reg is it's own executable you can also bypass calling it from cmd.exe like so

stdout, stderr, rc = conn.run_executable("reg.exe", arguments='add "HKLM:\\SOFTWARE" /v hsg')

If you are still having trouble please post the stdout, and stderr that you are getting back.

jborean93 commented 4 years ago

Closing as per the above.