3

我正在尝试创建一个新的 Win 2008 服务器本地用户并为该用户分配不同的配置文件路径。我不希望 Windows 生成 C:\Users\newUser 下的所有文件,相反,我想让它在 D:\customDir\newUser 中执行此操作。有谁知道这样做的方法?

到目前为止,这就是我所拥有的:

$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:\Users\U*

4

4 回答 4

5

对 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
于 2013-10-09T21:00:15.100 回答
1

这工作正常。

$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

在不登录用户的情况下创建用户目录。

克里斯

于 2014-01-23T11:07:17.443 回答
0

这是我根据底层 COM 组件对象模型IADsUser 接口使用的方式。

$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()

这是结果。

在此处输入图像描述

于 2013-05-08T20:44:54.367 回答
-1

我唯一能想到的就是在注册表级别更改它。请注意,我不建议您弄乱注册表,但这确实可以满足您的要求-

新配置文件的默认目录由 HKLM:\Software\Microsoft\Windows NT\CurrentVersion\ProfileList 中的 ProfilesDirectory 确定,默认为 %SystemDrive%\Users,将其更改为 $userpath 应该可以满足您的要求 -

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

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

希望有帮助!

于 2013-05-08T20:45:09.483 回答