如何用PowerShell自动化配置Windows高级共享的所有网络设置?
1. Yes, PowerShell scripts exist for these settings
All three "All Networks" sharing settings can be configured by modifying Windows Registry keys via PowerShell. Below are the scripts for each requirement, plus guidance on applying them and integrating with your C# program.
2. Step-by-Step Configuration Scripts
All commands must be run as Administrator to modify system-level registry keys and services.
2.1 Enable Public Folder Sharing (Read/Write for All Network Users)
This setting grants full read/write access to Windows Public folders for any network visitor:
# Set Public folder sharing to read/write mode Set-ItemProperty -Path "HKLM:\SOFTWARE\Microsoft\Windows\CurrentVersion\Explorer\Advanced\Folder\Sharing\Public" -Name "Value" -Value 2 # Enable automatic sharing of Public folders Set-ItemProperty -Path "HKLM:\SYSTEM\CurrentControlSet\Services\LanmanServer\Parameters" -Name "AutoShareWks" -Value 1
2.2 Enable File Sharing for 40/56-bit Encryption Devices
This allows older devices using weaker encryption standards to connect to your file shares:
# Set minimum encryption level to support 40/56-bit devices Set-ItemProperty -Path "HKLM:\SYSTEM\CurrentControlSet\Services\LanmanServer\Parameters" -Name "MinEncryptionLevel" -Value 1
2.3 Turn Off Password-Protected Sharing
This removes the requirement for network users to enter a Windows account password to access shared resources:
# Disable password-protected sharing Set-ItemProperty -Path "HKLM:\SOFTWARE\Microsoft\Windows\CurrentVersion\Policies\Network" -Name "DisablePasswordProtectedSharing" -Value 1
2.4 Apply Changes Immediately
Restart the Server service to activate the new settings without a full system reboot:
Restart-Service -Name LanmanServer -Force
3. Full Combined Script
Here's a complete script that handles all three settings and applies changes in one go:
# Require Administrator privileges to run #Requires -RunAsAdministrator # 1. Configure Public Folder Sharing (Read/Write) Set-ItemProperty -Path "HKLM:\SOFTWARE\Microsoft\Windows\CurrentVersion\Explorer\Advanced\Folder\Sharing\Public" -Name "Value" -Value 2 -ErrorAction Stop Set-ItemProperty -Path "HKLM:\SYSTEM\CurrentControlSet\Services\LanmanServer\Parameters" -Name "AutoShareWks" -Value 1 -ErrorAction Stop # 2. Enable 40/56-bit Encryption Support Set-ItemProperty -Path "HKLM:\SYSTEM\CurrentControlSet\Services\LanmanServer\Parameters" -Name "MinEncryptionLevel" -Value 1 -ErrorAction Stop # 3. Disable Password-Protected Sharing Set-ItemProperty -Path "HKLM:\SOFTWARE\Microsoft\Windows\CurrentVersion\Policies\Network" -Name "DisablePasswordProtectedSharing" -Value 1 -ErrorAction Stop # Restart service to apply changes Restart-Service -Name LanmanServer -Force -ErrorAction Stop Write-Host "All network sharing settings configured successfully!" -ForegroundColor Green
4. Integrate with Your C# Program
To run this script from your C# application, use the System.Management.Automation namespace. Follow these steps:
- Add the NuGet package
System.Management.Automationto your project. - Use this code snippet to execute the script:
using System.Management.Automation; public void ConfigureNetworkSharingSettings() { string powerShellScript = @" #Requires -RunAsAdministrator Set-ItemProperty -Path 'HKLM:\SOFTWARE\Microsoft\Windows\CurrentVersion\Explorer\Advanced\Folder\Sharing\Public' -Name 'Value' -Value 2 -ErrorAction Stop Set-ItemProperty -Path 'HKLM:\SYSTEM\CurrentControlSet\Services\LanmanServer\Parameters' -Name 'AutoShareWks' -Value 1 -ErrorAction Stop Set-ItemProperty -Path 'HKLM:\SYSTEM\CurrentControlSet\Services\LanmanServer\Parameters' -Name 'MinEncryptionLevel' -Value 1 -ErrorAction Stop Set-ItemProperty -Path 'HKLM:\SOFTWARE\Microsoft\Windows\CurrentVersion\Policies\Network' -Name 'DisablePasswordProtectedSharing' -Value 1 -ErrorAction Stop Restart-Service -Name LanmanServer -Force -ErrorAction Stop "; using (PowerShell psInstance = PowerShell.Create()) { psInstance.AddScript(powerShellScript); try { var executionResults = psInstance.Invoke(); if (psInstance.HadErrors) { foreach (var errorRecord in psInstance.Streams.Error) { // Log or display error details Console.WriteLine($"Configuration Error: {errorRecord.ToString()}"); } } else { Console.WriteLine("Network sharing settings applied successfully."); } } catch (Exception ex) { Console.WriteLine($"Execution Exception: {ex.Message}"); } } }
Key Notes
- Your C# application must run as Administrator to modify system registry keys and restart services.
- Test the script on a non-production device first to confirm behavior matches your requirements.
- In rare cases, a system reboot may be needed if service restart doesn't apply changes (though the script avoids this where possible).
内容的提问来源于stack exchange,提问作者Hatterene

