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

相关问题