Last active
July 15, 2021 18:45
-
-
Save jrdnr/55ff635263ddc9612bb26bb0f773a7f0 to your computer and use it in GitHub Desktop.
CheckPublicNetwork.ps1
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| if ($env:SyncroModule){Import-Module $env:SyncroModule} | |
| $PartOfDomain = (Get-WmiObject -Class Win32_ComputerSystem).PartOfDomain | |
| # Check for network connections set to Public | |
| [hashtable]$NetworkConnections = @{} | |
| try { | |
| $networkInfo = Get-NetConnectionProfile -ErrorAction Stop | |
| } | |
| catch { | |
| try { | |
| $networkInfo = Get-WmiObject MSFT_NetConnectionProfile -Namespace root/StandardCimv2 -ErrorAction Stop | | |
| Select-Object Name,InterfaceAlias,InterfaceIndex,@{n='NetworkCategory';e={ | |
| switch ($_.NetworkCategory){ | |
| 0 {'Public'} | |
| 1 {'Private'} | |
| 2 {'Domain'} | |
| Default {$_.NetworkCategory} | |
| } | |
| } | |
| },@{n='IPv4Connectivity';e={ | |
| switch ($_.IPv4Connectivity){ | |
| 0 {'Disconnected'} | |
| 1 {'NoTraffic'} | |
| 2 {'Subnet'} | |
| 3 {'LocalNetwork'} | |
| 4 {'Internet'} | |
| Default {$_.IPv4Connectivity} | |
| } | |
| } | |
| },@{n='IPv6Connectivity';e={ | |
| switch ($_.IPv6Connectivity){ | |
| 0 {'Disconnected'} | |
| 1 {'NoTraffic'} | |
| 2 {'Subnet'} | |
| 3 {'LocalNetwork'} | |
| 4 {'Internet'} | |
| Default {$_.IPv6Connectivity} | |
| } | |
| } | |
| } | |
| } | |
| catch { | |
| Write-Host 'Fully Qualified Error:' -NoNewline | |
| (($Error[0] | Select-Object -Property Exception | Out-String).trim() -split '\r')[-1] | |
| '' | |
| Write-Host "Powershell Version: $($PSVersionTable.PSVersion)" | |
| $OS = Get-WmiObject Win32_OperatingSystem | |
| 'OS: {0}, v{1}' -f $OS.Caption, $OS.Version | |
| '' | |
| 'Getting Command Get-NetConnectionProfile' | |
| Get-Command -Name Get-NetConnectionProfile | |
| } | |
| } | |
| $NetworkInfo | Where-Object {$_.IPv4Connectivity -eq 'Internet' -or $_.IPv6Connectivity -eq 'Internet'} | | |
| ForEach-Object { | |
| $NCat = [string]$_.NetworkCategory | |
| if($NetworkConnections[$NCat]){ | |
| $NetworkConnections[$NCat] += ",$($_.InterfaceAlias)" | |
| } else { | |
| $NetworkConnections.Add($NCat,[string]$_.InterfaceAlias) | |
| } | |
| } | |
| $Networks = ($NetworkConnections.Keys | ForEach-Object{ | |
| '{0}:{1}' -f $_,$NetworkConnections.$_ | |
| }) -join '; ' | |
| $Networks | |
| if($PartOfDomain -eq $false -and $NetworkConnections.Keys -notcontains 'Private') { | |
| if ($env:SyncroModule){ | |
| Rmm-Alert -Category "Public Network Detected" -Body $Networks | |
| } else { | |
| Write-Warning "Public networks detected: $Networks" | |
| } | |
| } | |
| if ($env:SyncroModule){Set-Asset-Field -Name "Network Profile Status" -Value $Networks} |
Author
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Updated to now post networks with internet connections, and only alert on non-domain joined computers