2012-05-24 83 views
2

我試圖用powershell創建新用戶。我們沒有運行活動目錄(不知道是否改變了事情)。這是我想要做的:無法在PowerShell中使用ADSI設置屬性

$machine = [ADSI]"WinNT://localhost,computer" 
$newUser = $machine.Create("User", $Username) 
$newUser.setpassword($Password) 
$newUser.SetInfo() 

一切工作到這一點,並創建用戶。但現在我想改變這樣的其他設置,但他們都失敗

$newUser.Put("sAMAcountName", $Username) 
$newUser.SetInfo() 

$newUser.Put("userAccountControl", 0x10000) 
$newUser.SetInfo() 

UPDATE

這是我得到

Exception calling "Put" with "2" argument(s): "Exception from HRESULT: 0x8000500F" 

任何想法,我是什麼樣的錯誤做錯了?謝謝!

解決方案

JPBlanc的回答幫我指出了正確的方向。

最大的問題是,在不屬於Active Directory域的機器上使用[ADSI]幾乎沒有任何文檔。

我能夠使用UserFlags屬性解決問題。

$newUser.UserFlags = $UserFlags.DONT_EXPIRE_PASSWD 
$newUser.CommitChanges() 

回答

0

你得到的錯誤信息是什麼?您可能需要爲用戶獲取新的參考,然後才能再次修改它。

3

你可以嘗試以管理員身份:

$obj = [ADSI]"WinNT://$env:COMPUTERNAME" 
$user = $obj.Children.find("utilisateur1") 
$user.psbase.rename("user1") 
$user.put('FullName','user1') 
$user.setinfo() 

按照跟隨着代碼我不能看到sAMAcountNameuserAccountControl這是AD用戶屬性:

PS C:\Windows\system32> $a | fl * 


UserFlags     : {513} 
MaxStorage     : {-1} 
PasswordAge    : {917} 
PasswordExpired   : {0} 
LoginHours     : {255 255 255 255 255 255 255 255 255 255 255 255 255 255 255 255 255 255 255 255 255} 
FullName     : {user1} 
Description    : {} 
BadPasswordAttempts  : {0} 
HomeDirectory    : {} 
LoginScript    : {} 
Profile     : {} 
HomeDirDrive    : {} 
Parameters     : {} 
PrimaryGroupID    : {513} 
Name      : {user1} 
MinPasswordLength   : {0} 
MaxPasswordAge    : {3628800} 
MinPasswordAge    : {0} 
PasswordHistoryLength  : {0} 
AutoUnlockInterval   : {1800} 
LockoutObservationInterval : {1800} 
MaxBadPasswordsAllowed  : {0} 
objectSid     : {1 5 0 0 0 0 0 5 21 0 0 0 151 181 85 95 2 227 17 190 248 24 47 102 18 4 0 0} 
AuthenticationType   : Secure 
Children     : {} 
Guid      : {D83F1060-1E71-11CF-B1F3-02608C9E7553} 
ObjectSecurity    : 
NativeGuid     : {D83F1060-1E71-11CF-B1F3-02608C9E7553} 
NativeObject    : System.__ComObject 
Parent      : WinNT://WORKGROUP/JPBHPP2 
Password     : 
Path      : WinNT://WORKGROUP/JPBHPP2/user1 
Properties     : {UserFlags, MaxStorage, PasswordAge, PasswordExpired...} 
SchemaClassName   : User 
SchemaEntry    : System.DirectoryServices.DirectoryEntry 
UsePropertyCache   : True 
Username     : 
Options     : 
Site      : 
Container     : 


PS C:\Windows\system32> $a | select -ExpandProperty properties 

PropertyName              Value 
------------              ----- 
UserFlags               513 
MaxStorage               -1 
PasswordAge              917 
PasswordExpired              0 
LoginHours          {255, 255, 255, 255...} 
FullName               user1 
Description 
BadPasswordAttempts             0 
HomeDirectory 
LoginScript 
Profile 
HomeDirDrive 
Parameters 
PrimaryGroupID              513 
Name                user1 
MinPasswordLength             0 
MaxPasswordAge             3628800 
MinPasswordAge              0 
PasswordHistoryLength            0 
AutoUnlockInterval            1800 
LockoutObservationInterval          1800 
MaxBadPasswordsAllowed            0 
objectSid            {1, 5, 0, 0...}