Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/powershell/13.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
Windows 使用powershell为用户创建主目录时出错_Windows_Powershell_User Accounts - Fatal编程技术网

Windows 使用powershell为用户创建主目录时出错

Windows 使用powershell为用户创建主目录时出错,windows,powershell,user-accounts,Windows,Powershell,User Accounts,我认为下面是为用户创建主目录的建议。因此,我尝试了以下脚本来创建主目录: $sec_password = ConvertTo-SecureString 'Password' -AsPlainText -Force; $credential = New-Object System.Management.Automation.PSCredential -ArgumentList 'BLR2202710\Administrator', $sec_password; Start-Process cmd

我认为下面是为用户创建主目录的建议。因此,我尝试了以下脚本来创建主目录:

 $sec_password = ConvertTo-SecureString 'Password' -AsPlainText -Force;
$credential = New-Object System.Management.Automation.PSCredential -ArgumentList 'BLR2202710\Administrator', $sec_password;
Start-Process cmd /c -WindowStyle Hidden -Credential $credential -ErrorAction SilentlyContinue;  
$user = Get-WmiObject -Namespace root/cimv2 -Class win32_useraccount -Filter "LocalAccount=True AND Name='account17'";
$userprofile = Get-WmiObject -Namespace root/cimv2 -Class win32_userprofile -Filter "SID='$($user.sid)' ";
$userprofile.localpath;
但它失败了,但有以下例外: '无法使用指定的命名参数解析参数集。 +CategoryInfo:InvalidArgument::[Start Process],参数BindingException +FullyQualifiedErrorId:含糊不清的USParameterSet,Microsoft.PowerShell.Commands.StartProcessCommand'

我正在处理这个问题

Start-Process cmd /c -WindowStyle Hidden -Wait -Credential $credential    
    -ErrorAction SilentlyContinue;  

我没有看到你的代码在任何地方创建主目录。在PowerShell v2上引发错误的行在v3上工作,它只会生成并立即终止CMD进程。@AnsgarWiechers:如果CMD进程是在目标用户的上下文中启动的,这可能足以迫使Windows创建配置文件主目录。我不确定。@HarryJohnston提供了显式凭据,但该进程应在用户BLR2202710\管理员的上下文中运行。@AnsgarWiechers:很好地发现,这可能是OP的问题。我尝试了上面的脚本,并做了修改:Start process cmd-Credential$Credential-ErrorAction SilentlyContinue;我收到错误:“由于错误,无法执行此命令:访问被拒绝。”创建主目录需要哪些权限?