-
Notifications
You must be signed in to change notification settings - Fork 0
/
Automate-Module.ps1
2500 lines (2169 loc) · 125 KB
/
Automate-Module.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
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
These PowerShell Functions will Install, Push, Uninstall, and Confirm ConnectWise Automate installations.
.DESCRIPTION
Functions Included:
Confirm-Automate
Uninstall-Automate
Install-Automate
Push-Automate
Get-ADComputerNames
Install-Chrome
Install-Manage
Scan-Network
New-IPRange
http://powershell.com/cs/media/p/9437.aspx
Invoke-Ping
https://gallery.technet.microsoft.com/scriptcenter/Invoke-Ping-Test-in-b553242a
Get-IPv4Subnet
https://github.com/briansworth/GetIPv4Address/blob/master/GetIPv4Subnet.psm1
.LINK
https://github.com/Braingears/PowerShell
.NOTES
File Name : Automate-Module.psm1
Author : Chuck Fowler (Chuck@Braingears.com)
Version : 1.0
Creation Date : 11/10/2019
Purpose/Change : Initial script development
Prerequisite : PowerShell V2
Version : 1.1
Date : 11/15/2019
Changes : Add $Automate.InstFolder and $Automate.InstRegistry and check for both to be consdered for $Automate.Installed
It was found that the Automate Uninstaller EXE is leaving behind the LabTech registry keys and it was not being detected properly.
.EXAMPLE
Confirm-Automate [-Silent]
Confirm-Automate [-Show]
.EXAMPLE
Uninstall-Automate [-Silent]
.EXAMPLE
Install-Automate -Server 'YOURSERVER.DOMAIN.COM' -LocationID 2 [-Show]
.Example
To push a single Automate Agent:
Push-Automate -Computer 'ComputerName' -Server 'YOURSERVER.DOMAIN.COM' -LocationID 2 -Username 'DOMAIN\USERNAME' -Password 'Ch@ng3P@ssw0rd'
For multiple computers, use a | "pipe" into Push-Automate function:
$Computers | Push-Automate -Server 'YOURSERVER.DOMAIN.COM' -LocationID 2 -Username 'DOMAIN\USERNAME' -Password 'Ch@ng3P@ssw0rd'
- or -
Scan-Network | Push-Automate -Server 'YOURSERVER.DOMAIN.COM' -LocationID 2 -Username 'DOMAIN\USERNAME' -Password 'Ch@ng3P@ssw0rd'
- or -
Get-ADComputerNames | Push-Automate -Server 'YOURSERVER.DOMAIN.COM' -LocationID 2 -Username 'DOMAIN\USERNAME' -Password 'Ch@ng3P@ssw0rd'
- or -
"Computer1", "Computer2" | Push-Automate -Server 'YOURSERVER.DOMAIN.COM' -LocationID 2 -Username 'DOMAIN\USERNAME' -Password 'Ch@ng3P@ssw0rd'
#>
Function Confirm-Automate {
<#
.SYNOPSIS
This PowerShell Function will confirm If Automate is installed, services running, and checking-in.
.DESCRIPTION
This function will automatically start the Automate services (If stopped). It will collect Automate information from the registry.
.PARAMETER Raw
This will show the Automate registry entries
.PARAMETER Show
This will display $Automate object
.PARAMETER Silent
This will hide all output
.LINK
https://github.com/Braingears/PowerShell
.NOTES
Version : 1.0
Author : Chuck Fowler
Creation Date : 08/16/2019
Purpose/Change : Initial script development
Version : 1.1
Date : 11/15/2019
Changes : Add $Automate.InstFolder and $Automate.InstRegistry and check for both to be consdered for $Automate.Installed
It was found that the Automate Uninstaller EXE is leaving behind the LabTech registry keys and it was not being detected properly.
Version : 1.2
Date : 04/02/2020
Changes : Add $Automate.Service -eq $null
If the service still exists, the installation is failing with Exit Code 1638.
.EXAMPLE
Confirm-Automate [-Silent]
Confirm-Automate [-Show]
ServerAddress : https://yourserver.hostedrmm.com
ComputerID : 321
ClientID : 1
LocationID : 2
Version : 190.221
Service : Running
Online : True
LastHeartbeat : 29
LastStatus : 36
$Automate
$Global:Automate
This output will be saved to $Automate as an object to be used in other functions.
#>
[CmdletBinding(SupportsShouldProcess=$True)]
Param (
[switch]$Raw = $False,
[switch]$Show = $False,
[switch]$Silent = $False
)
$ErrorActionPreference = 'SilentlyContinue'
if ((Get-ItemProperty "HKLM:\SOFTWARE\LabTech\Service").LastSuccessStatus) {
$Online = If ((Test-Path "HKLM:\SOFTWARE\LabTech\Service") -and ((Get-Service ltservice).status) -eq "Running") {((((Get-Date) - (Get-Date (Get-ItemProperty "HKLM:\SOFTWARE\LabTech\Service").LastSuccessStatus)).TotalSeconds) -lt 600)} Else {Write $False}
} else {$Online = $False}
If (Test-Path "HKLM:\SOFTWARE\LabTech\Service") {
$Global:Automate = New-Object -TypeName psobject
$Global:Automate | Add-Member -MemberType NoteProperty -Name ComputerName -Value $env:ComputerName
$Global:Automate | Add-Member -MemberType NoteProperty -Name ServerAddress -Value ((Get-ItemProperty "HKLM:\SOFTWARE\LabTech\Service").'Server Address')
$Global:Automate | Add-Member -MemberType NoteProperty -Name ComputerID -Value ((Get-ItemProperty "HKLM:\SOFTWARE\LabTech\Service").ID)
$Global:Automate | Add-Member -MemberType NoteProperty -Name ClientID -Value ((Get-ItemProperty "HKLM:\SOFTWARE\LabTech\Service").ClientID)
$Global:Automate | Add-Member -MemberType NoteProperty -Name LocationID -Value ((Get-ItemProperty "HKLM:\SOFTWARE\LabTech\Service").LocationID)
$Global:Automate | Add-Member -MemberType NoteProperty -Name Version -Value ((Get-ItemProperty "HKLM:\SOFTWARE\LabTech\Service").Version)
$Global:Automate | Add-Member -MemberType NoteProperty -Name InstFolder -Value (Test-Path "$($env:windir)\ltsvc")
$Global:Automate | Add-Member -MemberType NoteProperty -Name InstRegistry -Value $True
$Global:Automate | Add-Member -MemberType NoteProperty -Name Installed -Value (Test-Path "$($env:windir)\ltsvc")
$Global:Automate | Add-Member -MemberType NoteProperty -Name Service -Value ((Get-Service LTService).Status)
$Global:Automate | Add-Member -MemberType NoteProperty -Name Online -Value $Online
if ((Get-ItemProperty "HKLM:\SOFTWARE\LabTech\Service").HeartbeatLastSent) {
$Global:Automate | Add-Member -MemberType NoteProperty -Name LastHeartbeat -Value ([int]((Get-Date) - (Get-Date (Get-ItemProperty "HKLM:\SOFTWARE\LabTech\Service").HeartbeatLastSent)).TotalSeconds)
}
if ((Get-ItemProperty "HKLM:\SOFTWARE\LabTech\Service").LastSuccessStatus) {
$Global:Automate | Add-Member -MemberType NoteProperty -Name LastStatus -Value ([int]((Get-Date) - (Get-Date (Get-ItemProperty "HKLM:\SOFTWARE\LabTech\Service").LastSuccessStatus)).TotalSeconds)
}
Write-Verbose $Global:Automate
If ($Show) {
$Global:Automate
} Else {
If (!$Silent) {
Write "Server Address checking-in to $($Global:Automate.ServerAddress)"
Write "ComputerID: $($Global:Automate.ComputerID)"
Write "The Automate Agent Online $($Global:Automate.Online)"
Write "Last Successful Heartbeat $($Global:Automate.LastHeartbeat) seconds"
Write "Last Successful Status Update $($Global:Automate.LastStatus) seconds"
} # End Not Silent
} # End If
If ($Raw -eq $True) {Get-ItemProperty "HKLM:\SOFTWARE\LabTech\Service"}
} Else {
$Global:Automate = New-Object -TypeName psobject
$Global:Automate | Add-Member -MemberType NoteProperty -Name ComputerName -Value $env:ComputerName
$Global:Automate | Add-Member -MemberType NoteProperty -Name InstFolder -Value (Test-Path "$($env:windir)\ltsvc")
$Global:Automate | Add-Member -MemberType NoteProperty -Name InstRegistry -Value $False
$Global:Automate | Add-Member -MemberType NoteProperty -Name Installed -Value ((Test-Path "$($env:windir)\ltsvc") -and (Test-Path "HKLM:\SOFTWARE\LabTech\Service"))
$Global:Automate | Add-Member -MemberType NoteProperty -Name Service -Value ((Get-Service ltservice ).status)
$Global:Automate | Add-Member -MemberType NoteProperty -Name Online -Value $Online
Write-Verbose $Global:Automate
} #End If Registry Exists
If (!$Global:Automate.InstFolder -and !$Global:Automate.InstRegistry -and ($Global:Automate.Service -eq $Null)) {If ($Silent -eq $False) {Write "Automate is NOT Installed"}}
} #End Function Confirm-Automate
########################
Set-Alias -Name LTC -Value Confirm-Automate -Description 'Confirm If Automate is running properly'
########################
Function Uninstall-Automate {
<#
.SYNOPSIS
This PowerShell Function Uninstall Automate.
.DESCRIPTION
This function will download the Automate Uninstaller from Connectwise and completely remove the Automate / LabTech Agent.
.PARAMETER Silent
This will hide all output
.LINK
https://github.com/Braingears/PowerShell
.NOTES
Version : 1.0
Author : Chuck Fowler
Website : braingears.com
Creation Date : 8/2019
Purpose : Create initial function script
Version : 1.1
Date : 11/15/2019
Changes : Add $Automate.InstFolder and $Automate.InstRegistry and check for both to be consdered for $Automate.Installed
It was found that the Automate Uninstaller EXE is leaving behind the LabTech registry keys and it was not being detected properly.
If the LTSVC Folder or Registry keys are found after the uninstaller runs, the script now performs a manual gutting via PowerShell.
Version : 1.2
Date : 04/02/2020
Changes : Add $Automate.Service -eq $null
If the service still exists, the installation is failing with Exit Code 1638.
.EXAMPLE
Uninstall-Automate [-Silent]
#>
[CmdletBinding(SupportsShouldProcess=$True)]
Param (
[switch]$Force,
[switch]$Raw,
[switch]$Show,
[switch]$Silent = $False
)
$ErrorActionPreference = 'SilentlyContinue'
$Verbose = If ($PSBoundParameters.Verbose -eq $True) { $True } Else { $False }
$DownloadPath = "https://s3.amazonaws.com/assets-cp/assets/Agent_Uninstall.exe"
If ((([Int][System.Environment]::OSVersion.Version.Build) -gt 6000) -and ((get-host).Version.ToString() -ge 3)) {
$DownloadPath = "https://s3.amazonaws.com/assets-cp/assets/Agent_Uninstall.exe"
} Else {
$DownloadPath = "http://s3.amazonaws.com/assets-cp/assets/Agent_Uninstall.exe"
}
$SoftwarePath = "C:\Support\Automate"
$UninstallApps = @(
"ConnectWise Automate Remote Agent"
"LabTech® Software Remote Agent"
)
Write-Debug "Checking if Automate Installed"
Confirm-Automate -Silent -Verbose:$Verbose
If (($Global:Automate.InstFolder) -or ($Global:Automate.InstRegistry) -or (!($Global:Automate.Service -eq $Null)) -or ($Force)) {
$Filename = [System.IO.Path]::GetFileName($DownloadPath)
$SoftwareFullPath = "$($SoftwarePath)\$Filename"
If (!(Test-Path $SoftwarePath)) {md $SoftwarePath | Out-Null}
Set-Location $SoftwarePath
If ((Test-Path $SoftwareFullPath)) {Remove-Item $SoftwareFullPath | Out-Null}
$WebClient = New-Object System.Net.WebClient
$WebClient.DownloadFile($DownloadPath, $SoftwareFullPath)
If (!$Silent) {Write-Host "Removing Existing Automate Agent..."}
Write-Verbose "Closing Open Applications and Stopping Services"
Stop-Process -Name "ltsvcmon","lttray","ltsvc","ltclient" -Force
Stop-Service ltservice,ltsvcmon -Force
$UninstallExitCode = (Start-Process "cmd" -ArgumentList "/c $($SoftwareFullPath)" -NoNewWindow -Wait -PassThru).ExitCode
If (!$Silent) {
If ($UninstallExitCode -eq 0) {
# Write-Host "The Automate Agent Uninstaller Executed Without Errors" -ForegroundColor Green
Write-Verbose "The Automate Agent Uninstaller Executed Without Errors"
} Else {
Write-Host "Automate Uninstall Exit Code: $($UninstallExitCode)" -ForegroundColor Red
Write-Verbose "Automate Uninstall Exit Code: $($UninstallExitCode)"
}
}
Write-Verbose "Checking For Removal - Loop 5X"
While ($Counter -ne 6) {
$Counter++
Start-Sleep 10
Confirm-Automate -Silent -Verbose:$Verbose
If ((!$Global:Automate.InstFolder) -and (!$Global:Automate.InstRegistry) -and ($Global:Automate.Service -eq $Null)) {
Write-Verbose "Automate Uninstaller Completed Successfully"
Break
}
}# end While
If (($Global:Automate.InstFolder) -or ($Global:Automate.InstRegistry) -or (!($Global:Automate.Service -eq $Null))) {
Write-Verbose "Uninstaller Failed"
Write-Verbose "Manually Gutting Automate..."
If (!(($Global:Automate.Service -eq $Null) -or ($Global:Automate.Service -eq "Stopped"))) {
Write-Verbose "LTService Service not Stopped. Disabling LTService Service"
Set-Service ltservice -StartupType Disabled
Stop-Service ltservice,ltsvcmon -Force
}
Stop-Process -Name "ltsvcmon","lttray","ltsvc","ltclient" -Force
Write-Verbose "Uninstalling LabTechAD Package"
$UninstallApps2 = foreach ($App in $UninstallApps) {Get-WmiObject -Class Win32_Product -ComputerName . | Where-Object -FilterScript {$_.Name -like $App} | Select-Object -ExpandProperty "Name"}
$UninstallAppsFound = $UninstallApps2 | Select-Object -Unique
foreach ($App in $UninstallAppsFound) {
$AppLocalPackage = Get-WmiObject -Class Win32_Product -ComputerName . | Where-Object -FilterScript {$_.Name -like $App} | Select-Object -ExpandProperty "LocalPackage"
If ($AppLocalPackage -eq $null) {
Write-Verbose "$($App) - Not Installed"
} Else {
Write-Verbose "Uninstalling: $($App) - msiexec /x $($AppLocalPackage) /qn /norestart"
msiexec /x $AppLocalPackage /qn /norestart
}
}
Remove-Item "$($env:windir)\ltsvc" -Recurse -Force
Get-ItemProperty "HKLM:\SOFTWARE\LabTech\Service" | Remove-Item -Recurse -Force
REG Delete HKLM\SOFTWARE\LabTech\Service /f | Out-Null
Start-Process "cmd" -ArgumentList "/c $($SoftwareFullPath)" -NoNewWindow -Wait -PassThru | Out-Null
Confirm-Automate -Silent -Verbose:$Verbose
If ($Global:Automate.InstFolder) {
If (!$Silent) {
Write-Host "Automate Uninstall Failed" -ForegroundColor Red
Write-Host "$($env:windir)\ltsvc folder still exists" -ForegroundColor Red
} else {
Write-Verbose "Automate Uninstall Failed"
Write-Verbose "$($env:windir)\ltsvc folder still exists"
}
}
If ($Global:Automate.InstRegistry) {
If (!$Silent) {
Write-Host "Automate Uninstall Failed" -ForegroundColor Red
Write-Host "HKLM:\SOFTWARE\LabTech\Service Registry keys still exists" -ForegroundColor Red
} else {
Write-Verbose "Automate Uninstall Failed"
Write-Verbose "HKLM:\SOFTWARE\LabTech\Service Registry keys still exists"
}
}
If (!($Global:Automate.Service -eq $Null)) {
If (!$Silent) {
Write-Host "Automate Uninstall Failed" -ForegroundColor Red
Write-Host "LTService Service still exists" -ForegroundColor Red
} else {
Write-Verbose "Automate Uninstall Failed"
Write-Verbose "LTService Service still exists"
}
}
} Else {
If (!$Silent) {Write-Host "The Automate Agent Uninstalled Successfully" -ForegroundColor Green}
Write-Verbose "The Automate Agent Uninstalled Successfully"
}
} # If Test Install
Confirm-Automate -Silent:$Silent
} # Function Uninstall-Automate
########################
Set-Alias -Name LTU -Value Uninstall-Automate -Description 'Uninstall Automate Agent'
########################
Function Install-Automate {
<#
.SYNOPSIS
This PowerShell Function is for Automate Deployments
.DESCRIPTION
Install the Automate Agent.
This function will qualIfy the If another Autoamte agent is already
installed on the computer. If the existing agent belongs to dIfferent
Automate server, it will automatically "Rip & Replace" the existing
agent. This comparison is based on the server's FQDN.
This function will also verIfy If the existing Automate agent is
checking-in. The Confirm-Automate Function will verIfy the Server
address, LocationID, and Heartbeat/Check-in. If these entries are
missing or not checking-in properly; this function will automatically
attempt to restart the services, and then "Rip & Replace" the agent to
remediate the agent.
$Automate
$Global:Automate
The output will be saved to $Automate as an object to be used in other functions.
Example:
Install-Automate -Server YOURSERVER.DOMAIN.COM -LocationID 2 -Transcript
Tested OS: Windows XP (with .Net 3.5.1 and PowerShell installed)
Windows Vista
Windows 7
Windows 8
Windows 10
Windows 2003R2
Windows 2008R2
Windows 2012R2
Windows 2016
Windows 2019
.PARAMETER Server
This is the URL to your Automate server.
Install-Automate -Server 'server.hostedrmm.com' -LocationID 2
.PARAMETER LocationID
Use LocationID to install the Automate Agent directly to the appropieate client's location / site.
If parameter is not specIfied, it will automatically assign LocationID 1 (New Computers).
.PARAMETER Token
Use Token to install the Automate Agent directly to the appropieate client's location / site.
If parameter is not specIfied, it will automatically attempt to use direct unauthenticated downloads.
This method in blocked after Automate v20.0.6.178 (Patch 6)
.PARAMETER Force
This will force the Automate Uninstaller prior to installation.
Essentually, this will be a fresh install and a fresh check-in to the Automate server.
Install-Automate -Server 'server.hostedrmm.com' -LocationID 2 -Force
.PARAMETER Silent
This will hide all output (except a failed installation when Exit Code -ne 0)
The function will exit once the installer has completed.
Install-Automate -Server 'server.hostedrmm.com' -LocationID 2 -Silent
.PARAMETER Transcript
This parameter will save the entire transcript and responsed to:
$($env:windir)\Temp\AutomateLogon.txt
Install-Automate -Server 'server.hostedrmm.com' -LocationID 2 -Transcript -Verbose
.LINK
https://github.com/Braingears/PowerShell
.NOTES
Version : 1.0
Author : Chuck Fowler
Creation Date : 08/2019
Purpose/Change : Initial script development
Version : 1.1
Date : 11/15/2019
Changes : Add $Automate.InstFolder and $Automate.InstRegistry and check for both to be consdered for $Automate.Installed
It was found that the Automate Uninstaller EXE is leaving behind the LabTech registry keys and it was not being detected properly.
If the LTSVC Folder or Registry keys are found after the uninstaller runs, the script now performs a manual gutting via PowerShell.
Version : 1.2
Date : 02/17/2020
Changes : Add MSIEXEC Log Files to C:\Windows\Temp\Automate_Agent_(Date).log
Version : 1.3
Date : 05/26/2020
Changes : Look for and replace "Enter the server address here" with the actual Automate Server address.
Version : 1.4
Date : 06/29/2020
Changes : Added Token Parameter for Deployment
.EXAMPLE
Install-Automate -Server 'automate.domain.com' -LocationID 42 -Token adb68881994ed93960346478303476f4
This will install the LabTech agent using the provided Server URL, and LocationID.
#>
[CmdletBinding(SupportsShouldProcess=$True)]
Param(
[Parameter(ValueFromPipelineByPropertyName = $True, Position=0)]
[Alias("FQDN","Srv")]
[string[]]$Server = $Null,
[Parameter(ValueFromPipelineByPropertyName = $True, Position=1)]
[AllowNull()]
[Alias('LID','Location')]
[int]$LocationID = '1',
[Parameter(ValueFromPipelineByPropertyName = $True, Position=2)]
[Alias("InstallerToken")]
[string[]]$Token = $Null,
[switch]$Force,
[Parameter()]
[AllowNull()]
[switch]$Show = $False,
[switch]$Silent,
[Parameter()]
[AllowNull()]
[switch]$Transcript = $False
)
$ErrorActionPreference = 'SilentlyContinue'
$Verbose = If ($PSBoundParameters.Verbose -eq $True) { $True } Else { $False }
$Error.Clear()
If ($Transcript) {Start-Transcript -Path "$($env:windir)\Temp\Automate_Deploy.txt" -Force}
Write-Verbose "Checking Operating System (WinXP and Older) for HTTP vs HTTPS"
If ((([Int][System.Environment]::OSVersion.Version.Build) -gt 6000) -and ((get-host).Version.ToString() -ge 3)) {$AutomateURL = "https://$($Server)"} Else {$AutomateURL = "http://$($Server)"}
$SoftwarePath = "C:\Support\Automate"
$Filename = "Automate_Agent.msi"
$SoftwareFullPath = "$SoftwarePath\$Filename"
$DownloadPath = $null
If ($Token -ne $null) {
$DownloadPath = "$($AutomateURL)/Labtech/Deployment.aspx?InstallerToken=$Token"
Write-Verbose "DownloadPathToken: $($DownloadPath)"
}
If ($DownloadPath -eq $null) {
$DownloadPath = "$($AutomateURL)/Labtech/Deployment.aspx?Probe=1&installType=msi&MSILocations=$($LocationID)"
Write-Host "The -Token Parameters Was Not Entered" -ForegroundColor Red
Write-Verbose "DownloadPathOld: $($DownloadPath)"
}
Write-Verbose "Downloading from $($DownloadPath)"
Write-Verbose "Checking if Automate Server URL is active. Server entered: $($Server)"
Try {
If ((get-host).Version.ToString() -ge 3 -and (!$Installer)) {
$TestURL = (New-Object Net.WebClient).DownloadString($DownloadPath)
Write-Verbose "$AutomateURL is Active"
}
}
Catch {
Write-Host "The Automate Server or Token Parameters Was Not Entered or Inaccessible. Failed to Download:" -ForegroundColor Red
Write-Host "$($DownloadPath)" -ForegroundColor Red
Write-Host "Help: Get-Help Install-Automate -Full"
Write-Host " "
Confirm-Automate -Show
Break
}
Confirm-Automate -Silent -Verbose:$Verbose
Write-Verbose "If ServerAddress matches, the Automate Agent is currently Online, and Not forced to Rip & Replace then Automate is already installed."
Write-Verbose (($Global:Automate.ServerAddress -like "*$($Server)*") -and ($Global:Automate.Online) -and !($Force))
If (($Global:Automate.ServerAddress -like "*$($Server)*") -and $Global:Automate.Online -and !$Force) {
If (!$Silent) {
If ($Show) {
$Global:Automate
} Else {
Write-Host "The Automate Agent is already installed on $($Global:Automate.Computername) ($($Global:Automate.ComputerID)) and checked-in $($Global:Automate.LastStatus) seconds ago to $($Global:Automate.ServerAddress)" -ForegroundColor Green
}
}
} Else {
If (!$Silent -and $Global:Automate.Online -and (!($Global:Automate.ServerAddress -like "*$($Server)*"))) {
Write-Host "The Existing Automate Server Does Not Match The Target Automate Server." -ForegroundColor Red
Write-Host "Current Automate Server: $($Global:Automate.ServerAddress)" -ForegroundColor Red
Write-Host "New Automate Server: $($AutomateURL)" -ForegroundColor Green
} # If Different Server
Write-Verbose "Downloading Automate Agent from $($AutomateURL)"
If (!(Test-Path $SoftwarePath)) {md $SoftwarePath | Out-Null}
Set-Location $SoftwarePath
If ((test-path $SoftwareFullPath)) {Remove-Item $SoftwareFullPath | Out-Null}
Try {
Write-Verbose "Downloading from: $($DownloadPath)"
Write-Verbose "Downloading to: $($SoftwareFullPath)"
$WebClient = New-Object System.Net.WebClient
$WebClient.DownloadFile($DownloadPath, $SoftwareFullPath)
Write-Verbose "Download Complete"
}
Catch {
Write-Host "The Automate Server or Token Parameters Was Not Entered or Inaccessible" -ForegroundColor Red
Write-Host "Exiting Installation..."
Break
}
Write-Verbose "Removing Existing Automate Agent"
Uninstall-Automate -Force:$Force -Silent:$Silent -Verbose:$Verbose
If (!$Silent) {Write-Host "Installing Automate Agent to $AutomateURL"}
Stop-Process -Name "ltsvcmon","lttray","ltsvc","ltclient" -Force -PassThru
$Date = (get-date -UFormat %Y-%m-%d_%H-%M-%S)
$LogFullPath = "$env:windir\Temp\Automate_Agent_$Date.log"
$InstallExitCode = (Start-Process "msiexec.exe" -ArgumentList "/i $($SoftwareFullPath) /quiet /norestart LOCATION=$($LocationID) SERVERADDRESS=$($AutomateURL) /L*V $($LogFullPath)" -NoNewWindow -Wait -PassThru).ExitCode
Write-Verbose "MSIEXEC Log Files: $LogFullPath"
If ($InstallExitCode -eq 0) {
If (!$Silent) {Write-Verbose "The Automate Agent Installer Executed Without Errors"}
} Else {
Write-Host "Automate Installer Exit Code: $InstallExitCode" -ForegroundColor Red
Write-Host "Automate Installer Logs: $LogFullPath" -ForegroundColor Red
Write-Host "The Automate MSI failed. Waiting 15 Seconds..." -ForegroundColor Red
Start-Sleep -s 15
Write-Host "Installer will execute twice (KI 12002617)" -ForegroundColor Yellow
$Date = (get-date -UFormat %Y-%m-%d_%H-%M-%S)
$LogFullPath = "$env:windir\Temp\Automate_Agent_$Date.log"
$InstallExitCode = (Start-Process "msiexec.exe" -ArgumentList "/i $($SoftwareFullPath) /quiet /norestart LOCATION=$($LocationID) SERVERADDRESS=$($AutomateURL) /L*V $($LogFullPath)" -NoNewWindow -Wait -PassThru).ExitCode
Write-Host "Automate Installer Exit Code: $InstallExitCode" -ForegroundColor Yellow
Write-Host "Automate Installer Logs: $LogFullPath" -ForegroundColor Yellow
}# End Else
If ($InstallExitCode -eq 0) {
While ($Counter -ne 30) {
$Counter++
Start-Sleep 10
Confirm-Automate -Silent -Verbose:$Verbose
If ($Global:Automate.Server -like "Enter the server address here*") {
Write-Verbose "The Automate Server Address was not written properly"
Write-Verbose "Manually overwriting the Server Address to: $($AutomateURL)"
Set-ItemProperty -Path HKLM:\SOFTWARE\LabTech\Service 'Server Address' -Value $AutomateURL –Force
Write-Verbose "Restarting LTService after correcting the Server Address"
Get-Service LTService | Where {$_.Status -eq "Running"} | Restart-Service -Force
Confirm-Automate -Silent -Verbose:$Verbose
}
If ($Global:Automate.Online -and $Global:Automate.ComputerID -ne $Null) {
If (!$Silent) {
Write-Host "The Automate Agent Has Been Successfully Installed" -ForegroundColor Green
$Global:Automate
}#end If Silent
Break
} # end If
}# end While
} Else {
While ($Counter -ne 3) {
$Counter++
Start-Sleep 10
Confirm-Automate -Silent -Verbose:$Verbose
If ($Global:Automate.Server -like "Enter the server address here*") {
Write-Verbose "The Automate Server Address was not written properly"
Write-Verbose "Manually overwriting the Server Address to: $($AutomateURL)"
Set-ItemProperty -Path HKLM:\SOFTWARE\LabTech\Service 'Server Address' -Value $AutomateURL –Force
Write-Verbose "Restarting LTService after correcting the Server Address"
Get-Service LTService | Where {$_.Status -eq "Running"} | Restart-Service -Force
Confirm-Automate -Silent -Verbose:$Verbose
}
If ($Global:Automate.Online -and $Global:Automate.ComputerID -ne $Null) {
If (!$Silent) {
Write-Host "The Automate Agent Has Been Successfully Installed" -ForegroundColor Green
$Global:Automate
}#end If Silent
Break
} # end If
} # end While
} # end If ExitCode 0
Confirm-Automate -Silent -Verbose:$Verbose
If (!($Global:Automate.Online -and $Global:Automate.ComputerID -ne $Null)) {
If (!$Silent) {
Write-Host "The Automate Agent FAILED to Install" -ForegroundColor Red
$Global:Automate
}# end If Silent
} # end If Not Online
} # End
If ($Transcript) {Stop-Transcript}
} #End Function Install-Automate
########################
Set-Alias -Name LTI -Value Install-Automate -Description 'Install Automate Agent'
########################
Function Push-Automate
{
<#
.SYNOPSIS
This PowerShell Function is for pushing Automate Deployments
.DESCRIPTION
Install the Automate Agent.
This function will qualIfy the If another Autoamte agent is already
installed on the computer. If the existing agent belongs to dIfferent
Automate server, it will automatically "Rip & Replace" the existing
agent. This comparison is based on the server's FQDN.
This function will also verIfy If the existing Automate agent is
checking-in. The Confirm-Automate Function will verIfy the Server
address, LocationID, and Heartbeat/Check-in. If these entries are
missing or not checking-in properly; this function will automatically
attempt to restart the services, and then "Rip & Replace" the agent to
remediate the agent.
$AutoResults
$Global:AutoResults
The output will be saved to $AutoResults as an object to be used in other functions.
Example:
To push a single Automate Agent:
Push-Automate -Computer 'Computername' -Server 'YOURSERVER.DOMAIN.COM' -LocationID 2 -Token adb68881994ed93960346478303476f4 -Username 'DOMAIN\USERNAME' -Password 'Ch@ng3P@ssw0rd'
For multiple computers, use a | "pipe" into Push-Automate function:
$Computers | Push-Automate -Server 'YOURSERVER.DOMAIN.COM' -LocationID 2 -Token adb68881994ed93960346478303476f4 -Username 'DOMAIN\USERNAME' -Password 'Ch@ng3P@ssw0rd'
- or -
Get-ADComputerNames | Push-Automate -Server 'YOURSERVER.DOMAIN.COM' -LocationID 2 -Token adb68881994ed93960346478303476f4 -Username 'DOMAIN\USERNAME' -Password 'Ch@ng3P@ssw0rd'
- or -
"Computer1", "Computer2" | Push-Automate -Server 'YOURSERVER.DOMAIN.COM' -LocationID 2 -Token adb68881994ed93960346478303476f4 -Username 'DOMAIN\USERNAME' -Password 'Ch@ng3P@ssw0rd'
When pushing to multiple computers, use the actual computer names. If you use IP Address, it will fail when using WINRM Protocols (and use WMI/RCP instead).
.PARAMETER Server
This is the URL to your Automate server.
Install-Automate -Server 'server.hostedrmm.com' -LocationID 2
.PARAMETER LocationID
Use LocationID to install the Automate Agent directly to the appropieate client's location / site.
If parameter is not specIfied, it will automatically assign LocationID 1 (New Computers).
Install-Automate -Server 'server.hostedrmm.com' -LocationID 2 -Token adb68881994ed93960346478303476f4
.PARAMETER Username
Enter username with Domain Admin rights. When entering username, use 'DOMAIN\USERNAME'
The function will accept PSCredentials saved to $Credentials prior to running this function.
.PARAMETER Password
Enter Password for Domain Admin account.
The function will accept PSCredentials saved to $Credentials prior to running this function.
.PARAMETER Force
>>> This Function Is Currently Disabled <<<
This will force the Automate Uninstaller prior to installation.
Essentually, this will be a fresh install and a fresh check-in to the Automate server.
Install-Automate -Server 'server.hostedrmm.com' -LocationID 2 -Token adb68881994ed93960346478303476f4 -Force
.PARAMETER Silent
>>> This Function Is Currently Disabled <<<
This will hide all output (except a failed installation when Exit Code -ne 0)
The function will exit once the installer has completed.
Install-Automate -Server 'server.hostedrmm.com' -LocationID 2 -Token adb68881994ed93960346478303476f4 -Silent
.PARAMETER Transcript
>>> This Function Is Currently Disabled <<<
This parameter will save the entire transcript and responsed to:
$($env:windir)\Temp\AutomateLogon.txt
Install-Automate -Server 'server.hostedrmm.com' -LocationID 2 -Token adb68881994ed93960346478303476f4 -Transcript -Verbose
.LINK
https://github.com/Braingears/PowerShell
.NOTES
Version : 1.0
Author : Chuck Fowler
Creation Date : 08/2019
Purpose/Change : Initial script development
Version : 1.1
Date : 11/15/2019
Changes : Add $Automate.InstFolder and $Automate.InstRegistry and check for both to be consdered for $Automate.Installed
It was found that the Automate Uninstaller EXE is leaving behind the LabTech registry keys and it was not being detected properly.
If the LTSVC Folder or Registry keys are found after the uninstaller runs, the script now performs a manual gutting via PowerShell.
.EXAMPLE
Push-Automate -Server 'YOURSERVER.DOMAIN.COM' -LocationID 2 -Username 'DOMAIN\USERNAME' -Password 'Ch@ng3P@ssw0rd' -Token adb68881994ed93960346478303476f4 -Computer COMPUTERNAME
Use the -Computer parameter for single computers.
.EXAMPLE
$Computers | Push-Automate -Server 'YOURSERVER.DOMAIN.COM' -LocationID 2 -Username 'DOMAIN\USERNAME' -Password 'Ch@ng3P@ssw0rd' -Token adb68881994ed93960346478303476f4
Use Array to pipe multiple computers into Push=Automate function.
.EXAMPLE
Get-ADComputerNames | Push-Automate -Server 'YOURSERVER.DOMAIN.COM' -LocationID 2 -Username 'DOMAIN\USERNAME' -Password 'Ch@ng3P@ssw0rd' -Token adb68881994ed93960346478303476f4
Use another function to pipe multiple computers into Push=Automate function. Select only computer names.
.EXAMPLE
"Computer1", "Computer2" | Push-Automate -Server 'YOURSERVER.DOMAIN.COM' -LocationID 2 -Username 'DOMAIN\USERNAME' -Password 'Ch@ng3P@ssw0rd' -Token adb68881994ed93960346478303476f4
When pushing to multiple computers, use the actual computer names. If you use IP Address, it will fail when using WINRM Protocols (and use WMI/RCP instead).
This will install the LabTech agent using the provided Server URL, and LocationID.
.EXAMPLE
$Credential = Get-Credential
Push-Automate -Server 'YOURSERVER.DOMAIN.COM' -LocationID 2 -Token adb68881994ed93960346478303476f4
You can proactivly load PSCredential, then use the Push-Automate function within the same Powershell session.
#>
[CmdletBinding()]
Param
(
[Parameter(ValueFromPipeline=$True)]
[string[]]$Computer = $env:COMPUTERNAME,
[Parameter()]
[Alias("FQDN","Srv")]
[string[]]$Server = $Null,
[Parameter()]
[AllowNull()]
[Alias('LID','Location')]
[int]$LocationID = '1',
[Parameter()]
[Alias("InstallerToken")]
[string[]]$Token = $Null,
[Parameter()]
[AllowNull()]
[Alias('User')]
[string[]]$Username,
[Parameter()]
[AllowNull()]
[Alias('Pass')]
[string[]]$Password,
[Parameter()]
[AllowNull()]
[switch]$Force = $False,
[Parameter()]
[AllowNull()]
[switch]$Show = $False,
[Parameter()]
[AllowNull()]
[switch]$Silent = $False,
[Parameter()]
[AllowNull()]
[switch]$Transcript = $False
)
BEGIN
{
$ErrorActionPreference = "SilentlyContinue"
$Verbose = If ($PSBoundParameters.Verbose -eq $True) { $True } Else { $False }
If ((([Int][System.Environment]::OSVersion.Version.Build) -gt 6000) -and ((get-host).Version.ToString() -ge 3)) {$AutomateURL = "https://" + $Server} Else {$AutomateURL = "http://" + $Server}
$AutomateURLTest = $AutomateURL +"/LabTech/"
Write-Verbose "Checking if Automate Server URL is active. Server entered: $($Server)"
Write-Verbose "$AutomateURLTest"
Try {
$TestURL = (New-Object Net.WebClient).DownloadString($AutomateURLTest)
Write-Verbose "$($AutomateURL) is Active"
}
Catch {
Write-Host "The Automate Server Parameter Was Not Entered or Inaccessible" -ForegroundColor Red
Write-Host "Help: Get-Help Push-Automate -Full"
Break
}
$Whoami = whoami
Write-Verbose "Running Script as: $whoami"
If (($Username -eq $Null) -and ($Password -eq $Null) -and ($Credential -eq $Null) -and !((whoami) -eq 'nt authority\system'))
{$Credential = Get-Credential -Message "Enter Domain Admin Credentials for Remote Automate Push"}
If (($Username -ne $Null) -and ($Password -ne $Null)) {
$Pass = $Password | ConvertTo-SecureString -asPlainText -Force
$Credential = New-Object System.Management.Automation.PSCredential($Username,$Pass)
}
If ($Credential -eq $Null) {
If ((whoami) -eq 'nt authority\system') {Write-Host "Running function as $($Whoami)"}
Write-Host "Credentials Are Missing!" -ForegroundColor Red
Clear-Variable Computer, Server, Force, Silent
Break
}
Write-Verbose "Credential loaded: $($Credential.Username)"
$Global:AutoChecks = @()
} #End Begin
PROCESS
{
# Variables
$Time = Date
$CheckAutomateWinRM = {
Write-Verbose "Invoke Confirm-Automate -Silent"
Invoke-Expression(New-Object Net.WebClient).DownloadString('https://raw.githubusercontent.com/Braingears/PowerShell/master/Automate-Module.psm1')
Confirm-Automate -Silent
Write $Global:Automate
}
$InstallAutomateWinRM = {
$Server = $Args[0]
$LocationID = $Args[1]
$Token = $Args[2]
$Force = $Args[3]
$Silent = $Args[4]
$Transcript = $Args[5]
Invoke-Expression(New-Object Net.WebClient).DownloadString('https://raw.githubusercontent.com/Braingears/PowerShell/master/Automate-Module.psm1')
Install-Automate -Server $Server -LocationID $LocationID -Token $Token -Transcript
}
$WMICMD = 'powershell.exe -Command "Invoke-Expression(New-Object Net.WebClient).DownloadString(''https://raw.githubusercontent.com/Braingears/PowerShell/master/Automate-Module.psm1''); '
$WMIPOSH = "Install-Automate -Server $Server -LocationID $LocationID -Token $Token -Transcript"
$WMIArg = Write-Output "$WMICMD$WMIPOSH"""
$WinRMConectivity = "N/A"
$WMICConectivity = "N/A"
$WinRMDeployed = $False
$WMIDeployed = $False
Clear-Variable Automate, ProcessErrorWinRM, ProcessErrorWMIC
# End Variables
# Now Trying WinRM
If ($Computer -eq $env:COMPUTERNAME) {
Write-Verbose "Installing Automate on Local Computer - $Computer"
Invoke-Expression(New-Object Net.WebClient).DownloadString('https://raw.githubusercontent.com/Braingears/PowerShell/master/Automate-Module.psm1')
Install-Automate -Server $Server -LocationID $LocationID -Token $Token -Show:$Show -Transcript:$Transcript
} Else { # Remote Computer
If (!$Silent) {Write-Host "$($Time) - Now Checking $($COMPUTER)"}
Write-Verbose "Ping Connectivity - Testing..."
If (Test-Connection -ComputerName $COMPUTER -Count 1 -Quiet) {
Write-Verbose "Ping Connectivity - Passed"
$PingTest = $True
Write-Verbose "IP or NetBIOS Name - Testing..."
If ($Computer -notmatch "[a-z]") {
Write-Verbose "$Computer is IP Address"
$ComputerNetBIOS = nbtstat -A $Computer | Where-Object { $_ -match '^\s*([^<\s]+)\s*<00>\s*UNIQUE' } | ForEach-Object { $matches[1] }
if ($ComputerNetBIOS -eq $Null) {
Write-Verbose "$Computer could not query NetBIOS Name"
Write-Verbose "$Computer as an IP Address will likely fail WinRM Connectivity"
} else {
Write-Verbose "Replacing $Computer with $ComputerNetBIOS"
$Computer = $ComputerNetBIOS
}
}
Try {
Write-Verbose "Proactively Remote Starting WinRM Service"
Get-Service WinRM -ComputerName $Computer -ErrorAction Stop | Start-Service
}
Catch {Write-Verbose "Start Service - Failed"}
Try {
# The $WinRMConectivity will change to $True if the Invoke-Command has no $Errors
$WinRMConectivity = $False
$WinRMFailed = $True
Write-Verbose "WinRM Connectivity - Testing..."
$Global:Automate = (Invoke-Command $COMPUTER -Credential $Credential -ScriptBlock $CheckAutomateWinRM -ErrorAction Stop -ErrorVariable ProcessErrorWinRM)
Write-Verbose "WinRM Connectivity - Passed"
Write-Verbose "Global Automate: $($Global:Automate)"
$WinRMConectivity = $True
$WinRMFailed = $False
}
Catch [System.Management.Automation.Remoting.PSRemotingTransportException] {
If ($($ProcessErrorWinRM) -like "*Logon failure*") {
Write-Verbose "WinRM Connectivity - Credentials Failed"
Write-Host "WinRM Connectivity - Credentials Failed" -ForegroundColor Red
} else {
Write-Verbose "WinRM Connectivity - Failed"
}
}
Catch {
Write-Verbose "WinRM Connectivity - Failed"
Write-Verbose "WinRM Errors: $ProcessErrorWinRM.Exception"
$ProcessErrorWinRM.Exception | Select -Property *
}
If (($Global:Automate.ServerAddress -like "*$($Server)*") -and $WinRMConectivity -and $Global:Automate.Online -and !$Force) {
If ($Show) {
$Global:Automate
} Else {
Write-Host "The Automate Agent is already installed on $($Global:Automate.Computername) ($($Global:Automate.ComputerID)) and checked-in $($Global:Automate.LastStatus) seconds ago to $($Global:Automate.ServerAddress)" -ForegroundColor Green
}
} Else {
If ($WinRMConectivity) {
Write-Verbose "WinRM Connectivity - Passed"
Write-Verbose "Installing Automate..."
Invoke-Command $COMPUTER -Credential $Credential -ScriptBlock $InstallAutomateWinRM -ArgumentList $Server, $LocationID, $Token, $Force, $Silent, $Transcript -ErrorAction SilentlyContinue
$Global:Automate = (Invoke-Command $COMPUTER -Credential $Credential -ScriptBlock $CheckAutomateWinRM -ErrorAction SilentlyContinue)
Write-Verbose "Local Automate: $($Automate)"
Write-Verbose "Global Automate: $($Global:Automate)"
$WinRMDeployed = $True
}
}
#### Now Trying RPC
If (!$Global:Automate.Online) {
Write-Verbose "WMIC Connectivity - Testing..."
Try {
$WMICFailed = $True
$WMICConectivity = $False
$ComputerWMI = ((Get-WmiObject -ComputerName $Computer -Class Win32_ComputerSystem -Credential $Credential -ErrorAction Stop -ErrorVariable ProcessErrorWMIC).Name)
Write-Verbose "WMIC Connectivity - Passed"
$WMICConectivity = $True
$WMICFailed = $False
}
Catch [System.Runtime.InteropServices.COMException] {
Write-Verbose "WMIC Connectivity - RPC Server is Unavailable"
}
Catch [System.UnauthorizedAccessException] {
Write-Verbose "WMIC Connectivity - Credentials Failed"
Write-Host "WMIC Connectivity - Credentials Failed" -ForegroundColor Red
}
Catch {
Write-Verbose "WMIC Connectivity - Failed"
Write-Verbose "WMIC Errors: $ProcessErrorWMIC"
}
If ($WMICConectivity) {
$Reg = Get-WmiObject -List StdRegProv -Namespace root\default -ComputerName $Computer -Credential $Credential
$HKLM = 2147483650
$Key = 'SOFTWARE\LabTech\Service\'
$Values = $Reg.EnumValues($HKLM,$Key)
# Registry types enumerations:
$RegTypes = @{
1 = 'REG_SZ'
2 = 'REG_EXPAND_SZ'
3 = 'REG_BINARY'
4 = 'REG_DWORD'
7 = 'REG_MULTI_SZ'
}
# Use a for loop to go through the values
$Results = @(
for ($i = 0; $i -lt $Values.sNames.count; $i++) {
$Name = $Values.sNames[$i]
$Type = $RegTypes[$Values.Types[$i]]
switch ($Values.Types[$i]) {
1 {$Value = $Reg.GetStringValue($HKLM,$Key,$Name).sValue}
2 {$Value = $Reg.GetExpandedStringValue($HKLM,$Key,$Name).sValue}
3 {$Value = $Reg.GetBinaryValue($HKLM,$Key,$Name).uValue}
4 {$Value = $Reg.GetDWORDValue($HKLM,$Key,$Name).uValue}
7 {$Value = $Reg.GetMultiStringValue($HKLM,$Key,$Name).sValue}
}
[pscustomobject]@{
Name = $Name
Type = $Type
Data = $Value
}
}
) # $Results - Registry
If ($Results) {
Write-Verbose "Confirm Install - Automate Installed - Registry Keys Found"
$Global:Automate = New-Object -TypeName psobject
$Global:Automate | Add-Member -MemberType NoteProperty -Name ComputerName -Value $ComputerWMI
$Global:Automate | Add-Member -MemberType NoteProperty -Name ServerAddress -Value (($Results | Where-Object -Property Name -eq 'Server Address').Data)
$Global:Automate | Add-Member -MemberType NoteProperty -Name ComputerID -Value (($Results | Where-Object -Property Name -eq 'ID').Data)
$Global:Automate | Add-Member -MemberType NoteProperty -Name ClientID -Value (($Results | Where-Object -Property Name -eq 'ClientID').Data)
$Global:Automate | Add-Member -MemberType NoteProperty -Name LocationID -Value (($Results | Where-Object -Property Name -eq 'LocationID').Data)
$Global:Automate | Add-Member -MemberType NoteProperty -Name Version -Value (($Results | Where-Object -Property Name -eq 'Version').Data)
$Global:Automate | Add-Member -MemberType NoteProperty -Name InstFolder -Value (Test-Path "$($env:windir)\ltsvc")
$Global:Automate | Add-Member -MemberType NoteProperty -Name InstRegistry -Value $True
$Global:Automate | Add-Member -MemberType NoteProperty -Name Installed -Value (Test-Path "$($env:windir)\ltsvc")
$Global:Automate | Add-Member -MemberType NoteProperty -Name Service -Value ((Get-WmiObject -ComputerName $Computer -Class Win32_Service -Filter "Name='LTService'" -Credential $Credential -ErrorAction SilentlyContinue -ErrorVariable ProcessErrorWMIC).State)
if ((Get-ItemProperty "HKLM:\SOFTWARE\LabTech\Service").HeartbeatLastSent) {
$Global:Automate | Add-Member -MemberType NoteProperty -Name LastHeartbeat -Value ([int]((Get-Date) - (Get-Date (Get-ItemProperty "HKLM:\SOFTWARE\LabTech\Service").HeartbeatLastSent)).TotalSeconds)
}
if ((Get-ItemProperty "HKLM:\SOFTWARE\LabTech\Service").LastSuccessStatus) {
$Global:Automate | Add-Member -MemberType NoteProperty -Name LastStatus -Value ([int]((Get-Date) - (Get-Date (Get-ItemProperty "HKLM:\SOFTWARE\LabTech\Service").LastSuccessStatus)).TotalSeconds)
}
$Global:Automate | Add-Member -MemberType NoteProperty -Name Online -Value ($Global:Automate.InstFolder -and ($Global:Automate.Service -eq "Running"))
Write-Verbose $Global:Automate
If (($Global:Automate.ServerAddress -like "*$($Server)*") -and $Global:Automate.Online -and !$Force) {
If ($Show) {
$Global:Automate
} Else {
Write-Host "The Automate Agent is already installed on $($Global:Automate.Computername) ($($Global:Automate.ComputerID)) and checked-in $($Global:Automate.LastStatus) seconds ago to $($Global:Automate.ServerAddress)" -ForegroundColor Green
}
} Else {
IF (!($Global:Automate.ServerAddress -like "*$($Server)*")) {
Write-Host "The Existing Automate Server Does Not Match The Target Automate Server." -ForegroundColor Red
Write-Host "Current Automate Server: $($Global:Automate.ServerAddress)" -ForegroundColor Red
}
Write-Verbose "Installing Automate..."
$WMIExitCode = Invoke-WmiMethod -class Win32_process -name Create -ArgumentList $WMIArg -ComputerName $Computer -Impersonation 3 -EnableAllPrivileges -Credential $Credential -ErrorAction SilentlyContinue
If ($WMIExitCode.ReturnValue -eq 0) {
Write-Host "Installing Automate Agent to https://$($Server) - WMI" -ForegroundColor Green
Write-Verbose "When pushing via WMI/RPC, the function will not wait and confirm the installation. "
$WMIDeployed = $True
} Else {
Write-Host "WMI Did NOT Execute Properly." -ForegroundColor Red
Write-Host "WMI Return Value: $($WMIExitCode.ReturnValue)" -ForegroundColor Red
}
}
} else {
Write-Verbose "Confirm Install - Automate NOT Installed"
Write-Verbose "Installing Automate..."
$WMIExitCode = Invoke-WmiMethod -class Win32_process -name Create -ArgumentList $WMIArg -ComputerName $Computer -Impersonation 3 -EnableAllPrivileges -Credential $Credential -ErrorAction SilentlyContinue
If ($WMIExitCode.ReturnValue -eq 0) {