PSSmb.psm1
Function Get-AvailableSmbVersion ($Session, $Version) { $sb = { Param($Version) $Version = [System.Collections.ArrayList]@($Version) $OsVersion = (Get-WmiObject Win32_OperatingSystem).Version $OsVersion -match '^\d+\.\d' | Out-Null [double]$OsVersion = $Matches[0] switch ($OsVersion) { {$_ -lt 6} { $Version.Remove(2) } {$_ -lt 6.2} { $Version.Remove(3) } } $Version } if ($Session) { Invoke-Command -ScriptBlock $sb -Session $Session -ArgumentList $Version, $null } else { & $sb $Version } } Function Get-Smb { <# .SYNOPSIS Get the status of SMB on local or remote computers .DESCRIPTION Get-Smb gets the status of SMB on local or remote computers, and returns an object reporting on both the client and the server components for each version installed on the system. Due to recent vulnerabilities, it is now recommended to disable SMB version 1, both the client and the server components, entirely. For more information, see Disable-Smb. .PARAMETER ComputerName The name of the computer to get the status of SMB from. Default is the local computer. .PARAMETER Session Specifies an array of sessions in which this command runs the command. Enter a variable that contains PSSession objects or a command that creates or gets the PSSession objects, such as a New-PSSession or Get-PSSession command. For more information, see about_PSSessions. .PARAMETER Version An array of integers corresponding to SMB versions to get the status of. Default is to retrieve all versions. .INPUTS System.String or System.Management.Automation.Runspaces.PSSession Get-Smb accepts either strings of computer names or PSSession objects. .OUTPUTS System.Management.Automation.PSCustomObject Get-Smb returns a PSCustomObject that details the client and server components for the requested SMB versions. .EXAMPLE PS C:> Get-Smb This command will get the status of the client and server components for all installed versions of SMB on the local computer. .EXAMPLE PS C:> Get-Smb -ComputerName server01 -Version 1 This command will get the status of the client and server components for SMB Version 1 on the computer named server01. .NOTES This command uses PowerShell Remoting to contact remote computers, and will not function if remote computers do not have it enabled. .LINK Invoke-Command .LINK Get-ItemProperty .LINK Add-Member #> [CmdletBinding(DefaultParameterSetName="ByName")] Param( [Parameter(ValueFromPipeline=$true,ParameterSetName="ByName")] [Alias("Cn")] [String[]]$ComputerName="$env:COMPUTERNAME", [Parameter(ValueFromPipeline=$true,ParameterSetName="BySession")] [System.Management.Automation.Runspaces.PSSession[]]$Session, [Parameter(Position=0)] [ValidateRange(1,3)] [Int[]]$Version=@(1,2,3) ) Begin { Function Get-SmbClientServerStatus ($Session, $Version) { $sb = { Param($Version) $SmbResult = New-Object PSCustomObject foreach ($num in $Version) { if ($num -eq 3) { $adjustedNum = 2 } else { $adjustedNum = $num } $ServerValue = (Get-ItemProperty -Path HKLM:\System\CurrentControlSet\services\LanmanServer\Parameters -Name "SMB$adjustedNum" -ea SilentlyContinue)."SMB$adjustedNum" $ClientValue = (Get-ItemProperty -Path "HKLM:\SYSTEM\CurrentControlSet\services\mrxsmb$($adjustedNum)0" -Name "Start" -ea SilentlyContinue).Start if ($ServerValue -eq 1 -or $ServerValue -eq $null) { $ServerEnabled = $true } else { $ServerEnabled = $false } if ($ClientValue -ne 4) { $ClientEnabled = $true } else { $ClientEnabled = $false } $SmbResult | Add-Member -MemberType NoteProperty -Name "SmbServerV$num" -Value $ServerEnabled $SmbResult | Add-Member -MemberType NoteProperty -Name "SmbClientV$num" -Value $ClientEnabled } $SmbResult } if ($Session) { Invoke-Command -ScriptBlock $sb -Session $Session -Args $Version, $null } else { & $sb $Version } } } Process { foreach ($computer in $ComputerName) { if ($PSCmdlet.ParameterSetName -eq "ByName") { if ($computer -eq $env:COMPUTERNAME -or $computer -eq "." -or $computer -eq "localhost") { $AvailableVersions = Get-AvailableSmbVersion -Version $Version Get-SmbClientServerStatus -Version $AvailableVersions } else { if (-not ($Session = New-PSSession -ComputerName $computer)) { continue } } } if ($Session) { foreach ($s in $Session) { $AvailableVersions = Get-AvailableSmbVersion -Session $s -Version $Version Get-SmbClientServerStatus -Session $s -Version $AvailableVersions if ($PSCmdlet.ParameterSetName -eq "ByName") { Remove-PSSession $s } } } } } } Function Disable-Smb { <# .SYNOPSIS Disables the client and server components of SMB. .DESCRIPTION Disable-Smb disables the SMB client and server components of the version specified in the Version parameter on local and remote computers. It is also important that the computer be rebooted for the settings made by this command to take effect which can be done immediately by using the Restart parameter. Note that versions 2 and 3 of SMB use the same stack, so disabling one of them also disables the other. Due to recent vulnerabilities like EternalBlue that was used by the WannaCry ransomware and the wiper PetyaWrap, it is now recommended to disable SMB version 1, both the client and the server components, entirely. .PARAMETER ComputerName The name of the computer to disable SMB on. Default is the local computer. .PARAMETER Session Specifies an array of sessions in which this command runs the command. Enter a variable that contains PSSession objects or a command that creates or gets the PSSession objects, such as a New-PSSession or Get-PSSession command. For more information, see about_PSSessions. .PARAMETER PassThru Returns objects representing the items that were changed. By default, this command does not generate any output. .PARAMETER Restart For the changes made by this command to take effect, the computer must be rebooted. Specifying this parameter will cause the computer to reboot immediately once the changes are made. .PARAMETER Version The version of SMB to disable. .INPUTS System.String or System.Management.Automation.Runspaces.PSSession Disable-Smb accepts either strings of computer names or PSSession objects. .OUTPUTS None or System.Management.Automation.PSCustomObject and System.ServiceProcess.ServiceController When you use the PassThru parameter, Disable-Smb returns a collection of objects representing the objects changed. Otherwise, this command does not generate any output. .EXAMPLE PS C:> Disable-Smb -Version 1 This command will disable the client and server components of SMB version 1 on the local computer. The computer will still need to be rebooted in order for the changes to take effect. .EXAMPLE PS C:> Disable-Smb -ComputerName server01 -Version 1 -Restart This command will disable the client and server components for SMB Version 1 on the computer named server01, and then immediately reboot the machine so that the settings can take effect. .NOTES This command uses PowerShell Remoting to contact remote computers, and will not function if remote computers do not have it enabled. Any computer that runs this command must be rebooted for the settings to take effect. It is recommended to use the Restart parameter to cause the machine to reboot as soon as the changes are made. .LINK Invoke-Command .LINK Set-ItemProperty .LINK Set-Service #> [CmdletBinding(DefaultParameterSetName="ByName",SupportsShouldProcess=$true)] Param( [Parameter(ValueFromPipeline=$true,ParameterSetName="ByName")] [Alias("Cn")] [String[]]$ComputerName="$env:COMPUTERNAME", [Switch]$PassThru, [Switch]$Restart, [Parameter(ValueFromPipeline=$true,ParameterSetName="BySession")] [System.Management.Automation.Runspaces.PSSession]$Session, [Parameter(Position=0,Mandatory=$true)] [ValidateRange(1,3)] [Int]$Version ) Begin { Function Set-SmbClientServerStatus ($Session, $Version, $PassThru) { $sb = { Param($Version, $PassThru) if ($Version -eq 3) { $adjustedNum = 2 } else { $adjustedNum = $Version } if ($adjustedNum -eq 1) { $dependNum = 2 } else { $dependNum = 1 } if ((Get-Service -Name "mrxsmb$($dependNum)0").StartType -ne "Disabled") { $Dependency = "Bowser","MRxSmb$($dependNum)0","NSI" } else { $Dependency = "Bowser", "NSI" } Set-ItemProperty -Path HKLM:\System\CurrentControlSet\services\LanmanServer\Parameters -Name "SMB$adjustedNum" -Type DWord -Value 0 -PassThru:$PassThru Set-ItemProperty -Path HKLM:\SYSTEM\CurrentControlSet\services\LanmanWorkstation -Name DependOnService -Type MultiString -Value $Dependency -PassThru:$PassThru Set-Service -Name "mrxsmb$($adjustedNum)0" -StartupType Disabled -PassThru:$PassThru } if (!$Version) { return } if ($Session) { Invoke-Command -ScriptBlock $sb -Session $Session -Args $Version, $PassThru } else { & $sb -Version $Version -PassThru $PassThru } } } Process { foreach ($computer in $ComputerName) { if ($PSCmdlet.ParameterSetName -eq "ByName") { if ($computer -eq $env:COMPUTERNAME -or $computer -eq "." -or $computer -eq "localhost") { $AvailableVersion = Get-AvailableSmbVersion -Version $Version Set-SmbClientServerStatus -Version $AvailableVersion -PassThru $PassThru if ($Restart) { Restart-Computer -Force } } else { if (-not ($Session = New-PSSession -ComputerName $computer)) { continue } } } if ($Session) { foreach ($s in $Session) { $AvailableVersion = Get-AvailableSmbVersion -Session $s -Version $Version Set-SmbClientServerStatus -Session $s -Version $AvailableVersion -PassThru $PassThru if ($Restart) { Restart-Computer -ComputerName $s.ComputerName -Force } if ($PSCmdlet.ParameterSetName -eq "ByName") { Remove-PSSession $s -ea SilentlyContinue } } } } } } Function Enable-Smb { <# .SYNOPSIS Enables the client and server components of SMB. .DESCRIPTION Enable-Smb enables the SMB client and server components of the version specified in the Version parameter on local and remote computers. It is also important that the computer be rebooted for the settings made by this command to take effect which can be done immediately by using the Restart parameter. Note that versions 2 and 3 of SMB use the same stack, so enabling one of them also enables the other. Due to recent vulnerabilities, it is now recommended to disable SMB version 1, both the client and the server components, entirely. .PARAMETER ComputerName The name of the computer to enable SMB on. Default is the local computer. .PARAMETER Session Specifies an array of sessions in which this command runs the command. Enter a variable that contains PSSession objects or a command that creates or gets the PSSession objects, such as a New-PSSession or Get-PSSession command. For more information, see about_PSSessions. .PARAMETER PassThru Returns objects representing the items that were changed. By default, this command does not generate any output. .PARAMETER Restart For the changes made by this command to take effect, the computer must be rebooted. Specifying this parameter will cause the computer to reboot immediately once the changes are made. .PARAMETER Version The version of SMB to enable. .INPUTS System.String or System.Management.Automation.Runspaces.PSSession Enable-Smb accepts either strings of computer names or PSSession objects. .OUTPUTS None or System.Management.Automation.PSCustomObject and System.ServiceProcess.ServiceController When you use the PassThru parameter, Enable-Smb returns a collection of objects representing the objects changed. Otherwise, this command does not generate any output. .EXAMPLE PS C:> Enable-Smb -Version 1 This command will enable the client and server components of SMB version 1 on the local computer. The computer will still need to be rebooted in order for the changes to take effect. .EXAMPLE PS C:> Enable-Smb -ComputerName server01 -Version 1 -Restart This command will enable the client and server components for SMB Version 1 on the computer named server01, and then immediately reboot the machine so that the settings can take effect. .NOTES This command uses PowerShell Remoting to contact remote computers, and will not function if remote computers do not have it enabled. Any computer that runs this command must be rebooted for the settings to take effect. It is recommended to use the Restart parameter to cause the machine to reboot as soon as the changes are made. .LINK Invoke-Command .LINK Set-ItemProperty .LINK Set-Service #> [CmdletBinding(DefaultParameterSetName="ByName",SupportsShouldProcess=$true)] Param( [Parameter(ValueFromPipeline=$true,ParameterSetName="ByName")] [Alias("Cn")] [String[]]$ComputerName="$env:COMPUTERNAME", [Switch]$PassThru, [Switch]$Restart, [Parameter(ValueFromPipeline=$true,ParameterSetName="BySession")] [System.Management.Automation.Runspaces.PSSession]$Session, [Parameter(Position=0,Mandatory=$true)] [ValidateRange(1,3)] [Int]$Version ) Begin { Function Set-SmbClientServerStatus ($Session, $Version, $PassThru) { $sb = { Param($Version, $PassThru) if ($Version -eq 3) { $adjustedNum = 2 } else { $adjustedNum = $Version } if ($adjustedNum -eq 1) { $dependNum = 2 } else { $dependNum = 1 } if ((Get-Service -Name "mrxsmb$($dependNum)0").StartType -ne "Disabled") { $Dependency = "Bowser","MRxSmb$($dependNum)0","MRxSmb$($adjustedNum)0","NSI" } else { $Dependency = "Bowser","MRxSmb$($adjustedNum)0", "NSI" } Set-ItemProperty -Path HKLM:\System\CurrentControlSet\services\LanmanServer\Parameters -Name "SMB$adjustedNum" -Type DWord -Value 1 -PassThru:$PassThru Set-ItemProperty -Path HKLM:\SYSTEM\CurrentControlSet\services\LanmanWorkstation -Name DependOnService -Type MultiString -Value $Dependency -PassThru:$PassThru Set-Service -Name "mrxsmb$($adjustedNum)0" -StartupType Automatic -PassThru:$PassThru } if (!$Version) { return } if ($Session) { Invoke-Command -ScriptBlock $sb -Session $Session -Args $Version, $PassThru } else { & $sb -Version $Version -PassThru $PassThru } } } Process { foreach ($computer in $ComputerName) { if ($PSCmdlet.ParameterSetName -eq "ByName") { if ($computer -eq $env:COMPUTERNAME -or $computer -eq "." -or $computer -eq "localhost") { $AvailableVersion = Get-AvailableSmbVersion -Version $Version Set-SmbClientServerStatus -Version $AvailableVersion -PassThru $PassThru if ($Restart) { Restart-Computer -Force } } else { if (-not ($Session = New-PSSession -ComputerName $computer)) { continue } } } if ($Session) { foreach ($s in $Session) { $AvailableVersion = Get-AvailableSmbVersion -Session $s -Version $Version Set-SmbClientServerStatus -Session $s -Version $AvailableVersion -PassThru $PassThru if ($Restart) { Restart-Computer -ComputerName $s.ComputerName -Force } if ($PSCmdlet.ParameterSetName -eq "ByName") { Remove-PSSession $s -ea SilentlyContinue } } } } } } New-Alias -Name gsmb -Value Get-Smb New-Alias -Name dsmb -Value Disable-Smb New-Alias -Name esmb -Value Enable-Smb |