Szybki skrypt dla SQL serwer
# ############ Zmień tę wartość przed uruchomieniem skryptu.
$RemoteAddress = '192.168.0.0/24'
# Port TCP skonfigurowany dla instancji SQL Server.
$SqlPort = 1433
# Ustaw na $true tylko wtedy, gdy potrzebujesz SQL Server Browser,
# np. dla nazwanej instancji bez podawania portu przy połączeniu.
$EnableSqlBrowser = $True
# poszukujemy jakie są insancje i gdzie jest SqLserwer.exe
Get-CimInstance Win32_Service |
Where-Object {
$_.Name -like „MSSQL*” -and $_.PathName -match „sqlservr.exe”
} |
ForEach-Object {
$exe = [regex]::Match($_.PathName,’^”?(.*?)”?(?=\s-|$)’).Groups[1].Value
if (Test-Path $exe)
{
New-NetFirewallRule `
-DisplayName „SQL Server plik_exe- $($_.Name)” `
-Direction Inbound `
-Program $exe `
-Action Allow `
-Profile Domain,Private `
-ErrorAction SilentlyContinue
Write-Host „Dodano regułę dla $($_.Name)”
}
}
# Ogranicz reguły programu utworzone przez sekcję wyszukiwania
# do wskazanej podsieci.
Get-NetFirewallRule -DisplayName 'SQL Server plik_exe-*' -ErrorAction SilentlyContinue |
Set-NetFirewallRule -RemoteAddress $RemoteAddress -Profile Private # -Profile Domain,Private
# Zezwól na połączenia do Database Engine na skonfigurowanym porcie TCP.
New-NetFirewallRule `
-DisplayName "SQL Server TCP $SqlPort" `
-Direction Inbound `
-Protocol TCP `
-LocalPort $SqlPort `
-RemoteAddress $RemoteAddress `
-Profile Private `
-Action Allow `
-ErrorAction SilentlyContinue | Out-Null
# Opcjonalnie zezwól na SQL Server Browser. Dla innych profili dodaj >>>>>>-Profile Domain,Private
if ($EnableSqlBrowser) {
New-NetFirewallRule `
-DisplayName "SQL Server Browser UDP 1434" `
-Direction Inbound `
-Protocol UDP `
-LocalPort 1434 `
-RemoteAddress $RemoteAddress `
-Profile Private `
-Action Allow `
-ErrorAction SilentlyContinue | Out-Null
}
# Podsumowanie aktywnych reguł utworzonych dla SQL Server.
$rules = Get-NetFirewallRule -ErrorAction SilentlyContinue |
Where-Object {
$_.DisplayName -like 'SQL Server plik_exe-*' -or
$_.DisplayName -eq "SQL Server TCP $SqlPort" -or
$_.DisplayName -eq 'SQL Server Browser UDP 1434'
}
if ($rules) {
Write-Host "`nReguły SQL Server w zaporze:"
$rules | ForEach-Object {
$rule = $_
$portFilter = $rule | Get-NetFirewallPortFilter
$addressFilter = $rule | Get-NetFirewallAddressFilter
[PSCustomObject]@{
Nazwa = $rule.DisplayName
Kierunek = $rule.Direction
Akcja = $rule.Action
Protokol = $portFilter.Protocol
PortLokalny = ($portFilter.LocalPort -join ', ')
AdresZrodlowy = ($addressFilter.RemoteAddress -join ', ')
Profil = $rule.Profile
Stan = $rule.Enabled
}
} | Format-Table -AutoSize
}
else {
Write-Host "`nNie znaleziono reguł SQL Server do podsumowania."
}
———-DIAGNOZA
# szyblie info jakie serwery
Get-NetFirewallRule -DisplayName 'SQL Server plik_exe-*' |
Select-Object @{
Name = 'Serwer lub instancja'
Expression = { $_.DisplayName -replace '^SQL Server plik_exe-', '' }
}, @{
Name = 'Włączona'
Expression = { $_.Enabled }
} |
Format-Table -AutoSize
# Więcej szcegółów co w firewall
Get-NetFirewallRule -DisplayName "SQL*" | Get-NetFirewallProfile
Get-NetFirewallRule -DisplayName "SQL*" | Format-List *
Sprawdź, co blokuje Firewall
Możesz włączyć logowanie:
New-Item -ItemType Directory -Path 'C:\Logs' -Force | Out-Null
Set-NetFirewallProfile `
-Profile Domain,Private `
-LogBlocked True `
-LogFileName 'C:\Logs\pfirewall.log'
Uruchom PowerShell jako administrator. Jeśli chcesz logować także dozwolone połączenia, dodaj:
-LogAllowed True
Możesz potem sprawdzić ustawienia:
Get-NetFirewallProfile -Profile Domain,Private |
Select-Object Name, LogBlocked, LogAllowed, LogFileName
0 Comments