Question

I'm connecting to a remote computer via Powershell Remoting and am trying to retrieve Event Log entries after a certain datetime. The issue is that I am never assured as to what time zone the server lives in or even what locale... My "after" parameter is in UTC. What would be the most efficient way for me to find out and pass the right timestamp to Get-EventLog function and afterwards, retrieve the log entities with UTC timestamps back?

Here's a code sample. The "after" parameter has a UTC-based date.

                        using (var cmd = PowerShell.Create().AddCommand("get-eventlog"))
                        {
                            cmd.RunspacePool = pool;
                            cmd.AddParameter("-LogName", logName);
                            cmd.AddParameter("-After", after);

                            var result = cmd.Invoke();
                            foreach (var sample in result.Where(n => n.Properties != null && n.Properties.Any()))
                            {
                                var dentry = sample as dynamic;
                                var newEntry =
                                    new PowershellEventLogEntry
                                    {
                                        Channel = logName,
                                        Category = dentry.Category,
                                        CategoryNumber = dentry.CategoryNumber,
                                        Data = dentry.Data,
                                        EntryType = dentry.EntryType,
                                        EventId = dentry.EventId,
                                        MachineName = dentry.MachineName,
                                        Message = dentry.Message,
                                        Source = dentry.Source,
                                        Timestamp = dentry.TimeGenerated
                                    };
Était-ce utile?

La solution

You could use the ToUniversalTime and ToLocalTime methods on the DateTime object to convert between the two.

Licencié sous: CC-BY-SA avec attribution
Non affilié à StackOverflow
scroll top