如何使用远程PowerShell创建本地Windows用户帐户? [英] How to create a local windows user account using remote powershell?

查看:45
本文介绍了如何使用远程PowerShell创建本地Windows用户帐户?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

尝试使用 powershel 创建用户.这对本地机器工作正常.但是如何使用远程 powershell 在远程机器上创建本地用户帐户?

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?

脚本localwindows.ps1是

The script localwindows.ps1 is

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

我通过 C# 尝试了同样的事情:

I tried the same thing through 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".

This also works fine locally .But for remote computer its throwing exception "create :Access is denied ".

推荐答案

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

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

脚本是

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

这篇关于如何使用远程PowerShell创建本地Windows用户帐户?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆