public async Task ChangeOfficeChannelWmi(RemoteMachine client, OfficeInstallation localInstall) { var newChannel = client.Channel.ToString(); string version = null; if (client.Version != null) { version = client.Version.ToString(); } await Task.Run(async() => { var installOffice = new InstallOfficeWmi { remoteUser = client.UserName, remoteComputerName = client.Machine, remoteDomain = client.WorkGroup, remotePass = client.Password, newChannel = client.Channel.ToString(), newVersion = version, connectionNamespace = "\\root\\cimv2" }; try { var ppDownloader = new ProPlusDownloader(); var baseUrl = await ppDownloader.GetChannelBaseUrlAsync(newChannel, OfficeEdition.Office32Bit); if (string.IsNullOrEmpty(baseUrl)) { throw (new Exception(string.Format("Cannot find BaseUrl for Channel: {0}", newChannel))); } var channelToChangeTo = newChannel; if (string.IsNullOrEmpty(channelToChangeTo)) { throw (new Exception("Version required")); } await installOffice.ChangeOfficeChannel(channelToChangeTo, baseUrl); var installGenerator = new OfficeInstallManager(); } catch (Exception ex) { LogWmiErrorMessage(ex, new RemoteComputer() { Name = client.Machine, Domain = client.WorkGroup, UserName = client.UserName, Password = client.Password }); throw (new Exception("Update Failed")); } }); }
private async Task CollectMachineData(RemoteMachine remoteMachine) { var installGenerator = new OfficeInstallManager(remoteMachine.Machine, remoteMachine.WorkGroup, remoteMachine.UserName, remoteMachine.Password); try { remoteMachine.Status = "Checking..."; RemoteMachineList.ItemsSource = null; RemoteMachineList.ItemsSource = remoteClients; await installGenerator.InitConnections(); var officeInstall = await installGenerator.CheckForOfficeInstallAsync(); var channels = new List <Channel>(); var versions = new List <officeVersion>(); if (officeInstall.Channel != null) { var branches = GlobalObjects.ViewModel.Branches; var currentChannel = new Channel() { Name = officeInstall.Channel.Trim() }; var currentVersion = new officeVersion() { Number = officeInstall.Version.Trim() }; versions.Add(currentVersion); channels.Add(currentChannel); foreach (var branch in branches) { if (branch.NewName.ToString() != officeInstall.Channel) { var tempChannel = new Channel() { Name = branch.NewName.ToString() }; channels.Add(tempChannel); } else { versions = GetVersions(branch, versions, currentVersion.Number); } } remoteMachine.include = false; remoteMachine.Machine = remoteMachine.Machine; remoteMachine.UserName = remoteMachine.UserName; remoteMachine.Password = remoteMachine.Password; remoteMachine.WorkGroup = remoteMachine.WorkGroup; remoteMachine.Status = "Found"; remoteMachine.Channels = channels; remoteMachine.Channel = currentChannel; remoteMachine.OriginalChannel = currentChannel; remoteMachine.Versions = versions; remoteMachine.Version = currentVersion; remoteMachine.OriginalVersion = currentVersion; RemoteMachineList.ItemsSource = null; RemoteMachineList.ItemsSource = remoteClients; } } catch (Exception ex) { remoteMachine.Status = ex.Message; RemoteMachineList.ItemsSource = null; RemoteMachineList.ItemsSource = remoteClients; LogWmiErrorMessage(ex, new RemoteComputer() { Name = remoteMachine.Machine, Domain = remoteMachine.WorkGroup, UserName = remoteMachine.UserName, Password = remoteMachine.Password }); } if (remoteMachine.Status == "Checking...") { remoteMachine.Status = "Not Found"; } RemoteMachineList.ItemsSource = null; RemoteMachineList.ItemsSource = remoteClients; }
private async Task UpdateMachine(RemoteMachine client, int i) { var connectionInfo = new string[4]; RemoteMachineList.Dispatcher.Invoke(new Action(() => { RemoteMachineList.UpdateLayout(); })); var row = (DataGridRow)RemoteMachineList.ItemContainerGenerator.ContainerFromIndex(i); System.Windows.Controls.TextBlock statusText = null; if (row != null) { row.Dispatcher.Invoke(new Action(() => { statusText = row.FindChild <System.Windows.Controls.TextBlock>("TxtStatus"); })); } else { return; } RemoteMachineList.Dispatcher.Invoke(new Action(() => { RemoteMachineList.Items.Refresh(); })); try { client.Status = "Updating"; statusText.Dispatcher.Invoke(new Action(() => { statusText.Text = "Updating"; })); RemoteMachineList.Dispatcher.Invoke(new Action(() => { RemoteMachineList.Items.Refresh(); })); //throw (new Exception("")); connectionInfo = new string[4] { client.UserName, client.Password, client.Machine, client.WorkGroup }; var installGenerator = new OfficeInstallManager(client.Machine, client.WorkGroup, client.UserName, client.Password); var newVersion = client.Version; var newChannel = client.Channel; await Task.Run(async() => { await installGenerator.InitConnections(); }); var officeInstall = await Task.Run(() => { return(installGenerator.CheckForOfficeInstallAsync()); }); await Task.Run(async() => { await ChangeOfficeChannelWmi(client, officeInstall); }); client.Status = "Success"; statusText.Dispatcher.Invoke(new Action(() => { statusText.Text = "Success"; })); RemoteMachineList.Dispatcher.Invoke(new Action(() => { RemoteMachineList.Items.Refresh(); })); } catch (Exception ex) // if fails via WMI, try via powershell { try { LogWmiErrorMessage(ex, new RemoteComputer() { Name = client.Machine, Domain = client.WorkGroup, Password = client.Password, UserName = client.UserName }); string PSPath = System.IO.Path.GetTempPath() + client.Machine + "PowershellAttempt.txt"; System.IO.File.Delete(PSPath); var powerShellInstance = System.Management.Automation.PowerShell.Create(); if (!String.IsNullOrEmpty(client.OriginalVersion.Number) || client.Version.Number != client.OriginalVersion.Number) { powerShellInstance.AddScript(System.IO.Directory.GetCurrentDirectory() + "\\Resources\\UpdateScriptLaunch.ps1 -Channel " + client.Channel.Name + " -DisplayLevel $false -machineToRun " + client.Machine + " -UpdateToVersion " + client.Version.Number); var asyncResult = powerShellInstance.BeginInvoke(); //possible make async so toolkit doesn't freeze the console client.Status = "Updating..."; //statusText.Dispatcher.Invoke(new Action(() => //{ // statusText.Text = "Updating..."; //})); //RemoteMachineList.Dispatcher.Invoke(new Action(() => //{ // RemoteMachineList.Items.Refresh(); // RemoteMachineList.ItemsSource = null; // RemoteMachineList.ItemsSource = remoteClients; //})); powerShellInstance.EndInvoke(asyncResult); } else { //statusText.Dispatcher.Invoke(new Action(() => //{ // statusText.Text = "Success"; //})); client.Status = "Success"; //RemoteMachineList.Dispatcher.Invoke(new Action(() => // { // RemoteMachineList.Items.Refresh(); // })); RemoteMachineList.Items.Refresh(); } PsUpdateExited(PSPath, statusText, client); } catch (Exception ex1) { client.Status = "Error: " + ex.Message; using (System.IO.StreamWriter file = new System.IO.StreamWriter( System.IO.Path.GetTempPath() + client.Machine + "PowershellError.txt", true)) { file.WriteLine(ex1.Message); file.WriteLine(ex1.StackTrace); } statusText.Dispatcher.Invoke(new Action(() => { statusText.Text = "Error: " + ex.Message; })); RemoteMachineList.Dispatcher.Invoke(new Action(() => { RemoteMachineList.Items.Refresh(); })); } } }