我们正在尝试使用C#从远程计算机设置用户的登录脚本。但是,我们收到错误“术语'Set-ADUser'未被识别为cmdlet,函数,脚本文件或可操作程序的名称。检查名称的拼写,或者如果包含路径,请验证路径是否正确,然后重试。“您对如何解决此错误有任何疑问吗?
using System;
using System.Security;
using System.Management.Automation;
using System.Management.Automation.Runspaces;
namespace PowershellAdUser
{
class PowershellAdUser
{
static void Main(string[] args)
{
string runasUsername = @"login";
string runasPassword = "pass1234";
SecureString ssRunasPassword = new SecureString();
foreach (char x in runasPassword)
ssRunasPassword.AppendChar(x);
PSCredential credentials =
new PSCredential(runasUsername, ssRunasPassword);
var connInfo = new WSManConnectionInfo(
new Uri("http://1.2.3.4/PowerShell"),
"http://schemas.microsoft.com/powershell/Microsoft.Exchange",
credentials);
connInfo.AuthenticationMechanism =
AuthenticationMechanism.Basic;
var runspace = RunspaceFactory.CreateRunspace(connInfo);
runspace.Open();
var pipeline = runspace.CreatePipeline();
var command = new Command("Set-ADUser");
command.Parameters.Add("ScriptPath", "logonScript.bat");
command.Parameters.Add("Identity", "test.com/Users/Test User");
pipeline.Commands.Add(command);
var results = pipeline.Invoke();
runspace.Dispose();
}
}
}
我们也尝试添加
var command = new Command("Import-Module activedirectory");
pipeline.Commands.Add(command);
之后
var pipeline = runspace.CreatePipeline();
这是我们添加时的结果
“术语'Import-Module activedirectory'不会被识别为cmdlet,函数,脚本文件或可操作程序的名称。检查名称的拼写,或者如果包含路径,请验证路径是否正确,然后重试。“
在完成所有操作后,我们尝试同时传递连接信息和初始会话状态,以绕过先前的“导入模块未识别”错误。但是,似乎函数RunspaceFactory.CreateRunspace将采用WSManConnectionInfo对象或InitialSessionState对象,但不是两者。我们还尝试在创建运行空间后设置初始会话状态,但Runspace的InitialSessionState成员似乎是私有的。有没有办法同时使用WSManConnectionInfo对象或InitialSessionState对象初始化运行空间?
using System;
using System.DirectoryServices;
using System.Security;
using System.Management.Automation;
using System.Management.Automation.Runspaces;
namespace test
{
class Program
{
static void Main(string[] args)
{
var target = "servername";
var user = "login";
user = string.Format("{0}\\{1}", target, user);
string shell = "http://schemas.microsoft.com/powershell/Microsoft.PowerShell";
var targetWsMan = new Uri(string.Format("http://{0}:5985/wsman", target));
var password = "pass1234";
var ssPassword = new SecureString();
foreach (char c in password)
{
ssPassword.AppendChar(c);
}
var cred = new PSCredential(user, ssPassword);
var connectionInfo = new WSManConnectionInfo(targetWsMan, shell, cred);
InitialSessionState init_state = InitialSessionState.CreateDefault();
init_state.ImportPSModule(new[] { "ActiveDirectory" });
using (var runSpace = RunspaceFactory.CreateRunspace(connectionInfo))
{
runSpace.InitialSessionState = init_state;
var p = runSpace.CreatePipeline();
runSpace.Open();
var command = new Command("Set-ADUser");
command.Parameters.Add("ScriptPath", "logonScript.bat");
command.Parameters.Add("Identity", "test.com/Users/Test760 Blah760");
p.Commands.Add(command);
var returnValue = p.Invoke();
foreach (var v in returnValue)
Console.WriteLine(v.ToString());
}
Console.WriteLine("Press any key to exit.");
Console.ReadKey();
}
}
}
此外,我们还尝试使用“dsadd”命令而不是“Set-ADUser”命令。如果我们在没有任何参数的情况下调用“dsadd”,它将返回其帮助信息。但是,如果我们尝试传递任何参数,它不会抛出任何错误,但它似乎也不会执行命令。有谁知道如何从Pipeline对象调用“dsadd”命令?
using (var runSpace = RunspaceFactory.CreateRunspace(connectionInfo))
{
runSpace.InitialSessionState = init_state;
var p = runSpace.CreatePipeline();
runSpace.Open();
Command cmd = new Command("dsadd");
cmd.Parameters.Add("ou", "\"OU=test5,OU=Users,DC=test,DC=com\"");
var returnValue = p.Invoke();
foreach (var v in returnValue)
Console.WriteLine(v.ToString());
}
其他信息:术语“Set-ADUser”未被识别为cmdlet,函数,脚本文件或可操作程序的名称。检查名称的拼写,或者如果包含路径,请验证路径是否正确,然后重试。
var p = runSpace.CreatePipeline();
runSpace.Open();
Command com1 = new Command("Import-Module");
com1.Parameters.Add("Name", "ActiveDirectory");
p.Commands.Add(com1);
Command command = new Command("Set-ADUser");
command.Parameters.Add("Identity", "tuser19");
command.Parameters.Add("ScriptPath", "logonScript.bat");
p.Commands.Add(command);
var returnValue = p.Invoke();
答案 0 :(得分:0)
再次尝试Import-Module
命令,但不要将参数与命令名混合,即将参数分开并通过Parameters集合添加:
var command = new Command('Import-Module').Parameters.Add('Name', 'ActiveDirectory');
此外,请确保ActiveDirectory模块位于远程计算机上。如果该模块仅加载到特定位数控制台(32位或64位),请确保使用相应的远程端点。
答案 1 :(得分:0)
试试这段代码。它对我有用
InitialSessionState iss = InitialSessionState.CreateDefault();
iss.ImportPSModule(new String[] { @"<Module name or module path>" });
using (Runspace runspace = RunspaceFactory.CreateRunspace(iss))
{
}