Last active
March 5, 2024 03:09
-
-
Save davidlu1001/293e3a9f9004b7ff5a86562f32618efb to your computer and use it in GitHub Desktop.
SMB script to check/enable/disable
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
| param( | |
| [ValidateSet("Check", "Enable", "Disable")] | |
| [string]$Action = "Check", | |
| [ValidateSet("SMB1", "SMB2", "All")] | |
| [string]$SMBVersion = "All" | |
| ) | |
| function Check-SMBStatus { | |
| Write-Host "Checking SMB protocol status..." | |
| if ($SMBVersion -eq "SMB1") { | |
| Write-Host "SMBv1 status:" | |
| Get-SmbServerConfiguration | Select EnableSMB1Protocol | |
| } | |
| if ($SMBVersion -eq "SMB2") { | |
| Write-Host "SMBv2/SMBv3 status:" | |
| Get-SmbServerConfiguration | Select EnableSMB2Protocol | |
| } | |
| if ($SMBVersion -eq "All") { | |
| Write-Host "SMBv1/SMBv2/SMBv3 status:" | |
| Get-SmbServerConfiguration | Select EnableSMB1Protocol, EnableSMB2Protocol | |
| } | |
| } | |
| function Enable-SMBProtocol { | |
| Write-Host "Enabling SMB protocol..." | |
| if ($SMBVersion -eq "SMB1") { | |
| Write-Host "Enabling SMBv1..." | |
| Set-SmbServerConfiguration -EnableSMB1Protocol $true -Force | |
| } | |
| if ($SMBVersion -eq "SMB2") { | |
| Write-Host "Enabling SMBv2/SMBv3..." | |
| Set-SmbServerConfiguration -EnableSMB2Protocol $true -Force | |
| } | |
| if ($SMBVersion -eq "All") { | |
| Write-Host "Enabling SMBv1/SMBv2/SMBv3..." | |
| Set-SmbServerConfiguration -EnableSMB1Protocol $true -Force | |
| Set-SmbServerConfiguration -EnableSMB2Protocol $true -Force | |
| } | |
| } | |
| function Disable-SMBProtocol { | |
| Write-Host "Disabling SMB protocol..." | |
| if ($SMBVersion -eq "SMB1") { | |
| Write-Host "Disabling SMBv1..." | |
| Set-SmbServerConfiguration -EnableSMB1Protocol $false -Force | |
| } | |
| if ($SMBVersion -eq "SMB2") { | |
| Write-Host "Disabling SMBv2/SMBv3..." | |
| Set-SmbServerConfiguration -EnableSMB2Protocol $false -Force | |
| } | |
| if ($SMBVersion -eq "All") { | |
| Write-Host "Disabling SMBv1/SMBv2/SMBv3..." | |
| Set-SmbServerConfiguration -EnableSMB1Protocol $false -Force | |
| Set-SmbServerConfiguration -EnableSMB2Protocol $false -Force | |
| } | |
| } | |
| try { | |
| switch ($Action) { | |
| "Check" { | |
| Check-SMBStatus | |
| } | |
| "Enable" { | |
| Enable-SMBProtocol | |
| } | |
| "Disable" { | |
| Disable-SMBProtocol | |
| } | |
| } | |
| } catch { | |
| Write-Error "An error occurred: $_" | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment