嘗試使用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