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 59 60 61 62 63
| using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using System.Management.Automation;
using System.IO;
namespace TestVMWare
{
/// <summary>
/// Logique d'interaction pour MainWindow.xaml
/// </summary>
public partial class MainWindow : Window
{
public MainWindow()
{
InitializeComponent();
}
public string LanceScriptPowerShell(string monScript)
{
string FichierTemp = "C:\\Windows\\Temp\\RetourPowerShell.txt";
string ScriptEntier = monScript + " > " + FichierTemp;
string MonRetourPowerShell;
try
{
//using System.Management.Automation; pour utiliser les Script PowerShell
// Et ajouter la DLL depuis C:\Program Files (x86)\Reference Assemblies\Microsoft\WindowsPowerShell\3.0
PowerShell powerShell = PowerShell.Create();
powerShell.AddScript(ScriptEntier);
powerShell.Invoke();
}
catch (Exception ex)
{
MonRetourPowerShell = "Le Script n'est pas fonctionnel : " + ex;
return MonRetourPowerShell;
}
//using System.IO; pour Utiliser StreamReader
if (File.Exists(FichierTemp))
{
StreamReader FichierALire = new StreamReader(FichierTemp);
MonRetourPowerShell = FichierALire.ReadToEnd();
FichierALire.Close();
File.Delete(FichierTemp);
}
else
{
MonRetourPowerShell = "Le Script n'est pas fonctionnel";
}
return MonRetourPowerShell;
}
private void BoutonTest1_Click(object sender, RoutedEventArgs e)
{
string monScript, monResultat;
monScript = TxtBoxScipt.Text;
monResultat = LanceScriptPowerShell(monScript);
TxtBoxResultat.Text = monResultat;
}
}
} |
Partager