【发布时间】:2014-12-22 13:10:35
【问题描述】:
尝试使用 powershel 创建用户。这对本地机器效果很好。但是如何使用远程 powershell 在远程机器上创建本地用户帐户?
脚本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脚本吗
-
我已经用powershell脚本编辑了内容。
标签: windows powershell powershell-remoting