public bool RepairRemoteWmi(string hostname, CredToken credentialToken) { _logger.LogMessage($"Attempting to repair WMI on device {hostname}"); ResultConsole.Instance.AddConsoleLine($"Attempting to repair WMI on device {hostname}"); var remoteBatchPath = $"\\\\{hostname}\\C$\\temp\\fixwmi.bat"; var commandline = @"-i cmd /c C:\temp\fixwmi.bat"; // -i flag is required for PSExec to push the command through successfully. var batchFileContent = CreateWmiRepairBatchConent(); try { _logger.LogMessage("Creating remote batch file"); _fileAndFolderServices.CreateRemoteTextFile(remoteBatchPath, batchFileContent, _logger); } catch (Exception ex) { _logger.LogError("Error creating remote WMI repair batch file.", ex); ResultConsole.Instance.AddConsoleLine($"Error creating remote WMI repair batch file. Exception thrown: {ex.Message}"); return(false); } try { _logger.LogMessage($"Run WMI repair batch on remote device {hostname}"); _psExecServices.RunOnDeviceWithAuthentication(hostname, commandline, credentialToken); return(true); } catch (Exception ex) { _logger.LogError($"Error running remote batch file on device {hostname}", ex); ResultConsole.Instance.AddConsoleLine($"Error running remote batch file on device {hostname}\n Exception: {ex.Message}"); return(false); } }
public void RunOnDeviceWithAuthentication(string device, string commandline, CredToken creds) { // For whatever reason, making everything a string literal fixed a problem with making this work correctly var loggableArguments = $@"\\{device} -u {creds.Domain}\{creds.User} -p [REDACTED] " + commandline; _logger.LogMessage($"Beginning PsExec attempt on {device} with following command line options: {loggableArguments}"); RunPsExec(device, commandline, creds); }
public override void OpenUserInterfaceElement(string rawDeviceList) { _parsedListCache = ParseDeviceList(rawDeviceList); _credToken = CredentialManager.RequestCredentials(); if (_credToken == null) { var msg = $"Action {ActionName} canceled by user."; Logger.LogMessage(msg); ResultConsole.AddConsoleLine(msg); CancellationToken.Cancel(); } }
private void RunPsExec(string device, string arguments, CredToken creds = null) { ForceCleanRemotePsExeSvc(device); var process = GeneratePsExecProcess(); try { if (creds != null) { process.StartInfo.Arguments = $@"\\{device} -u {creds.Domain}\{creds.User} -p {SecureStringHelper.GetInsecureString(creds.SecurePassword)} {arguments}"; } else { process.StartInfo.Arguments = $@"\\{device} {arguments}"; } process.Start(); _logger.LogMessage($"PSExec process started with start ID: {process.Id}"); process.WaitForExit(60000); if (!process.HasExited) { process.Kill(); _logger.LogMessage($"Killed process {process.Id}"); } var stdOutput = process.StandardOutput.ReadToEnd(); var errOutput = process.StandardError.ReadToEnd(); var stdResult = RemoveEmptyLines(stdOutput); var errResult = RemoveEmptyLines(errOutput); if (string.IsNullOrWhiteSpace(stdOutput) && string.IsNullOrWhiteSpace(errResult)) { throw new Exception($"Device did not return a value. Please run the command on the device manually. \n PsExec command line: {process.StartInfo.Arguments}"); } ResultConsole.Instance.AddConsoleLine(errResult); ResultConsole.Instance.AddConsoleLine(stdResult); _logger.LogMessage(errResult); } catch (Exception ex) { var exceptionResult = $"Exception running command on device {device}. {ex.Message}"; ResultConsole.Instance.AddConsoleLine(exceptionResult); } Thread.Sleep(1000); }
public static CredToken RequestCredentials() { var loginWindowViewModel = new LoginWindowViewModel(); _windowService.ShowDialog <LoginWindow>(loginWindowViewModel); if (loginWindowViewModel.WasCanceled) { loginWindowViewModel.Dispose(); return(null); } var credtoken = new CredToken(loginWindowViewModel.Domain, loginWindowViewModel.Username, SecureStringHelper.BuildSecureString(loginWindowViewModel.Password)); return(credtoken); }