尝试使用powershel创建用户。对于本地计算机,此工作正常。但是,如何使用远程PowerShell在远程计算机上创建本地用户帐户?如何使用远程PowerShell创建本地Windows用户帐户?
脚本localwindows.ps1是
$comp = [adsi]'WinNT://machinename,computer';
$user = $comp.Create('User', 'account4');
$user.SetPassword('change,password.10');
$user.SetInfo();
我通过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();
}
这也适用于本地罚款。但对于远程计算机及其引发异常“创建:访问是否认“。
你可以在这种情况下添加PowerShell脚本 – Chelseawillrecover
我已经使用powershell脚本编辑了内容。 –
http://gallery.technet.microsoft.com/scriptcenter/Add-new-local-administrator-3a962c5e – JohnLBevan