2009-12-31 22 views
0

我需要編寫一個程序,該程序將採用現有的本地windows用戶,在其環境選項卡中更改「在登錄字段中啓動以下程序」並將密碼更改爲新密碼。從c#中更改本地用戶設置

我使用的是本地用戶而不是AD,System.DirectoryServices會工作嗎?還有關於使用System.DirectoryServices的任何教程都會非常有幫助。

編輯 - 好的,所以我可以找到現有的用戶,我知道如何更新它。然而我不知道該把什麼放進去。什麼AD屬性表示在用戶設置頁面的環境選項卡上的「在登錄字段中啓動以下程序」?

 DirectorySearcher search = new DirectorySearcher(); 
     search.Filter = String.Format("(SAMAccountName={0})", prams.user); 
     search.PropertiesToLoad.Add("cn"); 
     SearchResult result = search.FindOne(); 
     if (result == null) 
     { 
      Console.Error.WriteLine("No user of name {0} in the system", prams.user); 
      return -2; 
     } 
     DirectoryEntry entry = result.GetDirectoryEntry(); 
     entry.InvokeSet("???", new object[] { newProgramLognField }); 

編輯兩個--- 我現在知道,我需要的字段是「參數」不過,我這個詞是很難搜索在MSDN,因爲它是這樣一個通用術語。我想我有正確的,因爲當我運行的代碼

DirectoryEntry entry = new DirectoryEntry("WinNT://testComputer/testUser"); 
Console.WriteLine("connected"); 
string value = (string)entry.Properties["Parameters"].Value; 
Console.WriteLine(value); 

我得到的結果

P☺CtxCfgPresent????☺CtxCfgFlags1 
????☺CtxShadow????*☻☺CtxMinEncryptionLevel? @☺CtxWorkDirectory?????????????????? 
??????????????"C☺CtxInitialProgram?????????????????????????????????????????????? 
???????????????????????????????????????????????????????????????????????????????? 
???????? 

我猜測InitalProgram部分接近我想要的,最有可能的???之後是現有的路徑,但已被刪除。

現在的問題是我如何正確寫入它?

編輯-3 我嘗試改變從一個字符串的鑄件。然而PropertyValueCollection只是給了我在運行時錯誤

Unhandled Exception: System.InvalidCastException: Unable to cast object of type 
'System.String' to type 'System.DirectoryServices.PropertyValueCollection'. 
at UserUpdater.Program.Main(String[] args) in e:\Visual Studio 2008\Projects\ 
UserUpdater\UserUpdater\Program.cs:line 91 
+4

提醒我在完成後不要運行它。 :) – 2009-12-31 19:41:44

回答

0

我的TechNet Script Repository的副本說,這WSH腳本可以用來更改密碼。

Set objComputer = GetObject _ 
("LDAP://CN=atl-dc-01,CN=Computers,DC=Reskit,DC=COM") 
objComputer.SetPassword "whatever"