2

我試圖創建一個新的Win 2008服務器本地用戶併爲用戶分配不同的配置文件路徑。我不想讓Windows生成C:\ Users \ newUser下的所有文件,相反,我想讓它在D:\ customDir \ newUser中執行。有誰知道一種方法來做到這一點?使用Powershell中的自定義用戶文件夾創建本地用戶

到目前爲止,這是我:

$users= @("U1","U2","U3") 
$computer = [ADSI]"WinNT://$env:COMPUTERNAME,computer" 
$group = [ADSI]"WinNT://$env:COMPUTERNAME/MyCustomGroup,group" 
$users | foreach { 
    $userName = $_ 
    $userPath = "D:\customDir\$userName" 
    echo "Creating $userName..." 
    $user = $computer.Create("User",$userName) 
    $user.put("description","User $userName Description") 
    #$user.put("profilePath",$userPath) #This does not work, throws an error 
    #$user.put("homeDirDrive","D:") #This appears to be ignored when uncommented 
    $user.setpassword($userName) 
    $user.setInfo() 

    $group.add($user.Path) 

    #run cmd from user account to create profile files/folders 
    $spw = ConvertTo-SecureString $userName -AsPlainText -Force 
    $cred = New-Object System.Management.Automation.PSCredential -ArgumentList $userName,$spw 
    Start-Process cmd /c -WindowStyle Hidden -Credential $cred -ErrorAction SilentlyContinue 

} 

腳本成功創建用戶,並將其添加到自定義組,但所有的文件/文件夾用C結束:\用戶\ U *

+0

您能否包含錯誤? – 2013-05-08 16:38:54

+0

使用「2」參數調用「put」的異常:「來自HRESULT的異常:0x8000500F」 在行:1 char:1 + $ user.put(「profilePath」,「d:\ CustomDir \ U1」) + ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ + CategoryInfo:NotSpecified:(:) [] ,MethodInvocationException + FullyQualifiedErrorId:CatchFromBaseAdapterMethodInvokeTI – Farlan 2013-05-08 17:00:37

回答

0

我能想到的,你會在註冊表水平來改變它的唯一的事情。請注意,我並不建議您混淆註冊表,但這確實會做你想做的 -

新配置文件的默認目錄由HKEY_LOCAL_MACHINE \ SOFTWARE \ Microsoft \ Windows NT \ CurrentVersion \ ProfileList,默認情況下是%SystemDrive%\ Users,將其更改爲$ userpath應該做你想做的 -

$regpath = "HKLM:\Software\Microsoft\Windows NT\CurrentVersion\ProfileList" 
$regname = "ProfilesDirectory" 

set-itemproperty -path $regpath -name $regname -value $userpath 

希望幫助!

+0

同意,這是我最終做的,儘管它真的感覺像一個黑客。希望有一個更優雅的方式來做到這一點。 – Farlan 2013-05-10 14:58:30

+0

同意!沒有任何關於砍掉註冊表的優雅,太糟糕的PowerShell不處理本地用戶喜好,就像AD。不知道這是否有助於你的情況,但我注意到這[模塊](http://gallery.technet.microsoft.com/scriptcenter/Local-Account-Management-a777191b)。祝你好運! – 2013-05-10 16:45:21

0

以下是我根據underlaying COM component object modelIADsUser interface使用的方式。

$obj = [ADSI]"WinNT://$env:COMPUTERNAME" 
$user1=$obj.create("user", "utilisateur1") 
$user1.PasswordExpired=0 
$user1.Invoke("setpassword","test.2012") 
$user1.Invoke("put", "HomeDirectory", "c:\silogix") 
$user1.Invoke("put", "Profile", "c:\docs") 
$user1.Invoke("put", "LoginScript", "test.cmd") 
$user1.CommitChanges() 

這是結果。

enter image description here

+0

這相當於我的腳本執行此操作:'$ user.put(「Profile」,$ userPath)''和'$ user.put(「HomeDirectory」,$ userPath)''。HomeDirectory出現在用戶的屬性上,但是當Windows創建配置文件文件夾時,它仍然將它們放在c:\ Users \ $ userName下,而不是$ userPath – Farlan 2013-05-09 18:27:26

+0

這完全正常。即使使用roming配置文件,它也存在本地配置文件,但是當用戶關閉他的會話時,該配置文件將複製到遠程位置。 – JPBlanc 2013-05-09 19:08:35

2

到OP,

感謝這一塊,我試圖找出如何使這對我來說工作:從用戶帳戶

運行CMD創建配置文件的文件/文件夾

$spw = ConvertTo-SecureString $userName -AsPlainText -Force 
$cred = New-Object System.Management.Automation.PSCredential -ArgumentList $userName,$spw 
Start-Process cmd /c -WindowStyle Hidden -Credential $cred -ErrorAction SilentlyContinue 

,但我不得不修改的最後一行,以得到它的工作:

Start-Process cmd /c -Credential $cred -ErrorAction SilentlyContinue -LoadUserProfile 
0

該作品罰款。

$loginName="ChrisMcCormack" 
$newPass="123456ABCDEFG" # obviously generate a proper password... 
$desc="average user" 
$localHost=[ADSI]"WinNT://localhost" 
$newUser=$localHost.Create("user",$loginName); 
$newUser.setPassword($newPass); 
$newUser.put("HomeDirectory","c:\users\$loginName"); 
$newUser.put("description",$desc);   
$newUser.setInfo(); 

您還可以使用:

$spw = ConvertTo-SecureString $newPass -AsPlainText -Force 
$cred = New-Object System.Management.Automation.PSCredential -ArgumentList $loginName,$spw 
Start-Process cmd /c -WindowStyle Hidden -Credential $cred -ErrorAction SilentlyContinue 

無需登錄的用戶創建的用戶目錄。

Chris

相關問題