public void QueryInstance(string computer, string query, string user, string pass, string domain) { try { Exodus ExodusRoot = new Exodus(); ConnectionOptions connection = new ConnectionOptions(); connection.Username = user; connection.Password = pass; connection.Authority = "ntlmdomain:" + domain; ManagementScope scope = new ManagementScope("\\\\" + computer + "\\root\\virtualization\\v2", connection); scope.Connect(); ManagementObjectSearcher searcher = new ManagementObjectSearcher(query); searcher.Scope = scope; foreach (ManagementObject queryObj in searcher.Get()) { ExodusRoot.ExodusEventLog.WriteEntry(String.Format("Machine Found: {0}\nStatus: {1}\nDescription: {2}", queryObj["ElementName"], queryObj["EnabledState"], queryObj["Description"]), EventLogEntryType.Information, 200); } } catch (ManagementException ex) { // Handle the exception as appropriate. Exodus ExodusRoot = new Exodus(); ExodusRoot.ExodusEventLog.WriteEntry(ex.Message, EventLogEntryType.Error, 299); } }
public void GetAllVMs(string host, string path) { try { Exodus ExodusRoot = new Exodus(); // create a new PS instance using (PowerShell PowerShellInstance = PowerShell.Create()) { // use "AddScript" to add the contents of a script file to the end of the execution pipeline. // use "AddCommand" to add individual commands/cmdlets to the end of the execution pipeline. // use "AddParameter" to add a single parameter to the last command/script on the pipeline. PowerShellInstance.AddCommand("Get-VM"); PowerShellInstance.AddParameter("ComputerName", host); Collection <PSObject> PSOutput = PowerShellInstance.Invoke(); int startCounter = 0; int endCounter = PSOutput.Count(); foreach (PSObject outputItem in PSOutput) { if (outputItem != null) { // log the name and id of the virtual machine - REMOVED TO AVOID LOG CLUTTER //ExodusRoot.ExodusEventLog.WriteEntry(outputItem.BaseObject.GetType().FullName + "\n" + outputItem.BaseObject.ToString(), EventLogEntryType.Information, 201); string vm = outputItem.Members["Name"].Value.ToString(); BackupVM(host, vm, path); if (startCounter >= endCounter) { startCounter++; // log cluster backup completed ExodusRoot.ExodusEventLog.WriteEntry("Backups for " + endCounter.ToString() + " virtual machines in the Hyper-V cluster were successfully completed.", EventLogEntryType.Information, 201); } } } if (PowerShellInstance.Streams.Error.Count > 0) { foreach (ErrorRecord error in PowerShellInstance.Streams.Error) { ExodusRoot.ExodusEventLog.WriteEntry("Error Details: " + error.ErrorDetails + "\n" + "Exception: " + error.Exception, EventLogEntryType.Error, 298); } } } } catch (ManagementException ex) { // Handle the exception as appropriate. Exodus ExodusRoot = new Exodus(); ExodusRoot.ExodusEventLog.WriteEntry(ex.Message, EventLogEntryType.Error, 299); } }
public void MirrorFileShare(string source, string destination) { try { Exodus ExodusRoot = new Exodus(); // create a new PS instance using (PowerShell PowerShellInstance = PowerShell.Create()) { // use "AddScript" to add the contents of a script file to the end of the execution pipeline. // use "AddCommand" to add individual commands/cmdlets to the end of the execution pipeline. // use "AddParameter" to add a single parameter to the last command/script on the pipeline. PowerShellInstance.AddScript(System.IO.Path.Combine(AppDomain.CurrentDomain.BaseDirectory, "Files/RoboCopyMirror.ps1" + " -source " + source + " -destination " + destination)); Collection <PSObject> PSOutput = PowerShellInstance.Invoke(); string RoboCopyOutput = ""; foreach (PSObject outputItem in PSOutput) { if (outputItem != null) { RoboCopyOutput += outputItem.BaseObject.ToString() + System.Environment.NewLine; //ExodusRoot.ExodusEventLog.WriteEntry(outputItem.BaseObject.GetType().FullName + "\n" + outputItem.BaseObject.ToString(), EventLogEntryType.Information, 301); } } if (RoboCopyOutput != null) { // write robocopy info to the event viewer // TO DO : I'm pretty sure this is a problem, where the Robocopy info written to log can occasionally exceed 32766 characters, after which it will throw an error in the Event Viewer // I need to refactor this code so that it strips this RoboCopyOutput string into something shorter than 32766 characters. ExodusRoot.ExodusEventLog.WriteEntry(RoboCopyOutput, EventLogEntryType.Information, 302); } if (PowerShellInstance.Streams.Error.Count > 0) { foreach (ErrorRecord error in PowerShellInstance.Streams.Error) { ExodusRoot.ExodusEventLog.WriteEntry("Error Details: " + error.ErrorDetails + "\n" + "Exception: " + error.Exception, EventLogEntryType.Error, 398); } } } } catch (ManagementException ex) { // Handle the exception as appropriate. Exodus ExodusRoot = new Exodus(); ExodusRoot.ExodusEventLog.WriteEntry(ex.Message, EventLogEntryType.Error, 399); } }
public void BackupVM(string host, string vm, string path) { try { Exodus ExodusRoot = new Exodus(); // configure a dated directory path string datepath = path + Exodus.startTimeStamp; // create a new PS instance using (PowerShell PowerShellInstance = PowerShell.Create()) { // use "AddScript" to add the contents of a script file to the end of the execution pipeline. // use "AddCommand" to add individual commands/cmdlets to the end of the execution pipeline. // use "AddParameter" to add a single parameter to the last command/script on the pipeline. PowerShellInstance.AddScript(System.IO.Path.Combine(AppDomain.CurrentDomain.BaseDirectory, "HyperV/BackupOne.ps1" + " -hypervhost " + host + " -vm " + vm + " -path " + datepath)); Collection <PSObject> PSOutput = PowerShellInstance.Invoke(); foreach (PSObject outputItem in PSOutput) { if (outputItem != null) { // logs virtual machine name and id information - REMOVED TO AVOID LOG CLUTTER //ExodusRoot.ExodusEventLog.WriteEntry(outputItem.BaseObject.GetType().FullName + "\n" + outputItem.BaseObject.ToString(), EventLogEntryType.Information, 201); } } if (PowerShellInstance.Streams.Error.Count > 0) { foreach (ErrorRecord error in PowerShellInstance.Streams.Error) { ExodusRoot.ExodusEventLog.WriteEntry("Error Details: " + error.ErrorDetails + "\n" + "Exception: " + error.Exception, EventLogEntryType.Error, 298); } } } } catch (ManagementException ex) { // Handle the exception as appropriate. Exodus ExodusRoot = new Exodus(); ExodusRoot.ExodusEventLog.WriteEntry(ex.Message, EventLogEntryType.Error, 299); } }
public void DelegateAccount(string host, string path) { try { Exodus ExodusRoot = new Exodus(); // create a new PS instance using (PowerShell PowerShellInstance = PowerShell.Create()) { // use "AddScript" to add the contents of a script file to the end of the execution pipeline. // use "AddCommand" to add individual commands/cmdlets to the end of the execution pipeline. // use "AddParameter" to add a single parameter to the last command/script on the pipeline. PowerShellInstance.AddScript(System.IO.Path.Combine(AppDomain.CurrentDomain.BaseDirectory, "HyperV/DelegateAccount.ps1" + " -computer " + host + " -path " + path)); Collection <PSObject> PSOutput = PowerShellInstance.Invoke(); foreach (PSObject outputItem in PSOutput) { if (outputItem != null) { // log account delegate information ExodusRoot.ExodusEventLog.WriteEntry(outputItem.BaseObject.GetType().FullName + "\n" + outputItem.BaseObject.ToString(), EventLogEntryType.Information, 201); } } if (PowerShellInstance.Streams.Error.Count > 0) { foreach (ErrorRecord error in PowerShellInstance.Streams.Error) { ExodusRoot.ExodusEventLog.WriteEntry("Error Details: " + error.ErrorDetails + "\n" + "Exception: " + error.Exception, EventLogEntryType.Error, 298); } } } } catch (ManagementException ex) { // Handle the exception as appropriate. Exodus ExodusRoot = new Exodus(); ExodusRoot.ExodusEventLog.WriteEntry(ex.Message, EventLogEntryType.Error, 299); } }
public void ManageBackupDirectory(string path, int retain) { try { Exodus ExodusRoot = new Exodus(); // configure a dated directory path string datepath = path + Exodus.startTimeStamp; // create the directory for today's backup // this method does nothing if the directory already exists. System.IO.Directory.CreateDirectory(datepath); // log directory creation events - REMOVED TO AVOID LOG CLUTTER //string message0 = "The backup directory " + datepath + " was created."; //ExodusRoot.ExodusEventLog.WriteEntry(message0, EventLogEntryType.Information, 201); // if there are more backup directories than specified if (System.IO.Directory.GetDirectories(@path).Length > retain) { string message1 = System.IO.Directory.GetDirectories(@path).Length.ToString() + " directories were found, which is greater than the retention count specified in ExodusConfig.xml. Deleting oldest backup directory."; ExodusRoot.ExodusEventLog.WriteEntry(message1, EventLogEntryType.Information, 201); // log retention count information // this code gets all the directories in the specified remote path, orders them youngest first, skips the specified retain count, and deletes everything else foreach (var fi in new DirectoryInfo(path).GetDirectories().OrderByDescending(x => x.LastWriteTime).Skip(retain)) { fi.Delete(true); // recursively delete the directory string message2 = fi.FullName + " was deleted."; ExodusRoot.ExodusEventLog.WriteEntry(message2, EventLogEntryType.Information, 201); // log the deletion of a backup directory } } } catch (ManagementException ex) { // Handle the exception as appropriate. Exodus ExodusRoot = new Exodus(); ExodusRoot.ExodusEventLog.WriteEntry(ex.Message, EventLogEntryType.Error, 299); } }