2013-07-15 107 views
2

我們正在嘗試從C#中的遠程計算機設置用戶的登錄腳本。但是,我們收到錯誤「術語'Set-ADUser'未被識別爲cmdlet,函數,腳本文件或可操作程序的名稱。檢查名稱的拼寫,或者如果包含路徑,請驗證路徑是否正確,然後重試。「您對如何解決此錯誤有任何想法嗎?c#遠程PowerShell創建AD/Exchange用戶帳戶並修改

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 pipeline = runspace.CreatePipeline(); 

加入

var command = new Command("Import-Module activedirectory"); 
pipeline.Commands.Add(command); 

這就是我們得到的,當我們添加它

「術語 '導入模塊ActiveDirectory的' 不識別爲cmdlet,函數,腳本文件或可操作程序的名稱。檢查名稱的拼寫,或者如果包含路徑,請驗證路徑是否正確,然後再試一次。「

畢竟沒有工作,我們試圖將連接信息和初始會話狀態傳遞給同時爲了避開前面的'Import-Module not recognized'錯誤。但是,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

再次嘗試Import-Module命令,但不要在參數混用命令名稱參數即分離出來,並通過參數集合添加:

var command = new Command('Import-Module').Parameters.Add('Name', 'ActiveDirectory'); 

此外,確保ActiveDirectory的模塊在遠程機器上。如果該模塊只加載到特定的位控制檯(32位或64位),請確保使用相應的遠程端點。

+0

我們仍然得到「的參數無法找到匹配的參數‘ActiveDirectory的’我們也驗證了我們可以在目標服務器上運行的命令。 – j30a4

+0

我的壞,Parameters.Add()預計,兩個值,參數名稱和參數值 –

+0

我們這:)附加信息:術語'Set-ADUser'不被識別爲cmdlet,函數,腳本文件或可操作程序的名稱。檢查名稱的拼寫,或者如果包含路徑,請驗證路徑是否正確,然後重試。我將代碼添加到主帖子中 – j30a4

0

試試看看這個代碼。它的工作對我來說

 InitialSessionState iss = InitialSessionState.CreateDefault(); 
     iss.ImportPSModule(new String[] { @"<Module name or module path>" }); 


     using (Runspace runspace = RunspaceFactory.CreateRunspace(iss)) 
     { 
     }