windows - 如何使用远程 powershell 创建本地 Windows 用户帐户?

标签 windows powershell powershell-remoting

尝试使用 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();
              }  

这在本地也能正常工作。但对于远程计算机,它会抛出异常“create :Access is denied”。

最佳答案

我能够使用以下命令在远程计算机上创建本地用户帐户:

Invoke-Command -ComputerName machineName -filepath c:\script.ps1 -credential  $getcredential

脚本是

$comp = [adsi]'WinNT://localhost,computer';
$user = $comp.Create('User', 'account11');
$user.SetPassword('change,password.10');
$user.SetInfo();
$user

关于windows - 如何使用远程 powershell 创建本地 Windows 用户帐户?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20186677/

相关文章:

windows - 包安装报错 Microsoft.NetCore.UniversalWindowsPlatform.5.0.0

c - 错误 LNK2001 : unresolved reference with Pro*C code with Visual C++

powershell - 如何永久使用 Powershell 为可执行文件设置别名?

c# - Azure Keyvault - "Operation "列表“保管库策略不允许”,但已检查所有权限

python - 如何在 Windows 上调试 Python 的 C 扩展

c# - 如果多个应用程序实例正在运行,如何获取应用程序的事件/前台实例?

azure - 运行命令扩展正在执行。请等待完成后再调用运行命令

Powershell 脚本在远程执行期间失败,但在本地工作

c# - 在 C# 中检查远程 PowerShell session 状态

powershell - Winrm 在 Windows Azure Windows VM 上不可用