private void RestartEvasluated()
        {
            var wi = WindowsIdentity.GetCurrent();
            var wp = new WindowsPrincipal(wi);

            bool runAsAdmin = wp.IsInRole(WindowsBuiltInRole.Administrator);

            if (!runAsAdmin)
            {
                // It is not possible to launch a ClickOnce app as administrator directly,
                // so instead we launch the app as administrator in a new process.
                var processInfo = new ProcessStartInfo(Assembly.GetExecutingAssembly().CodeBase);

                // The following properties run the new process as administrator
                processInfo.UseShellExecute = true;
                processInfo.Verb            = "runas";

                // Start the new process
                try
                {
                    Process.Start(processInfo);
                }
                catch (Exception)
                {
                    // The user did not allow the application to run as administrator
                    var message = "Sorry, but I don't seem to be able to start  this program with administrator rights!";
                    ConsoleTxt.AppendText(message);
                    ConsoleTxt.AppendText(Environment.NewLine);
                }

                // Shut down the current process
                System.Windows.Application.Current.Shutdown();
            }
        }
        private async Task <Boolean> UpdateTrustedHosts(IProgress <TaskAsyncProgress> progress, string trustedHosts)
        {
            try
            {
                OnProgress(progress, "Updating TrustedHosts...");
                bool             isElevated;
                WindowsIdentity  identity  = WindowsIdentity.GetCurrent();
                WindowsPrincipal principal = new WindowsPrincipal(identity);
                isElevated = principal.IsInRole(WindowsBuiltInRole.Administrator);

                if (isElevated)
                {
                    using (PowerShell PowerShellInstance = PowerShell.Create())
                    {
                        await Task.Run(() =>
                        {
                            string value = trustedHosts == "" ? _settings.GetServerNameFull() : (trustedHosts + ", " + _settings.GetServerNameFull());
                            var script   = "set-item -path WSMan:\\localhost\\Client\\TrustedHosts -Value \"" + value.ToLower() + "\" -Force";
                            PowerShellInstance.AddScript(script);
                            PowerShellInstance.Invoke();
                        });

                        if (PowerShellInstance.Streams.Error.Count > 0)
                        {
                            foreach (var error in PowerShellInstance.Streams.Error)
                            {
                                OnProgress(progress, error.ToString());
                                return(false);
                            }
                        }

                        var result = await CheckTrustedHosts(progress);

                        return(result);
                    }
                }
                else
                {
                    OnProgress(progress, "You should restart the program with administratrion privileges to be able to add server to TrustedHosts");
                    RestartEvasluated();
                    return(false);
                }
            }
            catch (Exception exeption)
            {
                ConsoleTxt.AppendText(exeption.Message);
                ConsoleTxt.AppendText(Environment.NewLine);
                return(false);
            }
        }
        private async Task <Boolean> RestartIis(IProgress <TaskAsyncProgress> progress)
        {
            try
            {
                OnProgress(progress, "Restarting IIS...");
                await Task.Run(() =>
                {
                    var serverName = _settings.GetServerNameFull();

                    ConnectionOptions options = new ConnectionOptions();
                    options.Password          = _settings.Password;
                    options.Username          = _settings.GetUserNameFull();
                    options.Impersonation     = ImpersonationLevel.Impersonate;

                    var remoteComputer    = "\\\\" + serverName + "\\root\\cimv2";
                    ManagementScope scope = new ManagementScope(remoteComputer, options);
                    scope.Connect();

                    ServiceController sc = new ServiceController("IISADMIN", serverName);

                    OnProgress(progress, "IIS Status = " + sc.Status);
                    OnProgress(progress, "Stopping " + sc.DisplayName);
                    sc.Stop();
                    while (sc.Status != ServiceControllerStatus.Stopped)
                    {
                        Task.Delay(TimeSpan.FromSeconds(1)).Wait();
                        sc.Refresh();
                        OnProgress(progress, "Waiting to stop... ");
                    }
                    OnProgress(progress, "IIS Stopped. Status = " + sc.Status);
                    OnProgress(progress, "IIS starting...");
                    sc.Start();
                    while (sc.Status == ServiceControllerStatus.Stopped)
                    {
                        Task.Delay(TimeSpan.FromSeconds(1)).Wait();
                        sc.Refresh();
                        OnProgress(progress, "Waiting to start... ");
                    }
                    OnProgress(progress, "IIS Started. Status = " + sc.Status);
                });

                return(true);
            }
            catch (Exception exeption)
            {
                ConsoleTxt.AppendText(exeption.Message);
                ConsoleTxt.AppendText(Environment.NewLine);
                return(false);
            }
        }
        private async void UploadCommand_Executed(object sender, ExecutedRoutedEventArgs e)
        {
            if (!_customControlViewModel.IsEmpty())
            {
                _customControlViewModel.ReloadContent();
            }
            ConsoleTxt.Clear();
            ConsoleTxt.AppendText("Start.");
            ConsoleTxt.AppendText(Environment.NewLine);
            ConsoleTxt.AppendText("Connecting to server " + _settings.GetServerNameFull());
            ConsoleTxt.AppendText(Environment.NewLine);

            UploadBtn.IsEnabled = false;
            var progress = new Progress <TaskAsyncProgress>();

            progress.ProgressChanged += (p, s) =>
            {
                ConsoleTxt.AppendText(s.Text);
                ConsoleTxt.AppendText(Environment.NewLine);
            };

            var chk = await CheckTrustedHosts(progress);

            if (chk)
            {
                var psh = await RunPsShell(progress);

                if (psh)
                {
                    var restart = await RestartIis(progress);
                }
            }

            ConsoleTxt.AppendText("OK. Update completed!");
            ConsoleTxt.AppendText(Environment.NewLine);
            UploadBtn.IsEnabled = true;
        }
        public MainWindow()
        {
            InitializeComponent();
            try
            {
                using (StreamReader r = new StreamReader(jsonFileName))
                {
                    string json = r.ReadToEnd();
                    _settings = JsonConvert.DeserializeObject <AppSettings>(json);
                }
                if (ApplicationDeployment.IsNetworkDeployed)
                {
                    this.Title = String.Format("{0} - v{1}", this.Title, ApplicationDeployment.CurrentDeployment.CurrentVersion.ToString(4));
                }
            }
            catch (Exception exeption)
            {
                ConsoleTxt.AppendText(exeption.Message);
                ConsoleTxt.AppendText(Environment.NewLine);
            }

            this.DataContext         = _customControlViewModel.dt;
            SettingsGrig.DataContext = _settings;
        }