Posts Tagged ‘threading’

This is an example of a very simple multi thread script. All it does is output 2 user names per function.
But you can only imagine the power.

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
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
#Create a function, or in this example create/split up a large job into multiple functions.
 
#Step 1 : Create threading.ps1
#Step 2 : Create function and add it to threading.ps1
 
#(Sample Function)
 
function one{
	$users1 = "CORP1Bollasr1", "CORP2aardeje1"
		foreach ($u in $users1){
			Write-Output "$u"
		}
}
one | out-File C:lnloneresults.txt
 
 
#Step 3 : Wrap the function inside a function with (Write-Output ' ')  around each line. 
#(Sample Wrapped Function)
 
function createone{
Write-Output 'function one{'
Write-Output '	$users1 = "CORP1Bollasr1", "CORP2aardeje1"'
Write-Output '		foreach ($u in $users1){'
Write-Output '			Write-Output "$u"'
Write-Output '		}'
Write-Output '}'
Write-Output 'one | out-File C:lnloneresults.txt'
}
 
 
#Now I will show an example of 5 threads. This file is called threading.ps1
 
##Create Multi Thread Script
function createone{
Write-Output 'function one{'
Write-Output '	$users1 = "CORP1Bollasr1", "CORP2aardeje1"'
Write-Output '		foreach ($u in $users1){'
Write-Output '			Write-Output "$u"'
Write-Output '		}'
Write-Output '}'
Write-Output 'one | out-File C:lnloneresults.txt'
}
 
 
function createtwo{
Write-Output 'function two{'
Write-Output '	$users2 = "CORP1dudlesu1", "CORP2duerrma1"'
Write-Output '		foreach ($u in $users2){'
Write-Output '			Write-Output "$u"'
Write-Output '		}'
Write-Output '}'
Write-Output 'two | out-File C:lnltworesults.txt'
}
 
 
function createthree{
Write-Output 'function three{'
Write-Output '	$users3 = "CORP1khattpa2", "CORP1khattsa1"'
Write-Output '		foreach ($u in $users3){'
Write-Output '			Write-Output "$u"'
Write-Output '		}'
Write-Output '}'
Write-Output 'three | out-File C:lnlthreeresults.txt'
}
 
 
function createfour{
Write-Output 'function four{'
Write-Output '	$users4 = "CORP1pellath1", "CORP2pellian5"'
Write-Output '		foreach ($u in $users4){'
Write-Output '			Write-Output "$u"'
Write-Output '		}'
Write-Output '}'
Write-Output 'four | out-File C:lnlfourresults.txt'
}
 
 
function createfive{
Write-Output 'function five{'
Write-Output '	$users5 = "CORP1trancu1", "CORP2tranth8"'
Write-Output '		foreach ($u in $users5){'
Write-Output '			Write-Output "$u"'
Write-Output '		}'
Write-Output '}'
Write-Output 'five | out-File C:lnlfiveresults.txt'
}
 
 
#Step 4 : Create .ps1 scripts out of the functions.
#Take note that .... alows for a releative path.
 
createone | out-File ....Threadone.ps1
createtwo | out-File ....Threadtwo.ps1
createthree | out-File ....Threadthree.ps1
createfour | out-File ....Threadfour.ps1
createfive | out-File ....Threadfive.ps1
 
 
#Step 5 : Launch threading script.
 
start-job -filepath ....Threadone.ps1
start-job -filepath ....Threadtwo.ps1
start-job -filepath ....Threadthree.ps1
start-job -filepath ....Threadfour.ps1
start-job -filepath ....Threadfive.ps1
 
#Step 6 : Check Status of Jobs
#The Wait-Job cmdlet waits for Windows PowerShell background jobs to complete before it displays the command prompt.
 
get-job | wait-job
 
#Step 7 : Stop any Job that may be hung.
#The Stop-Job cmdlet stops Windows PowerShell background jobs that are in progress.
 
get-job | stop-job
 
#full Script called threading.ps1
##Create Multi Thread Script
 
function createone{
Write-Output 'function one{'
Write-Output '	$users1 = "CORP1Bollasr1", "CORP2aardeje1"'
Write-Output '		foreach ($u in $users1){'
Write-Output '			Write-Output "$u"'
Write-Output '		}'
Write-Output '}'
Write-Output 'one | out-File C:lnloneresults.txt'
}
 
 
function createtwo{
Write-Output 'function two{'
Write-Output '	$users2 = "CORP1dudlesu1", "CORP2duerrma1"'
Write-Output '		foreach ($u in $users2){'
Write-Output '			Write-Output "$u"'
Write-Output '		}'
Write-Output '}'
Write-Output 'two | out-File C:lnltworesults.txt'
}
 
 
function createthree{
Write-Output 'function three{'
Write-Output '	$users3 = "CORP1khattpa2", "CORP1khattsa1"'
Write-Output '		foreach ($u in $users3){'
Write-Output '			Write-Output "$u"'
Write-Output '		}'
Write-Output '}'
Write-Output 'three | out-File C:lnlthreeresults.txt'
}
 
 
function createfour{
Write-Output 'function four{'
Write-Output '	$users4 = "CORP1pellath1", "CORP2pellian5"'
Write-Output '		foreach ($u in $users4){'
Write-Output '			Write-Output "$u"'
Write-Output '		}'
Write-Output '}'
Write-Output 'four | out-File C:lnlfourresults.txt'
}
 
 
function createfive{
Write-Output 'function five{'
Write-Output '	$users5 = "CORP1trancu1", "CORP2tranth8"'
Write-Output '		foreach ($u in $users5){'
Write-Output '			Write-Output "$u"'
Write-Output '		}'
Write-Output '}'
Write-Output 'five | out-File C:lnlfiveresults.txt'
}
 
createone | out-File ....Threadone.ps1
createtwo | out-File ....Threadtwo.ps1
createthree | out-File ....Threadthree.ps1
createfour | out-File ....Threadfour.ps1
createfive | out-File ....Threadfive.ps1
 
start-job -filepath ....Threadone.ps1
start-job -filepath ....Threadtwo.ps1
start-job -filepath ....Threadthree.ps1
start-job -filepath ....Threadfour.ps1
start-job -filepath ....Threadfive.ps1
 
get-job | wait-job
 
get-job | stop-job
  • Archives

  • Tags

  • Subscribe
  • Pages

  • More

  • Disclaimer…

    This is my personal weblog. The opinions expressed herein are my own and are not representative of any 3rd party influence. The owner of this blog reserves the right to edit or delete any comments submitted to this blog without notice if they are deemed to be spam, offensive or otherwise inappropriate. The owner of this blog makes no representations as to the accuracy or completeness of any information on this site or found by following any link on this site. The owner will not be liable for any errors or omissions in this information nor for the availability of this information. The owner will not be liable for any losses, injuries, or damages from the display or use of this information.


    Lastly, I do my best to document my sources if the article is not of my own creation. If I have missed or forgotten to source your work. I would love feedback via the comments section. Thank you.

DreamHost promos
SiteLock