2014-02-06 28 views
2

我花了整天尋找答案,但沒有運氣。以編程方式在Windows中禁用密碼複雜度

我需要能夠在獨立Windows 7 PC上禁用本地安全策略中的密碼複雜性。

我已經嘗試使用secedit.exe腳本。我也弄亂了C#。

最終結果應該是一個腳本/程序,它將禁用該策略,然後在本地創建一個新的用戶帳戶。

回答

2

經過一些擴展研究後,我發現如何去做。 在這裏發佈代碼以防其他人需要使用它。

string tempFile = Path.GetTempFileName(); 
Process p = new Process(); 
p.StartInfo.FileName = Environment.ExpandEnvironmentVariables(@"%SystemRoot%\system32\secedit.exe"); 
p.StartInfo.Arguments = String.Format("/export /cfg \"{0}\" /quiet", tempFile); 
p.StartInfo.CreateNoWindow = true; 
p.StartInfo.UseShellExecute = false; 
p.Start(); 
p.WaitForExit(); 

StringBuilder newCfg = new StringBuilder(); 
string[] cfg = File.ReadAllLines(tempFile); 

foreach (string line in cfg) 
{ 
    if (line.Contains("PasswordComplexity")) 
    { 
     newCfg.AppendLine(line.Replace("1", "0")); 
     continue; 
    } 
    newCfg.AppendLine(line); 
} 
File.WriteAllText(tempFile, newCfg.ToString()); 

Process p2 = new Process(); 
p2.StartInfo.FileName = Environment.ExpandEnvironmentVariables(@"%SystemRoot%\system32\secedit.exe"); 
p2.StartInfo.Arguments = String.Format("/configure /db secedit.sdb /cfg \"{0}\" /quiet", tempFile); 
p2.StartInfo.CreateNoWindow = true; 
p2.StartInfo.UseShellExecute = false; 
p2.Start(); 
p2.WaitForExit(); 
相關問題