-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathCalling_PowerShell.py
134 lines (104 loc) · 4.41 KB
/
Calling_PowerShell.py
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
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
# I am learning Django and thinking about calling PowerShell scripts behind the scenes, so its a POC for my self.
# Below are three different ways of executing PowerShell code.. these are:
# - Command only
# - Script
# - Script with arguments
# They all work the same.. just a list with elements in the right order
# example_script.ps1 just contains simple output based on optional parameters:
# param( [string]$UserName = "default_user", [string]$Title = "default_title", [string]$Tool = "default_tool" )
# "I am $UserName, working as `"$Title`" and primarily with $Tool"
import subprocess
arg_username = "Maekee"
arg_title = "Python Padawan"
arg_tool = "PyCharm"
powershell_path = r'C:\Windows\System32\WindowsPowerShell\v1.0\powershell.exe'
powershell_script_path = r'c:\powershell\example_script.ps1'
powershell_exepolicy = 'Bypass'
powershell_command_to_run = r'Write-Host "Hello Hejsan from Sweden"'
# Code example 1: Call PowerShell with Command
execute_powershell = [
powershell_path,
"-ExecutionPolicy",
powershell_exepolicy,
"-Command",
powershell_command_to_run
]
result = subprocess.run(execute_powershell, stdout=subprocess.PIPE, stderr=subprocess.PIPE)
print(result.stdout.decode('utf-8'))
# Code example 2: Call PowerShell script without parameters (using the defaults):
execute_powershell = [
powershell_path,
"-ExecutionPolicy",
powershell_exepolicy,
"-File",
powershell_script_path
]
result = subprocess.run(execute_powershell, stdout=subprocess.PIPE, stderr=subprocess.PIPE)
print(result.stdout.decode('utf-8'))
# Code example 3: Call PowerShell script with two out of three parameters:
execute_powershell = [
powershell_path,
"-ExecutionPolicy",
powershell_exepolicy,
"-File",
powershell_script_path,
"-UserName",
arg_username,
"-Title",
arg_title
]
result = subprocess.run(execute_powershell, stdout=subprocess.PIPE, stderr=subprocess.PIPE)
print(result.stdout.decode('utf-8'))
# As you see.. the same method, just different PowerShell parameter (Command vs File) with other parameters supplied in the list.
# Also created a function in python that does this:
def get_userinformation(username=None, title=None, tool=None):
"""Function returns a custom message if (named) arguments are supplied
Args:
username (str): Optional UserName to be printed
title (str): Optional Title to be printed
tool (str): Optional favourite tool to be printed
Returns:
A string based on arguments to function
Examples:
>>> get_userinformation(username='Maekee')
'I am Maekee, working as "default_title" and primarily with default_tool'
>>> get_userinformation(username='Maekee', title='Technician', tool='PyCharm')
'I am Maekee, working as "Technician" and primarily with PyCharm'
Raises:
TimeoutExpired: If called PowerShell script does not return stdout/stderr within 10 seconds
"""
import subprocess
powershell_path = r'C:\Windows\System32\WindowsPowerShell\v1.0\powershell.exe'
powershell_script_path = r'c:\powershell\example_script.ps1'
powershell_exepolicy = 'Bypass'
execute_powershell = [
powershell_path,
"-ExecutionPolicy",
powershell_exepolicy,
"-File",
powershell_script_path,
]
if username:
execute_powershell.append('-UserName')
execute_powershell.append(username)
if title:
execute_powershell.append('-Title')
execute_powershell.append(title)
if tool:
execute_powershell.append('-Tool')
execute_powershell.append(tool)
result = subprocess.run(
execute_powershell,
stdout=subprocess.PIPE,
stderr=subprocess.PIPE,
timeout=10
)
if result.returncode == 0:
return result.stdout.decode('utf-8').strip()
else:
return result.stderr.decode('utf-8').strip()
# If you want to make PowerShell script parameters mandatory, the Mandatory parameter attribute will cause PowerShell to interactively ask
# the user for the value and wait... this causes the Python function to stop responding. I added the timeout=10 (seconds) argument to subprocess
# to exit the script. But a better way if you can edit the PowerShell-script is to skip the Mandatory flag and instead use:
# [string]$UserName = $(throw "UserName is mandatory, please provide a value.'")
# this will cause PS to throw an exception and the Python will catch it.