Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/powershell/11.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
在Powershell中使用自定义用户文件夹创建本地用户_Powershell_Scripting_Windows Server 2008_Profile_User Accounts - Fatal编程技术网

在Powershell中使用自定义用户文件夹创建本地用户

在Powershell中使用自定义用户文件夹创建本地用户,powershell,scripting,windows-server-2008,profile,user-accounts,Powershell,Scripting,Windows Server 2008,Profile,User Accounts,我正在尝试创建一个新的Win 2008 server本地用户,并为该用户分配一个不同的配置文件路径。我不希望Windows生成C:\Users\newUser下的所有文件,而是希望它在d:\customDir\newUser中生成这些文件。有人知道这样做的方法吗 到目前为止,这就是我所拥有的: $users= @("U1","U2","U3") $computer = [ADSI]"WinNT://$env:COMPUTERNAME,computer" $group = [ADSI]"WinNT

我正在尝试创建一个新的Win 2008 server本地用户,并为该用户分配一个不同的配置文件路径。我不希望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*

以下是我根据和使用的方式

结果如下


我能想到的唯一一件事就是在注册表级别对其进行更改。请注意,我不建议你搞乱注册表,但这确实是你想要的-

新配置文件的默认目录由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
希望有帮助

到OP

感谢这篇文章,我一直在想如何让它为我工作:

从用户帐户运行cmd以创建配置文件/文件夹 但我不得不修改最后一行以使其正常工作:

Start-Process cmd /c -Credential $cred -ErrorAction SilentlyContinue -LoadUserProfile
这个很好用

$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

是否可以包含错误?异常调用“put”时带有“2”参数:“来自HRESULT:0x8000500F的异常”位于第1行char:1+$user.put(“profilePath”,“d:\CustomDir\U1”)+~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~类别信息:未指定:(:)[],MethodInvocationException+FullyQualifiedErrorId:CatchFromBaseAdapterMethodInvokeTIThis相当于我的脚本执行此操作:
$user.put(“Profile”、$userPath)
$user.put(“HomeDirectory”、$userPath)
。HomeDirectory显示在用户的属性上,但是当windows创建配置文件文件夹时,它仍然将它们放在c:\Users\$userName下,而不是$userPathThis Completypy normal下。即使使用roming配置文件,它也存在一个本地配置文件,但是当用户关闭会话时,该配置文件会被复制到远程位置。同意,这就是我最终要做的,尽管这感觉像是一次黑客攻击。希望有一个更优雅的方式来做这件事。同意!切碎注册表一点也不优雅,太糟糕了,powershell没有像处理广告那样处理本地用户的首选项。不确定这对您的情况是否有帮助,但我确实注意到了这一点。祝你好运这这是一百万次了。对于任何试图让ssh在基于windows云的部署上工作并发现git无法找出~所在位置的人。
$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