windows 如何使用远程PowerShell创建本地Windows用户帐户?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/20186677/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How to create a local windows user account using remote powershell?
提问by cmm user
tried creating users with powershel.This worked fine for local machine. But how to create a local user account in a remote machine using remote powershell?
尝试使用 powershel 创建用户。这适用于本地机器。但是如何使用远程 powershell 在远程机器上创建本地用户帐户?
The script localwindows.ps1 is
脚本 localwindows.ps1 是
$comp = [adsi]'WinNT://machinename,computer';
$user = $comp.Create('User', 'account4');
$user.SetPassword('change,password.10');
$user.SetInfo();
I tried the same thing through C# :
我通过 C# 尝试了同样的事情:
PSCredential credential = new PSCredential(userName, securePassword);
WSManConnectionInfo connectionInfo = new WSManConnectionInfo(false, "machinename", 5985, "/wsman", shellUri, credential);
using (Runspace runspace = RunspaceFactory.CreateRunspace(connectionInfo))
{
runspace.Open();
String file = "C:\localwindows.ps1";
Pipeline pipeline = runspace.CreatePipeline();
pipeline.Commands.AddScript(System.IO.File.ReadAllText(file));
pipeline.Commands.Add("Out-String");
// execute the script
Collection<PSObject> results = pipeline.Invoke();
}
This also works fine locally .But for remote computer its throwing exception "create :Access is denied ".
这在本地也能正常工作。但对于远程计算机,它会抛出异常“创建:访问被拒绝”。
采纳答案by cmm user
I was able to create a local user account in a remote computer using the following command :
我能够使用以下命令在远程计算机上创建本地用户帐户:
Invoke-Command -ComputerName machineName -filepath c:\script.ps1 -credential $getcredential
The script is
脚本是
$comp = [adsi]'WinNT://localhost,computer';
$user = $comp.Create('User', 'account11');
$user.SetPassword('change,password.10');
$user.SetInfo();
$user
回答by Wesley
Invoke-Command works but you can also use Enter-PSSession -Computer to submit commands locally on a remote machine. The following will prompt the user for the username and add them to the local Administrators group with no password:
Invoke-Command 有效,但您也可以使用 Enter-PSSession -Computer 在远程机器上本地提交命令。以下将提示用户输入用户名并将其添加到本地管理员组,无需密码:
$user = read-host 'What is the name of the local user you would like to add?'
net user /add $user
net localgroup Administrators /add $user
回答by IdoFlatow
I don't know if the question is still relevant, but I have tried this out and found what needs to be fixed. When you create the directory entry object, use the following code
我不知道这个问题是否仍然相关,但我已经尝试过并发现需要解决的问题。创建目录条目对象时,使用以下代码
$objOu = New-Object System.DirectoryServices.DirectoryEntry("WinNT://$computer", $admin, $adminPass, "Secure")
The rest is the same.
其余的都是一样的。
回答by Ansgar Wiechers
Use the ADSI WinNT provider:
$username = "foo"
$password = "bar"
$computer = "hostname"
$prov = [adsi]"WinNT://$computer"
$user = $prov.Create("User", $username)
$user.SetPassword($password)
$user.SetInfo()
回答by Snorre
The powershell script invoke-Command executes any powershell script on a remote computer. You didn't say just how you use powershell to create the user, but as an example you write:
powershell 脚本 invoke-Command 在远程计算机上执行任何 powershell 脚本。你没有说你如何使用 powershell 创建用户,但作为一个例子,你写:
invoke-command -computername myserver {[ADSI]$server="WinNT://localhost";$HD=$server.Create("User","HD");$HD.SetPassword("H3lpD3>K");$HD.SetInfo()}
You can also execute your local powershell script remotely by using the -filepath parameter:
您还可以使用 -filepath 参数远程执行本地 powershell 脚本:
Invoke-Command -ComputerName MyRemoteServer -filepath c:\Scripts\DaScript.ps1
To enable remote commands you will have to enable winrm on the remote computer. you can do this by running
要启用远程命令,您必须在远程计算机上启用 winrm。你可以通过运行来做到这一点
winrm quickconfig
On the remote computer.
在远程计算机上。