最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

windows - Configure Network Sharing Options via PowerShell - Stack Overflow

programmeradmin3浏览0评论

I want to modify certain network sharing options using a PowerShell script. Specifically, I need to automate the following actions, accessible via:

Windows + R -> control /name Microsoft.NetworkAndSharingCenter /page Advanced
  1. In the "All Networks" section → "Public folder sharing", select:

    "Turn on sharing so anyone with network access can read and write files in the Public folders"

  2. In the "File sharing connections" section, select:

    "Use 128-bit encryption to help protect file sharing connections (recommended)"

  3. In the "Password protected sharing" section, select:

    "Turn off password protected sharing"

I have developed the following PowerShell script. It runs without any errors, but the desired options are not selected. The script has no effect on the selection of the options. Do you have any idea what might be causing the issue?

# 16. Modify sharing settings
if ($config.Configurations.PartageAvance) {
    Write-Log "16. Configuring advanced sharing settings..."

    try {
        # Enable file and printer sharing for all network profiles
        $networkProfiles = @("Private", "Public", "Domain")
        
        # Use Get-NetFirewallRule to find sharing rules
        $fileAndPrinterSharingRules = Get-NetFirewallRule | Where-Object { 
            $_.DisplayGroup -like "*File and Printer Sharing*" 
        }

        foreach ($rule in $fileAndPrinterSharingRules) {
            Set-NetFirewallRule -Name $rule.Name -Enabled True -Profile $networkProfiles
        }
        
        # Alternative method using netsh
        netsh advfirewall firewall set rule group="File and Printer Sharing" new enable=Yes | Out-Null
        
        # Disable password-protected sharing
        $regPath = "HKLM:\SYSTEM\CurrentControlSet\Control\Lsa"
        Set-ItemProperty -Path $regPath -Name "LmCompatibilityLevel" -Value 1 -Type DWord

        $regPath = "HKLM:\SYSTEM\CurrentControlSet\Services\LanmanWorkstation\Parameters"
        Set-ItemProperty -Path $regPath -Name "RequireSecuritySignature" -Value 0 -Type DWord
        Set-ItemProperty -Path $regPath -Name "EnableSecuritySignature" -Value 0 -Type DWord
        
        $regPath = "HKLM:\SYSTEM\CurrentControlSet\Services\LanmanServer\Parameters"
        Set-ItemProperty -Path $regPath -Name "RequireSecuritySignature" -Value 0 -Type DWord
        Set-ItemProperty -Path $regPath -Name "EnableSecuritySignature" -Value 0 -Type DWord
        Set-ItemProperty -Path $regPath -Name "RestrictNullSessAccess" -Value 0 -Type DWord

        Write-Log "Sharing settings configured" -ForegroundColor Green
    }
    catch {
        Write-Log "Error configuring sharing settings: $_" -ForegroundColor Red
    }
}
else {
    Write-Log "Sharing settings configuration disabled" -ForegroundColor Yellow
}
发布评论

评论列表(0)

  1. 暂无评论