2016-07-10 1 views
1

J'ai vu un certain nombre de solutions, mais aucune n'aborde mon problème. J'essaie d'importer un module PowerShell personnalisé appelé "DSInternals" sur ma DLL C#.Importer le module PowerShell de C# Fail

https://github.com/MichaelGrafnetter/DSInternals

Tout dans mon code semble très bien, mais lorsque je tente d'obtenir le module disponible, il est pas chargé.

Le flux répond avec

Le terme « Get-ADReplAccount » est pas reconnu comme le nom d'une applet de commande, fonction, fichier de script ou d'un programme opérationnel. Vérifiez l'orthographe du nom ou, si un chemin a été inclus, vérifiez que le chemin est correct et réessayez.

Où est-ce que je me trompe avec ce code?

InitialSessionState init = InitialSessionState.CreateDefault(); 
init.ImportPSModule(new string[] { @"D:\\DSInternals\\dsinternals.psd1" }); //location of the module files 
Runspace runspace = RunspaceFactory.CreateRunspace(init); 
runspace.Open(); 
PowerShell ps = PowerShell.Create(); 
ps.Runspace = runspace; 
ps.Commands.AddCommand("Get-ADReplAccount"); //this command is un-recognized 

foreach (PSObject result in ps.Invoke()) 
{ 
    Console.WriteLine(result); //this always returns null 
} 
+0

* cette commande est un reconnu * Comment savez-vous cela? Toute exception levée? Une erreur dans 'ps.Streams.Error'? – PetSerAl

+0

Les flux répondent par "Le terme" Get-ADReplAccount "n'est pas reconnu comme le nom d'une applet de commande, d'une fonction, d'un fichier script ou d'un programme exécutable.Vérifiez l'orthographe du nom ou vérifiez que le chemin est correct et réessayez. ". Ces cmdlets sont incluses par le module DSInternals –

+0

Ajoutez ceci à votre code: 'init.ThrowOnRunspaceOpenError = true;'. – PetSerAl

Répondre

0

La question était la version du framework .NET dans lequel le module a été intégré. Ajout d'un module qui a été construit avec une version supérieure du framework .NET à la classe C# ne fonctionnera pas. Le module a été construit en 4.5.1 et je travaillais avec la version 2, en ajoutant

init.ThrowOnRunspaceOpenError=true; 

aidé à attraper la cause de l'erreur.

Voici mon code final qui fonctionne

 InitialSessionState init = InitialSessionState.CreateDefault(); 
     init.ImportPSModule(new string[] { @"D:\\DSInternals\\dsinternals.psd1" }); //location of the module files 
     init.ThrowOnRunspaceOpenError = true; 
     Runspace runspace = RunspaceFactory.CreateRunspace(init); 
     runspace.Open(); 
     var script = 
      "Get-ADReplAccount -SamAccountName peter -Domain BLABLA -Server dc.BLABLA.co.za -Credential $cred -Protocol TCP"; //my powershell script 

     _powershell = PowerShell.Create().AddScript(script); 
     _powershell.Runspace = runspace; 

     var results = _powershell.Invoke(); 
     foreach (var errorRecord in _powershell.Streams.Progress) 
      Console.WriteLine(errorRecord); 
     foreach (var errorRecord in _powershell.Streams.Debug) 
      Console.WriteLine(errorRecord); 
     foreach (var errorRecord in _powershell.Streams.Error) 
      Console.WriteLine(errorRecord); 
     foreach (var errorRecord in _powershell.Streams.Warning) 
      Console.WriteLine(errorRecord); 

     var stringBuilder = new StringBuilder(); 
     foreach (var obj in results) 
     { 
      stringBuilder.AppendLine(obj.ToString()); 
     } 

     return stringBuilder.ToString();