我正在尝试创建一个新的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 *
中答案 0 :(得分:4)
到OP,
感谢这篇文章,我试图弄清楚如何让这项工作为我服务:
$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
答案 1 :(得分: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
创建用户目录而不记录用户。
克里斯
答案 2 :(得分:0)
以下是我根据underlaying COM component object model和IADsUser 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()
结果如下。
答案 3 :(得分:0)
我唯一能想到的就是在注册表级别更改它。请注意,我不建议你弄乱注册表,但这确实做你想要的 -
新配置文件的默认目录由HKLM中的ProfilesDirectory确定:\ 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
希望有所帮助!