Bonjour,
J'aimerais appeler mes scripts PowerShell via un programme C#
Je sais que cela est possible car j'ai déjà un programme qui appelle mes scripts PS mais je ne peux y mettre que des scripts avec des cmdlet local du style:
script.ps1
Je ne peux pas y mettre des cmdlets qu'on lance sur des sessions distantes..
Code : Sélectionner tout - Visualiser dans une fenêtre à part
1
2 Get-Process
ex:
script1.ps1
import-credential.ps1 ( http://janel.spaces.live.com/blog/cn...88C2!358.entry )
Code : Sélectionner tout - Visualiser dans une fenêtre à part
1
2
3 $cred = D:\import-credential.ps1 D:\pass.txt Invoke-Command { Get-Process } -ComputerName wks0010 -Credential $cred
Code : Sélectionner tout - Visualiser dans une fenêtre à part
1
2
3
4
5 param ($filename) $username,$password = type $filename $pwd = convertto-securestring $password new-object system.management.automation.PSCredential $username,$pwd
Je ne trouve pas comment faire fonctionner le programme C# avec script1.ps1
Voic le prgr C#:
J'espère que j'ai été clair concernant le problème.
Code : Sélectionner tout - Visualiser dans une fenêtre à part
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58 using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Collections.ObjectModel; using System.Management.Automation; using System.Management.Automation.Runspaces; using System.Diagnostics; namespace PowerShellAndCSharp { class Program { static void Main(string[] args) { try { string scriptFile = ".\\script.ps1"; string scriptParameters = ""; RunspaceConfiguration runspaceConfiguration = RunspaceConfiguration.Create(); Runspace runspace = RunspaceFactory.CreateRunspace(runspaceConfiguration); runspace.Open(); Console.WriteLine("runspace open"); RunspaceInvoke scriptInvoker = new RunspaceInvoke(runspace); Pipeline pipeline = runspace.CreatePipeline(); Command scriptCommand = new Command(scriptFile); Console.WriteLine("script ajoute"); Collection<CommandParameter> commandParameters = new Collection<CommandParameter>(); foreach (string scriptParameter in scriptParameters.Split(' ')) { CommandParameter commandParm = new CommandParameter(null, scriptParameter); commandParameters.Add(commandParm); scriptCommand.Parameters.Add(commandParm); } pipeline.Commands.Add(scriptCommand); Collection<PSObject> psObjects = pipeline.Invoke(); runspace.Close(); Console.WriteLine("runspace close"); StringBuilder stringBuilder = new StringBuilder(); foreach (PSObject obj in psObjects) { stringBuilder.AppendLine(obj.ToString()); Console.WriteLine("reponse: " + obj.ToString()); } Console.WriteLine(stringBuilder.ToString()); } catch (Exception ex) { Console.WriteLine("exception: " + ex.Message); } } } }
Vous pouvez toujours essayer de tester le code pour script.ps1, ça fonctionne.
Quelqu'un aurait-il déjà fait ce genre de choses et saurait-il m'aider?
Merci d'avance
Partager