HM-Monitoring.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
################################################################################################################

#region Check_SNMP_Value_If_Exists
<#
.SYNOPSIS
Checks if the value provided by the snmp deamon is a valid value
 
.DESCRIPTION
Checks if the value provided by the snmp deamon is a valid value
 
.PARAMETER SNMPValue
The Output of an SNMP request
 
.EXAMPLE
Check_SNMP_Value_If_Exists -SNMPValue $AllSNMPDATA.Key
 
.NOTES
 
#>

function Check_SNMP_Value_If_Exists {
    [CmdletBinding()]
    param (
        [Parameter(Mandatory = $false)]
        $SNMPValue
    )

    if (
        $SNMPValue -notlike "*NoSuchInstance*" -and `
        $SNMPValue -notlike "*NoSuchObject*" -and `
        $SNMPValue -ne "" -and `
        $null -ne $SNMPValue)
    { 
        return $true
    }
    else { 
        return $false
    }
}
Export-ModuleMember -Function Check_SNMP_Value_If_Exists 
#endregion

#region Test-SNMP_Value_If_Exists
<#
.SYNOPSIS
Checks if the value provided by the snmp deamon is a valid value
 
.DESCRIPTION
Checks if the value provided by the snmp deamon is a valid value
 
.PARAMETER SNMPValue
The Output of an SNMP request
 
.EXAMPLE
Test-SNMP_Value_If_Exists -SNMPValue $AllSNMPDATA.Key
 
.NOTES
 
#>

function Test-SNMP_Value_If_Exists {
    [CmdletBinding()]
    param (
        [Parameter(Mandatory = $false)]
        $SNMPValue
    )

    if (
        $SNMPValue -notlike "*NoSuchInstance*" -and `
        $SNMPValue -notlike "*NoSuchObject*" -and `
        $SNMPValue -ne "" -and `
        $null -ne $SNMPValue)
    { 
        return $true
    }
    else { 
        return $false
    }
}
Export-ModuleMember -Function Test-SNMP_Value_If_Exists 
#endregion

################################################################################################################

#region Convert_Version_To_Accumulated
<#
    .Description
    Compares to versions insertet as an string array
 
    .example
    $FoundVersionArray = "21.2.16.590".Split(".")
    $MinVerionArray = "11.5.22.980".Split(".")
    Convert_Version_To_Accumulated -VersionArray $FoundVersionArray -lt Convert_Version_To_Accumulated -VersionArray $MinVerionArray
#>

function Convert_Version_To_Accumulated {
    [CmdletBinding()]
    param (
        # VersionArray
        [Parameter(Mandatory = $true)]
        $VersionArray
    )

    [int64]$Version = ([int64]$VersionArray[3] * 1 + [int64]$VersionArray[2] * 10000 + [int64]$VersionArray[1] * 100000000 + [int64]$VersionArray[0] * 1000000000000)

    return [int64]$Version
}
Export-ModuleMember -Function Convert_Version_To_Accumulated
#endregion

#region Convert-Version_To_Accumulated
<#
    .Description
    Compares to versions insertet as an string array
 
    .example
    $FoundVersionArray = "21.2.16.590".Split(".")
    $MinVerionArray = "11.5.22.980".Split(".")
    Convert-Version_To_Accumulated -VersionArray $FoundVersionArray -lt Convert_Version_To_Accumulated -VersionArray $MinVerionArray
#>

function Convert-Version_To_Accumulated {
    [CmdletBinding()]
    param (
        # VersionArray
        [Parameter(Mandatory = $true)]
        $VersionArray
    )

    [int64]$Version = ([int64]$VersionArray[3] * 1 + [int64]$VersionArray[2] * 10000 + [int64]$VersionArray[1] * 100000000 + [int64]$VersionArray[0] * 1000000000000)

    return [int64]$Version
}
Export-ModuleMember -Function Convert-Version_To_Accumulated
#endregion

################################################################################################################

#region Convert_Date_To_German
<#
    .Description
    Converts to german time format
#>

function Convert_Date_To_German {
    [CmdletBinding()]
    param (
        [System.DateTime]
        $DateObj
    )    
    return (Get-Date $Dateobj -Format "dd.MM.yyyy HH:mm:ss")
}
Export-ModuleMember -Function Convert_Date_To_German
#endregion

#region Convert-Date_To_German
<#
    .Description
    Converts to german time format
#>

function Convert-Date_To_German {
    [CmdletBinding()]
    param (
        [System.DateTime]
        $DateObj
    )    
    return (Get-Date $Dateobj -Format "dd.MM.yyyy HH:mm:ss")
}
Export-ModuleMember -Function Convert-Date_To_German
#endregion

################################################################################################################

#region Write_Separator
<#
.Description
writes separator
#>

function Write_Separator {
    Write-Host ""
    Write-Host "----------------------------------------------------------"
}
Export-ModuleMember -Function Write_Separator
#endregion

#region Write-Separator
<#
.Description
writes separator
#>

function Write-Separator {
    Write-Host ""
    Write-Host "----------------------------------------------------------"
}
Export-ModuleMember -Function Write-Separator
#endregion

################################################################################################################

#region Check_SNMP_Module_Installed
<#
.SYNOPSIS
Checks if the SNMP Module is installed
 
.DESCRIPTION
Checks if the SNMP Module is installed
 
.EXAMPLE
Check_SNMP_Module_Installed
 
.NOTES
No Parameters
#>

function Check_SNMP_Module_Installed {
    if ([environment]::OSVersion.Version.Major -gt 8) {
        if ($null -eq (Get-Module -ListAvailable -Name Snmp)) {
            Write-Host "PowerShell Module SNMP is not installed!"
            Write-Host "To install this Module use 'Install-Module -Name SNMP -Force' on systems with PowerShell Version 5.0 or greater"
            Write-Host "otherwise please copy the module from another device into the folder 'C:\Program Files\WindowsPowerShell\Modules'"
            Error_Exit
        }
    }
    else {
        $PSModulePath1 = "C:\Program Files\WindowsPowerShell\Modules"
        $PSModulePath2 = "C:\Windows\System32\WindowsPowerShell\v1.0\Modules"
    
        if (Test-Path -path "$PSModulePath1\SNMP\*\SNMP.psm1") {
            try {
                Set-ExecutionPolicy -ExecutionPolicy Unrestricted -Force -Scope Process
                Import-Module "$PSModulePath1\SNMP\1.0.0.1\SNMP.psm1"
                Add-Type -Path "$PSModulePath1\SNMP\1.0.0.1\SharpSnmpLib.dll"
            }
            catch { 
                Write-Host "(Error - SNMP-Module): $($PSItem.Exception.Message)"
                Error_Exit
            }
        }
        elseif (Test-Path -path "$PSModulePath2\SNMP\*\SNMP.psm1") {
            try {
                Set-ExecutionPolicy -ExecutionPolicy Unrestricted -Force -Scope Process
                Import-Module "$PSModulePath2\SNMP\1.0.0.1\SNMP.psm1"
                Add-Type -Path "$PSModulePath2\SNMP\1.0.0.1\SharpSnmpLib.dll"
            }
            catch { 
                Write-Host "(Error - SNMP-Module): $($PSItem.Exception.Message)"
                Error_Exit
            }
        }
        else { 
            Write-Host "PowerShell Module SNMP is not installed!"
            Write-Host "To install this Module use 'Install-Module -Name SNMP -Force' on systems with PowerShell Version 5.0 or greater"
            Write-Host "otherwise please copy the module from another device into the folder 'C:\Program Files\WindowsPowerShell\Modules'"
            Error_Exit
        }
    }
}
Export-ModuleMember -Function Check_SNMP_Module_Installed
#endregion

#region Test-SNMP_Module_Installed
<#
.SYNOPSIS
Checks if the SNMP Module is installed
 
.DESCRIPTION
Checks if the SNMP Module is installed
 
.EXAMPLE
Test-SNMP_Module_Installed
 
.NOTES
No Parameters
#>

function Test-SNMP_Module_Installed {
    if ([environment]::OSVersion.Version.Major -gt 8) {
        if ($null -eq (Get-Module -ListAvailable -Name Snmp)) {
            Write-Host "PowerShell Module SNMP is not installed!"
            Write-Host "To install this Module use 'Install-Module -Name SNMP -Force' on systems with PowerShell Version 5.0 or greater"
            Write-Host "otherwise please copy the module from another device into the folder 'C:\Program Files\WindowsPowerShell\Modules'"
            Exit-Error
        }
    }
    else {
        $PSModulePath1 = "C:\Program Files\WindowsPowerShell\Modules"
        $PSModulePath2 = "C:\Windows\System32\WindowsPowerShell\v1.0\Modules"
    
        if (Test-Path -path "$PSModulePath1\SNMP\*\SNMP.psm1") {
            try {
                Set-ExecutionPolicy -ExecutionPolicy Unrestricted -Force -Scope Process
                Import-Module "$PSModulePath1\SNMP\1.0.0.1\SNMP.psm1"
                Add-Type -Path "$PSModulePath1\SNMP\1.0.0.1\SharpSnmpLib.dll"
            }
            catch { 
                Write-Host "(Error - SNMP-Module): $($PSItem.Exception.Message)"
                Exit-Error
            }
        }
        elseif (Test-Path -path "$PSModulePath2\SNMP\*\SNMP.psm1") {
            try {
                Set-ExecutionPolicy -ExecutionPolicy Unrestricted -Force -Scope Process
                Import-Module "$PSModulePath2\SNMP\1.0.0.1\SNMP.psm1"
                Add-Type -Path "$PSModulePath2\SNMP\1.0.0.1\SharpSnmpLib.dll"
            }
            catch { 
                Write-Host "(Error - SNMP-Module): $($PSItem.Exception.Message)"
                Exit-Error
            }
        }
        else { 
            Write-Host "PowerShell Module SNMP is not installed!"
            Write-Host "To install this Module use 'Install-Module -Name SNMP -Force' on systems with PowerShell Version 5.0 or greater"
            Write-Host "otherwise please copy the module from another device into the folder 'C:\Program Files\WindowsPowerShell\Modules'"
            Exit-Error
        }
    }
}
Export-ModuleMember -Function Test-SNMP_Module_Installed
#endregion

################################################################################################################

#region Error_Exit
<#
    .Description
    Increases the Count in $ErrorCount and does an Exit
#>

function Error_Exit {
    $Script:ErrorCount++
    Exit 1001
}
Export-ModuleMember -Function Error_Exit
#endregion

#region Exit-Error
<#
    .Description
    Increases the Count in $ErrorCount and does an Exit
#>

function Exit-Error {
    $Script:ErrorCount++
    Exit 1001
}
Export-ModuleMember -Function Exit-Error
#endregion

################################################################################################################

#region Get-N_Able_RMM_Data
<#
.DESCRIPTION
Pulls N-Able RMM API Data
 
.PARAMETER Hostname
Hostname of the Dashboard, for example: wwwgermany1.systemmonitor.eu.com
 
.PARAMETER ApiKey
APIKey of the Dashboard, for example: d48a36b88c8ffhthtfdhjtzj5d653881
                                     
.PARAMETER Service
Service you want do use, for example: list_clients
 
.EXAMPLE
Get-N_Able_RMM_Data -Hostname $Hostname -ApiKey $ApiKey -Service "list_clients"
 
.NOTES
https://documentation.n-able.com/remote-management/userguide/Content/data_extraction_api.htm
#>

function Get-N_Able_RMM_Data {
    [CmdletBinding()]
    param (
        [Parameter(Mandatory)]
        [string]
        $Hostname,

        [Parameter(Mandatory)]
        [string]
        $ApiKey,

        [Parameter(Mandatory)]
        [string]
        $Service
    )

    try {
        [Net.ServicePointManager]::SecurityProtocol = [Net.ServicePointManager]::SecurityProtocol -bor [Net.SecurityProtocolType]::Tls12
        $ProxySettings = (netsh winhttp show proxy | Select-String -Pattern "Proxy" | Select-String -Pattern "server").ToString().Split(":").Trim()
        $Endpoint = "https://$($Hostname)/api/?apikey=$($ApiKey)&service=$($Service)"

        if ($ProxySettings.count -gt 1) {
            $password = ConvertTo-SecureString "password" -AsPlainText -Force
            $ProxyCred = New-Object System.Management.Automation.PSCredential ("administrator", $password)
            [xml]$N_Able_RMM_Data = (Invoke-RestMethod -Uri $Endpoint -Method Post -ProxyCredential $proxyCred -Proxy "http://$($ProxySettings[1]):$($ProxySettings[2])").InnerXml
        }
        elseif ($ProxySettings -like "*Access*") {
            [Net.ServicePointManager]::SecurityProtocol = [Net.SecurityProtocolType]::Tls12
            [system.net.webrequest]::defaultwebproxy = new-object system.net.webproxy($proxy)
            [system.net.webrequest]::defaultwebproxy.credentials = [System.Net.CredentialCache]::DefaultNetworkCredentials
            [system.net.webrequest]::defaultwebproxy.BypassProxyOnLocal = $true
            [xml]$N_Able_RMM_Data = (Invoke-RestMethod -Uri $Endpoint -Method Post).InnerXml
        }
        return $N_Able_RMM_Data
    }
    catch {
        Write-Error $PSItem.Exception.Message
    }
}
Export-ModuleMember -Function Get-N_Able_RMM_Data
#endregion

################################################################################################################

#region Get-N_Able_RMM_Clients
<#
.DESCRIPTION
Pulls N-Able RMM Clients
 
.PARAMETER Hostname
Hostname of the Dashboard, for example: wwwgermany1.systemmonitor.eu.com
 
.PARAMETER ApiKey
APIKey of the Dashboard, for example: d48a36b88c8ffhthtfdhjtzj5d653881
 
.EXAMPLE
Get-N_Able_RMM_Data -Hostname $Hostname -ApiKey $ApiKey -Service "list_clients"
 
.NOTES
https://documentation.n-able.com/remote-management/userguide/Content/listing_clients_.htm
#>

function Get-N_Able_RMM_Clients {
    [CmdletBinding()]
    param (
        [Parameter(Mandatory)]
        [string]
        $Hostname,

        [Parameter(Mandatory)]
        [string]
        $ApiKey
    )

    try {
        $N_Able_RMM_Data = Get-N_Able_RMM_Data -Hostname $Hostname -ApiKey $ApiKey -Service "list_clients"
        return ($N_Able_RMM_Data.SelectNodes("//client"))
    }
    catch {
        Write-Error $PSItem.Exception.Message
    }
}
Export-ModuleMember -Function Get-N_Able_RMM_Clients
#endregion

################################################################################################################

#region Get-N_Able_RMM_Checks
<#
.DESCRIPTION
Pulls N-Able RMM Device Checks
 
.PARAMETER Hostname
Hostname of the Dashboard, for example: wwwgermany1.systemmonitor.eu.com
 
.PARAMETER ApiKey
APIKey of the Dashboard, for example: d48a36b88c8ffhthtfdhjtzj5d653881
 
.PARAMETER DeviceID
DeviceID of an Device, for example: 1519987
 
.EXAMPLE
Get-N_Able_RMM_Checks -Hostname $Hostname -ApiKey $ApiKey -DeviceID $DeviceID
 
.NOTES
https://documentation.n-able.com/remote-management/userguide/Content/listing_checks_.htm
#>

function Get-N_Able_RMM_Checks {
    [CmdletBinding()]
    param (
        [Parameter(Mandatory)]
        [string]
        $Hostname,

        [Parameter(Mandatory)]
        [string]
        $ApiKey,
        
        [Parameter(Mandatory)]
        [string]
        $DeviceID
    )
    try {
        $Checks = (Get-N_Able_RMM_Data -Hostname $Hostname -ApiKey $ApiKey -Service "list_checks&deviceid=$($DeviceID)").result.items.check
        return $Checks
    }
    catch {
        Write-Error $PSItem.Exception.Message
    }
}
Export-ModuleMember -Function Get-N_Able_RMM_Checks
#endregion

################################################################################################################

#region Get-N_Able_RMM_Client_Devices
<#
.DESCRIPTION
Pulls N-Able RMM Client Devices
 
.PARAMETER Hostname
Hostname of the Dashboard, for example: wwwgermany1.systemmonitor.eu.com
 
.PARAMETER ApiKey
APIKey of the Dashboard, for example: d48a36b88c8ffhthtfdhjtzj5d653881
 
.PARAMETER ClientID
ClientID of an Client, for example: 134615
 
.PARAMETER DeviceType
DeviceType of the Device, can only be 'server', 'workstation', 'mobile_device'
 
.EXAMPLE
Get-N_Able_RMM_Client_Devices -Hostname $Hostname -ApiKey $ApiKey -ClientID $ClientID -DeviceType server
 
.EXAMPLE
Get-N_Able_RMM_Client_Devices -Hostname $Hostname -ApiKey $ApiKey -ClientID $ClientID -DeviceType workstation
 
.NOTES
https://documentation.n-able.com/remote-management/userguide/Content/listing_devices_at_client_.htm
#>

function Get-N_Able_RMM_Client_Devices {
    [CmdletBinding()]
    param (
        [Parameter(Mandatory)]
        [string]
        $Hostname,

        [Parameter(Mandatory)]
        [string]
        $ApiKey,

        [Parameter(Mandatory)]
        [string]
        $ClientID,

        [Parameter(Mandatory)]
        [ValidateSet("server","workstation","mobile_device")]
        [string]
        $DeviceType
    )

    try {
        if ($DeviceType -ceq "Server" -or $DeviceType -ceq "Workstation") {
            throw "Parameter 'DeviceType' is case sensitiv"
        }
        $N_Able_RMM_Data = Get-N_Able_RMM_Data -Hostname $Hostname -ApiKey $ApiKey -Service "list_devices_at_client&clientid=$($ClientID)&devicetype=$($DeviceType)"
        if ("" -ne $N_Able_RMM_Data.SelectNodes("//workstation")) {
            return ($N_Able_RMM_Data.SelectNodes("//workstation"))
        }
        elseif ("" -ne $N_Able_RMM_Data.SelectNodes("//server")) {
            return ($N_Able_RMM_Data.SelectNodes("//server"))
        }
    }
    catch {
        Write-Error $PSItem.Exception.Message
    }
}
Export-ModuleMember -Function Get-N_Able_RMM_Client_Devices
#endregion

################################################################################################################

#region Get-N_Able_RMM_Templates
<#
.DESCRIPTION
Pulls N-Able RMM Templates
 
.PARAMETER Hostname
Hostname of the Dashboard, for example: wwwgermany1.systemmonitor.eu.com
 
.PARAMETER ApiKey
APIKey of the Dashboard, for example: d48a36b88c8ffhthtfdhjtzj5d653881
 
.PARAMETER DeviceType
DeviceType of the Device, can only be 'server', 'workstation'
 
.EXAMPLE
Get-N_Able_RMM_Templates -Hostname $Hostname -ApiKey $ApiKey -DeviceType server
 
.NOTES
https://documentation.n-able.com/remote-management/userguide/Content/listing_checks_.htm
#>

function Get-N_Able_RMM_Templates{
    [CmdletBinding()]
    param (
        [Parameter(Mandatory)]
        [string]
        $Hostname,

        [Parameter(Mandatory)]
        [string]
        $ApiKey,
        
        [Parameter(Mandatory)]
        [ValidateSet("server","workstation")]
        [string]
        $DeviceType
    )
    try {
        if ($DeviceType -ceq "Server" -or $DeviceType -ceq "Workstation") {
            throw "Parameter 'DeviceType' is case sensitiv"
        }
        else {
            $Templates = (Get-N_Able_RMM_Data -Hostname $Hostname -ApiKey $ApiKey -Service "list_templates&devicetype=$($DeviceType)").result.items.installation_template
            return $Templates | Select-Object templateid,@{label="name"; expression={$PSItem.name.'#cdata-section'}}
        }
    }
    catch {
        Write-Error $PSItem.Exception.Message
    }
}
Export-ModuleMember -Function Get-N_Able_RMM_Templates
#endregion

################################################################################################################

#region Get-N_Able_RMM_Device_Monitoring_Details
<#
.DESCRIPTION
Lists all monitoring information for the device (server or workstation) identified by the deviceid.
 
.PARAMETER Hostname
Hostname of the Dashboard, for example: wwwgermany1.systemmonitor.eu.com
 
.PARAMETER ApiKey
APIKey of the Dashboard, for example: d48a36b88c8ffhthtfdhjtzj5d653881
 
.PARAMETER DeviceID
DeviceID of an Device, for example: 1519987
 
.EXAMPLE
Get-N_Able_RMM_Device_Monitoring_Details -Hostname $Hostname -ApiKey $ApiKey -DeviceID $DeviceID
 
.NOTES
https://documentation.n-able.com/remote-management/userguide/Content/listing_device_monitoring_deta.htm
#>

function Get-N_Able_RMM_Device_Monitoring_Details {
    [CmdletBinding()]
    param (
        [Parameter(Mandatory)]
        [string]
        $Hostname,

        [Parameter(Mandatory)]
        [string]
        $ApiKey,

        [Parameter(Mandatory)]
        [string]
        $DeviceID
    )

    try {
        $N_Able_RMM_Data = Get-N_Able_RMM_Data -Hostname $Hostname -ApiKey $ApiKey -Service "list_device_monitoring_details&deviceid=$($DeviceID)"
        if ("" -ne $N_Able_RMM_Data.SelectNodes("//workstation")) {
            return ($N_Able_RMM_Data.SelectNodes("//workstation"))
        }
        elseif ("" -ne $N_Able_RMM_Data.SelectNodes("//server")) {
            return ($N_Able_RMM_Data.SelectNodes("//server"))
        }
    }
    catch {
        Write-Error $PSItem.Exception.Message
    }
}
Export-ModuleMember -Function Get-N_Able_RMM_Device_Monitoring_Details
#endregion

################################################################################################################

#region Get-N_Able_RMM_Device_Patches
<#
.DESCRIPTION
Pulls N-Able RMM Device Patch Status
 
.PARAMETER Hostname
Hostname of the Dashboard, for example: wwwgermany1.systemmonitor.eu.com
 
.PARAMETER ApiKey
APIKey of the Dashboard, for example: d48a36b88c8ffhthtfdhjtzj5d653881
 
.PARAMETER DeviceID
DeviceID of an Device, for example: 1519987
 
.EXAMPLE
Get-N_Able_RMM_Device_Patches -Hostname $Hostname -ApiKey $ApiKey -DeviceID $DeviceID
 
.NOTES
https://documentation.n-able.com/remote-management/userguide/Content/list_all_patches_for_device.htm
#>

function Get-N_Able_RMM_Device_Patches {
    [CmdletBinding()]
    param (
        [Parameter(Mandatory)]
        [string]
        $Hostname,

        [Parameter(Mandatory)]
        [string]
        $ApiKey,

        [Parameter(Mandatory)]
        [string]
        $DeviceID
    )
    try {
        $N_Able_RMM_Data = Get-N_Able_RMM_Data -Hostname $Hostname -ApiKey $ApiKey -Service "patch_list_all&deviceid=$($DeviceID)"
        return ($N_Able_RMM_Data.SelectNodes("//patch"))
    }
    catch {
        Write-Error $PSItem.Exception.Message
    }
}
Export-ModuleMember -Function Get-N_Able_RMM_Device_Patches
#endregion

################################################################################################################

#region Get-N_Able_RMM_Device_AssetDetails
<#
.DESCRIPTION
Pulls N-Able RMM Device Asset Details, like installed hardware or software
 
.PARAMETER Hostname
Hostname of the Dashboard, for example: wwwgermany1.systemmonitor.eu.com
 
.PARAMETER ApiKey
APIKey of the Dashboard, for example: d48a36b88c8ffhthtfdhjtzj5d653881
 
.PARAMETER DeviceID
DeviceID of an Device, for example: 1519987
 
.PARAMETER AssetType
AssetType of an Device, for example: software or hardware
 
.EXAMPLE
Get-N_Able_RMM_Device_AssetDetails -Hostname $Hostname -ApiKey $ApiKey -DeviceID $DeviceID -AssetType software
 
.NOTES
https://documentation.n-able.com/remote-management/userguide/Content/listing_device_asset_details.htm
#>

function Get-N_Able_RMM_Device_AssetDetails {
    [CmdletBinding()]
    param (
        [Parameter(Mandatory)]
        [string]
        $Hostname,

        [Parameter(Mandatory)]
        [string]
        $ApiKey,

        [Parameter(Mandatory)]
        [string]
        $DeviceID,

        [Parameter(Mandatory)]
        [ValidateSet("hardware","software")]
        [string]
        $AssetType
    )
    try {
        $N_Able_RMM_Data = Get-N_Able_RMM_Data -Hostname $Hostname -ApiKey $ApiKey -Service "list_device_asset_details&deviceid=$($DeviceID)"

        switch ($AssetType) {
            "hardware" { return ($N_Able_RMM_Data.result.hardware.item) }
            "software" { return ($N_Able_RMM_Data.result.software.item) }
            Default { throw "asset type not defined" }
        }        
    }
    catch {
        Write-Error $PSItem.Exception.Message
    }
}
Export-ModuleMember -Function Get-N_Able_RMM_Device_AssetDetails
#endregion

################################################################################################################

#region Get-N_Able_RMM_Devices_With_Software
<#
.DESCRIPTION
Pulls N-Able RMM with the specified Software
 
.PARAMETER Hostname
Hostname of the Dashboard, for example: wwwgermany1.systemmonitor.eu.com
 
.PARAMETER ApiKey
APIKey of the Dashboard, for example: d48a36b88c8ffhthtfdhjtzj5d653881
 
.PARAMETER SearchPattern
Type a string which should be found at the installed software, for example: veeam
 
.PARAMETER DeviceType
DeviceType of the Device, can only be 'server', 'workstation'
 
.EXAMPLE
Get-N_Able_RMM_Devices_With_Software -Hostname $Hostname -ApiKey $ApiKey -SearchPattern 'Veeam' -DeviceType server
 
.NOTES
https://documentation.n-able.com/remote-management/userguide/Content/data_extraction_api.htm
#>

function Get-N_Able_RMM_Devices_With_Software {
    [CmdletBinding()]
    param (
        [Parameter(Mandatory)]
        [string]
        $Hostname,

        [Parameter(Mandatory)]
        [string]
        $ApiKey,

        [Parameter(Mandatory)]
        [string]
        $SearchPattern,

        [Parameter(Mandatory)]
        [ValidateSet("workstation","server")]
        [string]
        $DeviceType
    )

    try {
        $All_Data = @{}
        $All_Clients = Get-N_Able_RMM_Clients -Hostname $Hostname -ApiKey $ApiKey

        foreach ($Client in $All_Clients) {
            [System.Collections.Arraylist]$DevicesFound = @()

            Write-Verbose -Message "Getting Client Devices"
            $DevicesToCheck = Get-N_Able_RMM_Client_Devices -Hostname $Hostname -ApiKey $ApiKey -ClientID $Client.clientid -DeviceType $DeviceType
                    
            foreach($Device in $DevicesToCheck) {
                $TempVar = (Get-N_Able_RMM_Device_AssetDetails -Hostname $Hostname -ApiKey $ApiKey -DeviceID $Device.ID -AssetType software | Where-Object -FilterScript { $PSItem.name.'#cdata-section' -like "*$($SearchPattern)*" }).name.'#cdata-section'
                if ($TempVar) {
                    $DevicesFound.Add("$($Device.Name.'#cdata-section')") | Out-Null
                }
            }

            if ($null -ne $DevicesFound -and $DevicesFound -ne "") {
                Write-Verbose -Message "Adding '$($Client.name.'#cdata-section')' to All_Data Object"
                $All_Data.Add($($Client.name.'#cdata-section'),$DevicesFound)
            }
        }
        
        return $All_Data
    }
    catch {
        Write-Error $PSItem.Exception.Message
    }
}
Export-ModuleMember -Function Get-N_Able_RMM_Devices_With_Software
#endregion

################################################################################################################
################################################################################################################
################################################################################################################
# already migrated

#region Write-End_Of_Script
<#
.Description
writes separator at the end of script
#>

function Write-End_Of_Script {
    Write-Host ""
    Write-Host "--------------------end-of-output-------------------------"
}
Export-ModuleMember -Function Write-End_Of_Script
#endregion

#region Test-FileLock
<#
.Description
Check if a file is in use
#>

function Test-FileLock {
    param (
        [parameter(Mandatory=$true)][string]$Path
    )

    $oFile = New-Object System.IO.FileInfo $Path
    if ((Test-Path -Path $Path) -eq $false) {
        return $false
    }
    try {
        $oStream = $oFile.Open([System.IO.FileMode]::Open, [System.IO.FileAccess]::ReadWrite, [System.IO.FileShare]::None)

        if ($oStream) {
            $oStream.Close()
        }
        $false
    }
    catch {
        # file is locked by a process.
        return $true
    }
}
Export-ModuleMember -Function Test-FileLock 
#endregion