254 lines
9.6 KiB
C#
254 lines
9.6 KiB
C#
using System;
|
|
using System.ComponentModel;
|
|
using System.Diagnostics;
|
|
using System.IO;
|
|
using System.Security.Principal;
|
|
using System.Threading;
|
|
using System.Windows;
|
|
using System.Windows.Media;
|
|
|
|
namespace Test
|
|
{
|
|
/// <summary>
|
|
/// Interaction logic for MainWindow.xaml
|
|
/// </summary>
|
|
///
|
|
|
|
public partial class MainWindow : Window
|
|
{
|
|
static String homeDrive = Path.GetPathRoot(Environment.SystemDirectory);
|
|
public String ccmTempPath = $"{homeDrive}temp\\ccmtemp";
|
|
public ProgressWindow pwindow;
|
|
public BackgroundWorker worker = new BackgroundWorker();
|
|
public string[] args = Environment.GetCommandLineArgs();
|
|
public String fileName;
|
|
|
|
public MainWindow()
|
|
{
|
|
InitializeComponent();
|
|
if (args.Length > 1 && args[1] == "-admin")
|
|
{
|
|
pwindow = new ProgressWindow();
|
|
pwindow.Show();
|
|
this.Close();
|
|
StartWorker_UninstallCCM();
|
|
}
|
|
}
|
|
|
|
private void StartWorker_CopyFiles()
|
|
{
|
|
worker = new BackgroundWorker();
|
|
worker.WorkerReportsProgress = true;
|
|
pwindow.ProgressLabel.Content = "Copie des fichiers d'installation du centre logiciel...";
|
|
worker.ProgressChanged += setProgressBarValue;
|
|
worker.RunWorkerCompleted += StartAsAdmin;
|
|
worker.DoWork += CopyCCMFiles;
|
|
worker.RunWorkerAsync();
|
|
}
|
|
|
|
private void CopyCCMFiles(object sender, System.ComponentModel.DoWorkEventArgs e)
|
|
{
|
|
string sourcePath = @"\\ac-brt-fsncy1\Logiciels\ccm\client";
|
|
String[] Directories = Directory.GetDirectories(sourcePath, "*", SearchOption.AllDirectories);
|
|
String[] Files = Directory.GetFiles(sourcePath, "*.*", SearchOption.AllDirectories);
|
|
int DirectoryCount = Directories.Length;
|
|
int FilesCount = Files.Length;
|
|
int progress = 0;
|
|
int progressPerFileOrDir = Convert.ToInt32(100.0 / (DirectoryCount+FilesCount));
|
|
|
|
foreach (string dirPath in Directories)
|
|
{
|
|
Directory.CreateDirectory(dirPath.Replace(sourcePath, ccmTempPath));
|
|
progress += progressPerFileOrDir;
|
|
worker.ReportProgress(progress);
|
|
}
|
|
foreach (string newPath in Files)
|
|
{
|
|
File.Copy(newPath, newPath.Replace(sourcePath, ccmTempPath), true);
|
|
progress += progressPerFileOrDir;
|
|
worker.ReportProgress(progress);
|
|
}
|
|
fileName = Path.GetFileName(args[0]);
|
|
File.Copy(args[0], $"{homeDrive}temp\\{fileName}", true);
|
|
}
|
|
|
|
private void StartWorker_UninstallCCM()
|
|
{
|
|
worker = new BackgroundWorker();
|
|
worker.WorkerReportsProgress = true;
|
|
pwindow.ProgressLabel.Content = "Désinstallation du centre logiciel...";
|
|
worker.ProgressChanged += setProgressBarValue;
|
|
worker.RunWorkerCompleted += StartWorker_InstallCCM;
|
|
worker.DoWork += UninstallCCM;
|
|
worker.RunWorkerAsync();
|
|
}
|
|
|
|
private void UninstallCCM(object sender, System.ComponentModel.DoWorkEventArgs e)
|
|
{
|
|
Process ccmUninstallProcess = new Process();
|
|
ccmUninstallProcess.StartInfo.Verb = "runas";
|
|
ccmUninstallProcess.StartInfo.FileName = $"{ccmTempPath}\\ccmsetup.exe";
|
|
ccmUninstallProcess.StartInfo.Domain = ".";
|
|
ccmUninstallProcess.StartInfo.Arguments = "/uninstall";
|
|
ccmUninstallProcess.StartInfo.UseShellExecute = true;
|
|
ccmUninstallProcess.Start();
|
|
int progress = 0;
|
|
while (ccmUninstallProcess.HasExited == false)
|
|
{
|
|
Thread.Sleep(10000);
|
|
progress++;
|
|
worker.ReportProgress(progress);
|
|
}
|
|
worker.ReportProgress(40);
|
|
Thread.Sleep(3000);
|
|
}
|
|
|
|
private void StartWorker_InstallCCM(object sender, System.ComponentModel.RunWorkerCompletedEventArgs e)
|
|
{
|
|
worker = new BackgroundWorker();
|
|
worker.WorkerReportsProgress = true;
|
|
pwindow.ProgressLabel.Content = "Réinstallation du centre logiciel...";
|
|
worker.ProgressChanged += setProgressBarValue;
|
|
worker.RunWorkerCompleted += StartWorker_UpdateGPO;
|
|
worker.DoWork += InstallCCM;
|
|
worker.RunWorkerAsync();
|
|
}
|
|
|
|
private void InstallCCM(object sender, System.ComponentModel.DoWorkEventArgs e)
|
|
{
|
|
Process ccminstallProcess = new Process();
|
|
ccminstallProcess.StartInfo.Verb = "runas";
|
|
ccminstallProcess.StartInfo.FileName = $"{ccmTempPath}\\ccmsetup.exe";
|
|
ccminstallProcess.StartInfo.Domain = ".";
|
|
ccminstallProcess.StartInfo.Arguments = "/mp=ac-brt-ecm.in.ac-nancy-metz.fr smssitecode=ncy smsmp=ac-brt-ecm.in.ac-nancy-metz.fr dnsuffix=int.ac-nancy-metz.fr";
|
|
ccminstallProcess.StartInfo.UseShellExecute = true;
|
|
ccminstallProcess.Start();
|
|
Thread.Sleep(1000);
|
|
Process[] ccmProcess = Process.GetProcessesByName("ccmsetup");
|
|
int progress = 41;
|
|
|
|
while (ccmProcess.Length > 0)
|
|
{
|
|
Thread.Sleep(10000);
|
|
progress++;
|
|
worker.ReportProgress(progress);
|
|
ccmProcess = Process.GetProcessesByName("ccmsetup");
|
|
}
|
|
worker.ReportProgress(90);
|
|
Thread.Sleep(3000);
|
|
}
|
|
|
|
private void StartWorker_UpdateGPO(object sender, System.ComponentModel.RunWorkerCompletedEventArgs e)
|
|
{
|
|
worker = new BackgroundWorker();
|
|
worker.WorkerReportsProgress = true;
|
|
pwindow.ProgressLabel.Content = "Mise à jour des stratégies de l'ordinateur...";
|
|
worker.ProgressChanged += setProgressBarValue;
|
|
worker.DoWork += UpdateGPO;
|
|
worker.RunWorkerCompleted += closeApp;
|
|
worker.RunWorkerAsync();
|
|
}
|
|
|
|
private void UpdateGPO(object sender, System.ComponentModel.DoWorkEventArgs e)
|
|
{
|
|
Process updateGPOProcess = new Process();
|
|
updateGPOProcess.StartInfo.Verb = "runas";
|
|
updateGPOProcess.StartInfo.FileName = $"{Environment.SystemDirectory}\\gpupdate.exe";
|
|
updateGPOProcess.StartInfo.Domain = ".";
|
|
updateGPOProcess.StartInfo.WindowStyle = ProcessWindowStyle.Hidden;
|
|
updateGPOProcess.StartInfo.Arguments = "/force";
|
|
updateGPOProcess.StartInfo.UseShellExecute = true;
|
|
updateGPOProcess.Start();
|
|
Process[] gpupdateProcess = Process.GetProcessesByName("gpupdate");
|
|
while (gpupdateProcess.Length > 0)
|
|
{
|
|
Thread.Sleep(1000);
|
|
gpupdateProcess = Process.GetProcessesByName("gpupdate");
|
|
}
|
|
worker.ReportProgress(100);
|
|
Thread.Sleep(3000);
|
|
}
|
|
|
|
private void closeApp(object sender, System.ComponentModel.RunWorkerCompletedEventArgs e)
|
|
{
|
|
pwindow.Close();
|
|
}
|
|
|
|
private void setProgressBarValue(object sender, System.ComponentModel.ProgressChangedEventArgs e)
|
|
{
|
|
pwindow.setProgressBarValue(e.ProgressPercentage);
|
|
}
|
|
|
|
|
|
private void WrongPassword()
|
|
{
|
|
errorLabel.Visibility = Visibility.Visible;
|
|
passwordBox.BorderBrush = Brushes.Red;
|
|
errorLabel.Content = "Mauvais code";
|
|
}
|
|
|
|
private void StartAsAdmin(object sender, System.ComponentModel.RunWorkerCompletedEventArgs e)
|
|
{
|
|
try
|
|
{
|
|
worker = new BackgroundWorker();
|
|
worker.WorkerReportsProgress = true;
|
|
Process restartAsAdmin = new Process();
|
|
restartAsAdmin.StartInfo.FileName = $"{homeDrive}temp\\{fileName}";
|
|
restartAsAdmin.StartInfo.UserName = "administrateur";
|
|
restartAsAdmin.StartInfo.Password = passwordBox.SecurePassword;
|
|
restartAsAdmin.StartInfo.Domain = ".";
|
|
restartAsAdmin.StartInfo.Arguments = "-admin";
|
|
restartAsAdmin.StartInfo.UseShellExecute = false;
|
|
restartAsAdmin.Start();
|
|
}
|
|
catch(Exception)
|
|
{
|
|
}
|
|
pwindow.Close();
|
|
}
|
|
|
|
private void ValidPassword()
|
|
{
|
|
pwindow = new ProgressWindow();
|
|
pwindow.Show();
|
|
this.Close();
|
|
StartWorker_CopyFiles();
|
|
}
|
|
|
|
private void Button_Click(object sender, RoutedEventArgs e)
|
|
{
|
|
errorLabel.Visibility = Visibility.Hidden;
|
|
|
|
if (checkPassword())
|
|
{
|
|
ValidPassword();
|
|
}
|
|
else
|
|
{
|
|
WrongPassword();
|
|
}
|
|
}
|
|
public bool checkPassword()
|
|
{
|
|
try
|
|
{
|
|
Process testProcess = new Process();
|
|
testProcess.StartInfo.FileName = "conhost.exe";
|
|
testProcess.StartInfo.WorkingDirectory = Environment.SystemDirectory;
|
|
testProcess.StartInfo.UserName = "administrateur";
|
|
testProcess.StartInfo.Password = passwordBox.SecurePassword;
|
|
testProcess.StartInfo.Domain = ".";
|
|
testProcess.StartInfo.UseShellExecute = false;
|
|
testProcess.StartInfo.Arguments = "--headless";
|
|
testProcess.Start();
|
|
return true;
|
|
}
|
|
catch
|
|
{
|
|
return false;
|
|
}
|
|
}
|
|
}
|
|
}
|