-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathPyPS.py
More file actions
74 lines (54 loc) · 2.38 KB
/
PyPS.py
File metadata and controls
74 lines (54 loc) · 2.38 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
__all__ = ['PyPs']
__author__ = "Aslan Gurtsiev"
import os
import sys
import clr
SMA_Dir = (lambda s: s + os.listdir(s)[0])(
r'C:\Windows\Microsoft.NET\assembly\GAC_MSIL\System.Management.Automation\\')
sys.path.append(SMA_Dir)
clr.AddReference(r"C:\Windows\Microsoft.NET\Framework64\v4.0.30319\System.Runtime.dll")
clr.AddReference("System.Management.Automation")
clr.AddReference('System.Collections')
clr.AddReference('System.Management')
from System import Guid
from System.Diagnostics.Eventing import EventProvider
from System.Management.Automation import RunspaceInvoke
from System.Management.Automation.Runspaces import RunspaceFactory
from System.Reflection import BindingFlags
from System.Text import StringBuilder
class PyPs:
"""This class allows you to run powershell commands from your python code.
Accepts one parameter: (silent). If set, the script won't leave any traces
in Powershell logs"""
def __init__(self, silent=False):
if silent:
self._disable_logging()
def _disable_logging(self):
"""Taken from:
https://gist.github.com/benpturner/cb49sd37eb7eb3cfc0b6ea03dd00750c8"""
newrunspace = RunspaceFactory.CreateRunspace()
psEtwLogProvider = newrunspace.GetType().Assembly.GetType("System.Management.Automation.Tracing.PSEtwLogProvider")
if psEtwLogProvider:
etwProvider = psEtwLogProvider.GetField("etwProvider", BindingFlags.NonPublic | BindingFlags.Static)
eventProvider = EventProvider(Guid.NewGuid())
etwProvider.SetValue(None, eventProvider)
@staticmethod
def run_ps(command: str) -> str:
"""Runs the powershell command/script"""
runspace = RunspaceFactory.CreateRunspace()
runspace.Open()
RunspaceInvoke(runspace)
pipeline = runspace.CreatePipeline()
pipeline.Commands.AddScript(command)
pipeline.Commands.Add("Out-String")
result = pipeline.Invoke()
runspace.Close()
stringBuilder = StringBuilder()
for each in result:
stringBuilder.Append(each)
return stringBuilder.ToString()
if __name__ == '__main__':
# example: Running command "get=process" silently
command = 'get-process | select-object -property name'
output = PyPs(True).run_ps(command)
print(output)