-
Notifications
You must be signed in to change notification settings - Fork 10
/
CWCPoSh.psm1
1136 lines (914 loc) · 35.9 KB
/
CWCPoSh.psm1
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
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
<#
.SYNOPSIS
A PowerShell wrapper for the ConnectWise Control API
.DESCRIPTION
This module will allow you to interact with the Control API to issue commands and retrieve data.
.NOTES
Version: 1.0
Author: Chris Taylor
Creation Date: 1/20/2016
Purpose/Change: Initial script development
Update Date: 1/11/2019
Purpose/Change: Move to [PSCredential] authentication
.LINK
labtechconsulting.com
#>
#requires -version 3
if([Net.SecurityProtocolType]::Tls) {
[Net.ServicePointManager]::SecurityProtocol=[Net.ServicePointManager]::SecurityProtocol -bor [Net.SecurityProtocolType]::Tls
}
#region-[Functions]------------------------------------------------------------
function Get-CWCLastContact {
<#
.SYNOPSIS
Returns the date the machine last connected to the control server.
.DESCRIPTION
Returns the date the machine last connected to the control server.
.PARAMETER Server
The address to your Control server. Example 'https://control.labtechconsulting.com' or 'http://control.secure.me:8040'
.PARAMETER GUID
The GUID/SessionID for the machine you wish to connect to.
You can retrieve session info with the 'Get-CWCSessions' commandlet
On Windows clients, the launch parameters are located in the registry at:
HKEY_LOCAL_MACHINE\SYSTEM\CurrentControlSet\Services\ScreenConnect Client (xxxxxxxxxxxxxxxx)\ImagePath
On Linux and Mac clients, it's found in the ClientLaunchParameters.txt file in the client installation folder:
/opt/screenconnect-xxxxxxxxxxxxxxxx/ClientLaunchParameters.txt
.PARAMETER Credentials
[PSCredential] object used to authenticate against Control.
.PARAMETER User
User to authenticate against the Control server.
.PARAMETER Password
Password to authenticate against the Control server.
.PARAMETER Quiet
Will output a boolean result, $True for Connected or $False for Offline.
.PARAMETER Seconds
Used with the Quiet switch. The number of seconds a machine needs to be offline before returning $False.
.PARAMETER Group
Name of session group to use.
.OUTPUTS
[datetime] -or [boolean]
.NOTES
Version: 1.1
Author: Chris Taylor
Creation Date: 1/20/2016
Purpose/Change: Initial script development
Update Date: 8/24/2018
Purpose/Change: Fix Timespan Seconds duration
.EXAMPLE
Get-CWCLastContact -Server $Server -GUID $GUID -User $User -Password $Password
Will return the last contact of the machine with that GUID.
#>
[CmdletBinding()]
param(
[Parameter(Mandatory=$True)]
[string]$Server,
[Parameter(Mandatory=$True)]
[guid]$GUID,
[Parameter(Mandatory=$True, ParameterSetName='password')]
[string]$User,
[Parameter(Mandatory=$True, ParameterSetName='password')]
[string]$Password,
[switch]$Quiet,
[int]$Seconds,
[string]$Group = "All Machines",
[Parameter(Mandatory=$True, ParameterSetName='cred')]
[PSCredential]$Credentials
)
# Time conversion
$origin = New-Object -Type DateTime -ArgumentList 1970, 1, 1, 0, 0, 0, 0
$epoch = $((New-TimeSpan -Start $(Get-Date -Date "01/01/1970") -End $(Get-Date)).TotalSeconds)
if($Password) {
$secpasswd = ConvertTo-SecureString $Password -AsPlainText -Force
$Credentials = New-Object System.Management.Automation.PSCredential ($User, $secpasswd)
Write-Warning "Switch to -Credentials [PSCredential] authentication method."
}
$encodedCredentials = [System.Convert]::ToBase64String([System.Text.Encoding]::ASCII.GetBytes("$($Credentials.UserName):$($Credentials.GetNetworkCredential().Password)"))
$Headers = @{ Authorization = "Basic $encodedCredentials" }
$Body = ConvertTo-Json @(@($Group),$GUID) -Compress
Write-Verbose $Body
$URl = "$Server/Services/PageService.ashx/GetSessionDetails"
try {
$SessionDetails = Invoke-RestMethod -Uri $url -Method Post -ContentType "application/json" -Body $Body -Headers $Headers
}
catch {
Write-Error "$($_.Exception.Message)"
return
}
if ($SessionDetails -eq 'null' -or !$SessionDetails) {
Write-Error "Machine not found."
return $null
}
# Filter to only guest session events
$GuestSessionEvents = ($SessionDetails.Connections | Where-Object {$_.ProcessType -eq 2}).Events
if ($GuestSessionEvents) {
# Get connection events
$LatestEvent = ($GuestSessionEvents | Where-Object {$_.EventType -in (10,11)} | Sort-Object time)[0]
if ($LatestEvent.EventType -eq 10) {
# Currently connected
if ($Quiet) {
return $True
} else {
return Get-Date
}
}
else {
# Time conversion hell :(
$TimeDiff = $epoch - ($LatestEvent.Time /1000)
$OfflineTime = $origin.AddSeconds($TimeDiff)
$Difference = New-TimeSpan -Start $OfflineTime -End $(Get-Date)
if ($Quiet -and $Difference.TotalSeconds -lt $Seconds) {
return $True
} elseif ($Quiet) {
return $False
} else {
return $OfflineTime
}
}
}
else {
Write-Error "Unable to determine last contact."
return
}
}
function Invoke-CWCCommand {
<#
.SYNOPSIS
Will issue a command against a given machine and return the results.
.DESCRIPTION
Will issue a command against a given machine and return the results.
.PARAMETER Server
The address to your Control server. Example 'https://control.labtechconsulting.com' or 'http://control.secure.me:8040'
.PARAMETER GUID
The GUID identifier for the machine you wish to connect to.
You can retrieve session info with the 'Get-CWCSessions' commandlet
.PARAMETER Credentials
[PSCredential] object used to authenticate against Control.
.PARAMETER User
User to authenticate against the Control server.
.PARAMETER Password
Password to authenticate against the Control server.
.PARAMETER Command
The command you wish to issue to the machine.
.PARAMETER TimeOut
The amount of time in milliseconds that a command can execute. The default is 10000 milliseconds.
.PARAMETER PowerShell
Issues the command in a powershell session.
.PARAMETER Group
Name of session group to use.
.OUTPUTS
The output of the Command provided.
.NOTES
Version: 1.0
Author: Chris Taylor
Creation Date: 1/20/2016
Purpose/Change: Initial script development
.EXAMPLE
Invoke-CWCCommand -Server $Server -GUID $GUID -User $User -Password $Password -Command 'hostname'
Will return the hostname of the machine.
.EXAMPLE
Invoke-CWCCommand -Server $Server -GUID $GUID -User $User -Password $Password -TimeOut 120000 -Command 'iwr -UseBasicParsing "https://bit.ly/ltposh" | iex; Restart-LTService' -PowerShell
Will restart the Automate agent on the target machine.
#>
[CmdletBinding()]
param (
[Parameter(Mandatory=$True)]
[string]$Server,
[Parameter(Mandatory=$True)]
[guid]$GUID,
[Parameter(Mandatory=$True, ParameterSetName='password')]
[string]$User,
[Parameter(Mandatory=$True, ParameterSetName='password')]
[string]$Password,
[string]$Command,
[int]$TimeOut = 10000,
[switch]$PowerShell,
[string]$Group = "All Machines",
[Parameter(Mandatory=$True, ParameterSetName='cred')]
[PSCredential]$Credentials
)
if($Password) {
$secpasswd = ConvertTo-SecureString $Password -AsPlainText -Force
$Credentials = New-Object System.Management.Automation.PSCredential ($User, $secpasswd)
Write-Warning "Switch to -Credentials [PSCredential] authentication method."
}
$origin = New-Object -Type DateTime -ArgumentList 1970, 1, 1, 0, 0, 0, 0
$URI = "$Server/Services/PageService.ashx/AddEventToSessions"
# Format command
$FormattedCommand = @()
if ($Powershell) {
$FormattedCommand += '#!ps'
}
$FormattedCommand += "#timeout=$TimeOut"
$FormattedCommand += $Command
$FormattedCommand = $FormattedCommand | Out-String
$SessionEventType = 44
$Body = (ConvertTo-Json @($Group,@($GUID),$SessionEventType,$FormattedCommand)).Replace('\r\n','\n')
Write-Verbose $Body
$encodedCredentials = [System.Convert]::ToBase64String([System.Text.Encoding]::ASCII.GetBytes("$($Credentials.UserName):$($Credentials.GetNetworkCredential().Password)"))
$Headers = @{ Authorization = "Basic $encodedCredentials" }
# Issue command
try {
$null = Invoke-RestMethod -Uri $URI -Method Post -Headers $Headers -ContentType "application/json" -Body $Body
}
catch {
Write-Error $_
return
}
# Get Session
$URI = "$Server/Services/PageService.ashx/GetSessionDetails"
$Body = ConvertTo-Json @($Group,$GUID)
Write-Verbose $Body
try {
$SessionDetails = Invoke-RestMethod -Uri $URI -Method Post -Headers $Headers -ContentType "application/json" -Body $Body
}
catch {
Write-Error $_
return
}
#Get time command was executed
$epoch = $((New-TimeSpan -Start $(Get-Date -Date "01/01/1970") -End $(Get-Date)).TotalSeconds)
$ExecuteTime = $epoch - ((($SessionDetails.events | Where-Object {$_.EventType -eq 44})[-1]).Time /1000)
$ExecuteDate = $origin.AddSeconds($ExecuteTime)
# Look for results of command
$Looking = $True
$TimeOutDateTime = (Get-Date).AddMilliseconds($TimeOut)
$Body = ConvertTo-Json @($Group,$GUID)
while ($Looking) {
try {
$SessionDetails = Invoke-RestMethod -Uri $URI -Method Post -Headers $Headers -ContentType "application/json" -Body $Body
}
catch {
Write-Error $_
return
}
$ConnectionsWithData = @()
Foreach ($Connection in $SessionDetails.connections) {
$ConnectionsWithData += $Connection | Where-Object {$_.Events.EventType -eq 70}
}
$Events = ($ConnectionsWithData.events | Where-Object {$_.EventType -eq 70 -and $_.Time})
foreach ($Event in $Events) {
$epoch = $((New-TimeSpan -Start $(Get-Date -Date "01/01/1970") -End $(Get-Date)).TotalSeconds)
$CheckTime = $epoch - ($Event.Time /1000)
$CheckDate = $origin.AddSeconds($CheckTime)
if ($CheckDate -gt $ExecuteDate) {
$Looking = $False
$Output = $Event.Data -split '[\r\n]' | Where-Object {$_ -and $_ -ne "C:\WINDOWS\system32>$Command"}
Write-Verbose $Event.Data
return $Output
}
}
Start-Sleep -Seconds 1
if ($(Get-Date) -gt $TimeOutDateTime.AddSeconds(1)) {
$Looking = $False
Write-Warning "Command timed out."
}
}
}
function Get-CWCSessions {
<#
.SYNOPSIS
Will return a list of sessions.
.DESCRIPTION
Allows you to search for access or service sessions.
.PARAMETER Server
The address to your Control server. Example 'https://control.labtechconsulting.com' or 'http://control.secure.me:8040'
.PARAMETER Credentials
[PSCredential] object used to authenticate against Control.
.PARAMETER User
User to authenticate against the Control server.
.PARAMETER Password
Password to authenticate against the Control server.
.PARAMETER Type
The type of session Support/Access
.PARAMETER Group
Name of session group to use.
.PARAMETER Search
Limit results with search pattern.
.PARAMETER Limit
Limit the number of results returned.
.OUTPUTS
ConnectWise Control session objects
.NOTES
Version: 1.0
Author: Chris Taylor
Creation Date: 10/10/2018
Purpose/Change: Initial script development
.EXAMPLE
Get-CWCAccessSessions -Server $Server -User $User -Password $Password -Search "server1" -Limit 10
Will return the first 10 access sessions that match 'server1'.
#>
[CmdletBinding()]
param (
[Parameter(Mandatory=$True)]
[string]$Server,
[Parameter(Mandatory=$True, ParameterSetName='password')]
[string]$User,
[Parameter(Mandatory=$True, ParameterSetName='password')]
[string]$Password,
[Parameter(Mandatory=$True)]
[ValidateSet('Support','Access')]
$Type,
[string]$Group = "All Machines",
[string]$Search,
[int]$Limit,
[Parameter(Mandatory=$True, ParameterSetName='cred')]
[PSCredential]$Credentials
)
if($Password) {
$secpasswd = ConvertTo-SecureString $Password -AsPlainText -Force
$Credentials = New-Object System.Management.Automation.PSCredential ($User, $secpasswd)
Write-Warning "Switch to -Credentials [PSCredential] authentication method."
}
$URI = "$Server/Services/PageService.ashx/GetHostSessionInfo"
switch($Type){
'Support' {$Number = 0}
'Meeting' {$Number = 1}
'Access' {$Number = 2}
default {Write-Error "Unknown Type, $Type";return}
}
$Body = ConvertTo-Json @($Number,@($Group),$Search,$null,$Limit)
Write-Verbose $Body
$encodedCredentials = [System.Convert]::ToBase64String([System.Text.Encoding]::ASCII.GetBytes("$($Credentials.UserName):$($Credentials.GetNetworkCredential().Password)"))
$Headers = @{ Authorization = "Basic $encodedCredentials" }
try {
$Data = Invoke-RestMethod -Uri $URI -Method Post -Headers $Headers -ContentType "application/json" -Body $Body
return $Data.sessions
}
catch {
Write-Error $_
return
}
}
function Remove-CWCSession {
<#
.SYNOPSIS
Will end a given session.
.DESCRIPTION
Will end a given access or support session.
.PARAMETER Server
The address to your Control server. Example 'https://control.labtechconsulting.com' or 'http://control.secure.me:8040'
.PARAMETER Credentials
[PSCredential] object used to authenticate against Control.
.PARAMETER User
User to authenticate against the Control server.
.PARAMETER Password
Password to authenticate against the Control server.
.PARAMETER Type
The type of session Support/Meeting/Access
.PARAMETER GUID
The GUID identifier for the session you wish to end. Accepts an array of GUIDs.
.NOTES
Version: 1.0
Author: Chris Taylor
Creation Date: 10/10/2018
Purpose/Change: Initial script development
.EXAMPLE
Remove-CWCAccessSession -Server $Server -GUID $GUID -User $User -Password $Password
Will remove the given access session
#>
[CmdletBinding()]
param (
[Parameter(Mandatory=$True)]
[string]$Server,
[Parameter(Mandatory=$True)]
[guid[]]$GUID,
[Parameter(Mandatory=$True, ParameterSetName='password')]
[string]$User,
[Parameter(Mandatory=$True, ParameterSetName='password')]
[string]$Password,
[Parameter(Mandatory=$True)]
[ValidateSet('Access','Meeting','Support')]
$Type,
[Parameter(Mandatory=$True, ParameterSetName='cred')]
[PSCredential]$Credentials
)
if($Password) {
$secpasswd = ConvertTo-SecureString $Password -AsPlainText -Force
$Credentials = New-Object System.Management.Automation.PSCredential ($User, $secpasswd)
Write-Warning "Switch to -Credentials [PSCredential] authentication method."
}
$URI = "$Server/Services/PageService.ashx/AddEventToSessions"
switch($Type){
'Support' {$Group = 'All Sessions'}
'Access' {$Group = 'All Machines'}
default {Write-Error "Unknown Type, $Type";return}
}
$SessionEventType = 21
if($GUID.count -eq 1){
$Body = ConvertTo-Json @(@($Group),@($GUID),$SessionEventType,'')
}
else {
$Body = ConvertTo-Json @(@($Group),$GUID,$SessionEventType,'')
}
Write-Verbose $Body
$encodedCredentials = [System.Convert]::ToBase64String([System.Text.Encoding]::ASCII.GetBytes("$($Credentials.UserName):$($Credentials.GetNetworkCredential().Password)"))
$Headers = @{ Authorization = "Basic $encodedCredentials" }
# Issue command
try {
$null = Invoke-RestMethod -Uri $URI -Method Post -Headers $Headers -ContentType "application/json" -Body $Body
}
catch {
Write-Error $(($_.ErrorDetails | ConvertFrom-Json).message)
return
}
}
function Update-CWCSessionName {
<#
.SYNOPSIS
Updates the name of a session.
.DESCRIPTION
Updates the name of a session on the control server.
.PARAMETER Server
The address to your Control server. Example 'https://control.labtechconsulting.com' or 'http://control.secure.me:8040'
.PARAMETER GUID
The GUID/SessionID for the machine you wish to connect to.
You can retrieve session info with the 'Get-CWCSessions' commandlet
On Windows clients, the launch parameters are located in the registry at:
HKEY_LOCAL_MACHINE\SYSTEM\CurrentControlSet\Services\ScreenConnect Client (xxxxxxxxxxxxxxxx)\ImagePath
On Linux and Mac clients, it's found in the ClientLaunchParameters.txt file in the client installation folder:
/opt/screenconnect-xxxxxxxxxxxxxxxx/ClientLaunchParameters.txt
.PARAMETER Credentials
[PSCredential] object used to authenticate against Control.
.PARAMETER User
User to authenticate against the Control server.
.PARAMETER Password
Password to authenticate against the Control server.
.PARAMETER NewName
The new name for the session.
.NOTES
Version: 1.1
Author: Chris Taylor
Creation Date: 10/25/2018
Purpose/Change: Initial script development
.EXAMPLE
Update-CWCSessionName -Server $Server -GUID $GUID -User $User -Password $Password -NewName 'Session1'
Will rename the session to Session1
#>
[CmdletBinding()]
param(
[Parameter(Mandatory=$True)]
[string]$Server,
[Parameter(Mandatory=$True)]
[guid]$GUID,
[Parameter(Mandatory=$True, ParameterSetName='password')]
[string]$User,
[Parameter(Mandatory=$True, ParameterSetName='password')]
[string]$Password,
[Parameter(Mandatory=$True)]
[string]$NewName,
[string]$Group = "All Machines",
[Parameter(Mandatory=$True, ParameterSetName='cred')]
[PSCredential]$Credentials
)
if($Password) {
$secpasswd = ConvertTo-SecureString $Password -AsPlainText -Force
$Credentials = New-Object System.Management.Automation.PSCredential ($User, $secpasswd)
Write-Warning "Switch to -Credentials [PSCredential] authentication method."
}
$Body = ConvertTo-Json @($Group,$GUID,$NewName)
Write-Verbose $Body
$encodedCredentials = [System.Convert]::ToBase64String([System.Text.Encoding]::ASCII.GetBytes("$($Credentials.UserName):$($Credentials.GetNetworkCredential().Password)"))
$Headers = @{ Authorization = "Basic $encodedCredentials" }
$URl = "$Server/Services/PageService.ashx/UpdateSessionName"
try {
$null = Invoke-RestMethod -Uri $url -Method Post -Headers $Headers -ContentType "application/json" -Body $Body
}
catch {
Write-Error $_.Exception.Message
return
}
}
function Invoke-CWCWake {
<#
.SYNOPSIS
Will issue a wake command to a given session.
.DESCRIPTION
Will issue a wake command to a given access or support session.
.PARAMETER Server
The address to your Control server. Example 'https://control.labtechconsulting.com' or 'http://control.secure.me:8040'
.PARAMETER Credentials
[PSCredential] object used to authenticate against Control.
.PARAMETER User
User to authenticate against the Control server.
.PARAMETER Password
Password to authenticate against the Control server.
.PARAMETER Type
The type of session Support/Access
.PARAMETER GUID
The GUID identifier for the session you wish to end. Accepts an array of GUIDs
.NOTES
Version: 1.0
Author: Chris Taylor
Creation Date: 12/7/2018
Purpose/Change: Initial script development
.EXAMPLE
End-CWWake -Server $Server -GUID $GUID -User $User -Password $Password
Will issue a wake command to a given session.
#>
[CmdletBinding()]
param (
[Parameter(Mandatory=$True)]
[string]$Server,
[Parameter(Mandatory=$True)]
[guid[]]$GUID,
[Parameter(Mandatory=$True, ParameterSetName='password')]
[string]$User,
[Parameter(Mandatory=$True, ParameterSetName='password')]
[string]$Password,
[Parameter(Mandatory=$True)]
[ValidateSet('Support','Access')]
[string]$Type,
[Parameter(Mandatory=$True, ParameterSetName='cred')]
[PSCredential]$Credentials
)
if($Password) {
$secpasswd = ConvertTo-SecureString $Password -AsPlainText -Force
$Credentials = New-Object System.Management.Automation.PSCredential ($User, $secpasswd)
Write-Warning "Switch to -Credentials [PSCredential] authentication method."
}
$URI = "$Server/Services/PageService.ashx/AddEventToSessions"
switch($Type){
'Support' {$Group = 'All Sessions'}
'Access' {$Group = 'All Machines'}
default {Write-Error "Unknown Type, $Type";return}
}
$SessionEventType = 43
if($GUID.count -eq 1){
$Body = ConvertTo-Json @($Group,@($GUID),$SessionEventType,'')
}
else {
$Body = ConvertTo-Json @($Group,$GUID,$SessionEventType,'')
}
Write-Verbose $Body
$encodedCredentials = [System.Convert]::ToBase64String([System.Text.Encoding]::ASCII.GetBytes("$($Credentials.UserName):$($Credentials.GetNetworkCredential().Password)"))
$Headers = @{ Authorization = "Basic $encodedCredentials" }
# Issue command
try {
$null = Invoke-RestMethod -Uri $URI -Method Post -Headers $Headers -ContentType "application/json" -Body $Body
}
catch {
Write-Error $_
return
}
}
function Get-CWCSessionDetail {
<#
.SYNOPSIS
Will return information about a session.
.DESCRIPTION
Dispays more information about a session.
.PARAMETER Server
The address to your Control server. Example 'https://control.labtechconsulting.com' or 'http://control.secure.me:8040'
.PARAMETER Credentials
[PSCredential] object used to authenticate against Control.
.PARAMETER User
User to authenticate against the Control server.
.PARAMETER Password
Password to authenticate against the Control server.
.PARAMETER Group
Name of session group to use.
.PARAMETER GUID
GUID of the machine to retreive session details.
.OUTPUTS
ConnectWise Control session objects
.NOTES
Version: 1.0
Author: Chris Taylor
Creation Date: 1/15/2019
Purpose/Change: Initial script development
.EXAMPLE
Get-CWCAccessSessions -Server $Server -User $User -Password $Password -Search "server1" -Limit 10
Will return the first 10 access sessions that match 'server1'.
#>
[CmdletBinding()]
param (
[Parameter(Mandatory=$True)]
[string]$Server,
[Parameter(Mandatory=$True, ParameterSetName='password')]
[string]$User,
[Parameter(Mandatory=$True, ParameterSetName='password')]
[string]$Password,
[string]$Group = "All Machines",
[Parameter(Mandatory=$True, ParameterSetName='cred')]
[PSCredential]$Credentials,
[Parameter(Mandatory=$True)]
[guid]$GUID
)
if($Password) {
$secpasswd = ConvertTo-SecureString $Password -AsPlainText -Force
$Credentials = New-Object System.Management.Automation.PSCredential ($User, $secpasswd)
Write-Warning "Switch to -Credentials [PSCredential] authentication method."
}
$URI = "$Server/Services/PageService.ashx/GetSessionDetails"
$Body = ConvertTo-Json @($Group,$GUID)
Write-Verbose $Body
$encodedCredentials = [System.Convert]::ToBase64String([System.Text.Encoding]::ASCII.GetBytes("$($Credentials.UserName):$($Credentials.GetNetworkCredential().Password)"))
$Headers = @{ Authorization = "Basic $encodedCredentials" }
try {
$Data = Invoke-RestMethod -Uri $URI -Method Post -Headers $Headers -ContentType "application/json; charset=utf-8" -Body $Body -Verbose
return $Data
}
catch {
Write-Error $_
return
}
}
function Update-CWCCustomProperty {
<#
.SYNOPSIS
Updated the custom .
.DESCRIPTION
Updates the name of a session on the control server.
.PARAMETER Server
The address to your Control server. Example 'https://control.labtechconsulting.com' or 'http://control.secure.me:8040'
.PARAMETER GUID
The GUID/SessionID for the machine you wish to connect to.
You can retrieve session info with the 'Get-CWCSessions' commandlet
On Windows clients, the launch parameters are located in the registry at:
HKEY_LOCAL_MACHINE\SYSTEM\CurrentControlSet\Services\ScreenConnect Client (xxxxxxxxxxxxxxxx)\ImagePath
On Linux and Mac clients, it's found in the ClientLaunchParameters.txt file in the client installation folder:
/opt/screenconnect-xxxxxxxxxxxxxxxx/ClientLaunchParameters.txt
.PARAMETER Credentials
[PSCredential] object used to authenticate against Control.
.PARAMETER User
User to authenticate against the Control server.
.PARAMETER Password
Password to authenticate against the Control server.
.PARAMETER NewName
The new name for the session.
.NOTES
Version: 1.1
Author: Chris Taylor
Creation Date: 10/25/2018
Purpose/Change: Initial script development
.EXAMPLE
Update-CWCSessionName -Server $Server -GUID $GUID -User $User -Password $Password -NewName 'Session1'
Will rename the session to Session1
#>
[CmdletBinding()]
param(
[Parameter(Mandatory=$True)]
[string]$Server,
[Parameter(Mandatory=$True)]
[guid]$GUID,
[Parameter(Mandatory=$True, ParameterSetName='password')]
[string]$User,
[Parameter(Mandatory=$True, ParameterSetName='password')]
[string]$Password,
[Parameter(Mandatory=$True)]
[string]$NewName,
[string]$Group = "All Machines",
[Parameter(Mandatory=$True, ParameterSetName='cred')]
[PSCredential]$Credentials
)
if($Password) {
$secpasswd = ConvertTo-SecureString $Password -AsPlainText -Force
$Credentials = New-Object System.Management.Automation.PSCredential ($User, $secpasswd)
Write-Warning "Switch to -Credentials [PSCredential] authentication method."
}
$Body = ConvertTo-Json @($Group,$GUID,$NewName)
Write-Verbose $Body
$encodedCredentials = [System.Convert]::ToBase64String([System.Text.Encoding]::ASCII.GetBytes("$($Credentials.UserName):$($Credentials.GetNetworkCredential().Password)"))
$Headers = @{ Authorization = "Basic $encodedCredentials" }
$URl = "$Server/Services/PageService.ashx/UpdateSessionName"
try {
$null = Invoke-RestMethod -Uri $url -Method Post -Headers $Headers -ContentType "application/json" -Body $Body
}
catch {
Write-Error $_
return
}
}
function New-CWCUser {
<#
.SYNOPSIS
Creates a user.
.DESCRIPTION
Creates a new local user.
.PARAMETER Server
The ConnectWise Control server you care connecting to
.PARAMETER Credentials
Credentials of the non MFA user used to perform the actions.
.PARAMETER UserName
Username of the new user
.PARAMETER Password
Password of the new user
.PARAMETER OPT
The MFA token
.PARAMETER DisplayName
The display name of the new user
.PARAMETER SecurityGroups
An array of security groups the user is a part of
.PARAMETER ForcePassChange
Force the user to change their password at next login
.NOTES
Version: 1.1
Author: Chris Taylor
Creation Date: 4/2/2020
Purpose/Change: Initial script development
.EXAMPLE
#>
[CmdletBinding()]
param(
[Parameter(Mandatory=$True)]
[string]$Server,
[Parameter(Mandatory=$True, ParameterSetName='cred')]
[PSCredential]$Credentials,
[Parameter(Mandatory=$True)]
[string]$UserName,
[Parameter(Mandatory=$True)]
[string]$Password,
[string]$OTP,
[string]$DisplayName,
[string]$Email,
[string[]]$SecurityGroups,
[boolean]$ForcePassChange = $true
)
$Body = ConvertTo-Json -Depth 10 @(
"XmlMembershipProvider",
$null,
$UserName,
$Password,
$Password,
$OTP,
$DisplayName,
"",
$Email,
$SecurityGroups,
$ForcePassChange
)
Write-Verbose $Body
$encodedCredentials = [System.Convert]::ToBase64String([System.Text.Encoding]::ASCII.GetBytes("$($Credentials.UserName):$($Credentials.GetNetworkCredential().Password)"))
$Headers = @{ Authorization = "Basic $encodedCredentials" }
$URl = "$Server/Services/SecurityService.ashx/SaveUser"
$null = Invoke-RestMethod -Uri $url -Method Post -Headers $Headers -ContentType "application/json" -Body $Body
}
function New-CWCAssignment {
<#
.SYNOPSIS
Assign a user to machines.
.DESCRIPTION
Uses the remote workforce extension to assign a user to machines
.PARAMETER Server
.PARAMETER GUID
.PARAMETER Credentials
.PARAMETER User
.PARAMETER Password
.PARAMETER NewName
.NOTES
Version: 1.1
Author: Chris Taylor
Creation Date: 10/25/2018
Purpose/Change: Initial script development
.EXAMPLE
#>
[CmdletBinding()]
param(
[Parameter(Mandatory=$True)]
[string]$Server,
[Parameter(Mandatory=$True, ParameterSetName='cred')]
[PSCredential]$Credentials,
[Parameter(Mandatory=$True)]
[guid[]]$GUID,
[Parameter(Mandatory=$True)]
[string]$Username,
[string]$DisplayName
)
$Body = ConvertTo-Json @(
$GUID,
"UserName:$($Username),UserDisplayName:$($DisplayName)"
)
Write-Verbose $Body
$encodedCredentials = [System.Convert]::ToBase64String([System.Text.Encoding]::ASCII.GetBytes("$($Credentials.UserName):$($Credentials.GetNetworkCredential().Password)"))
$Headers = @{ Authorization = "Basic $encodedCredentials" }
$URl = "$Server/App_Extensions/2c4f522f-b39a-413a-8807-dc52a2fce13e/Service.ashx/AddAssignmentNoteToSession"
$null = Invoke-RestMethod -Uri $url -Method Post -Headers $Headers -ContentType "application/json" -Body $Body
}
function createRequiredRemoteWorkforceRole {
<#
.SYNOPSIS
Create a new security role
.DESCRIPTION
Create a new security role
.PARAMETER Server
.PARAMETER GUID
.PARAMETER Credentials
.PARAMETER User
.PARAMETER Password
.PARAMETER NewName
.NOTES
Version: 1.1
Author: Chris Taylor
Creation Date: 10/25/2018