Scripts/NlaSvcFix.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
Set-StrictMode -Version Latest
$ErrorActionPreference = 'Stop'

if ((Get-CimInstance -ClassName Win32_ComputerSystem).PartOfDomain) {
    while ($true) {
        if (Get-NetConnectionProfile | Where-Object { $_.NetworkCategory -eq 'DomainAuthenticated' }) {
            "$(Get-Date) NlaSvcFix not needed"
            break
        } else {
            try {
                if (Get-Service netprofm | Where-Object { $_.Status -eq 'Running' }) {
                    "$(Get-Date) NlaSvcFix stopping netprofm"
                    Stop-Service netprofm -Force
                }
                if (Get-Service nlasvc | Where-Object { $_.Status -eq 'Running' }) {
                    "$(Get-Date) NlaSvcFix stopping nlasvc"
                    Stop-Service nlasvc -Force
                }
                if (Get-Service nlasvc | Where-Object { $_.Status -ne 'Running' }) {
                    "$(Get-Date) NlaSvcFix starting nlasvc"
                    Start-Service nlasvc
                }
                if (Get-Service netprofm | Where-Object { $_.Status -ne 'Running' }) {
                    "$(Get-Date) NlaSvcFix starting netprofm"
                    Start-Service netprofm
                }
            } catch {
                "$(Get-Date) NlaSvcFix error occurred"
                $_
            }
        }
    }
} else {
    "$(Get-Date) NlaSvcFix skipped as this computer is not yet domain joined"
}