diff --git a/src/Microsoft.PowerShell.Commands.Diagnostics/CommonUtils.cs b/src/Microsoft.PowerShell.Commands.Diagnostics/CommonUtils.cs index cda350276..8c516462f 100644 --- a/src/Microsoft.PowerShell.Commands.Diagnostics/CommonUtils.cs +++ b/src/Microsoft.PowerShell.Commands.Diagnostics/CommonUtils.cs @@ -14,9 +14,90 @@ namespace Microsoft.PowerShell.Commands.Diagnostics.Common { internal static class CommonUtilities { + private const string LibraryLoadDllName = "api-ms-win-core-libraryloader-l1-2-0.dll"; + private const string LocalizationDllName = "api-ms-win-core-localization-l1-2-1.dll"; + + private const uint FORMAT_MESSAGE_IGNORE_INSERTS = 0x00000200; + private const uint FORMAT_MESSAGE_FROM_SYSTEM = 0x00001000; + private const uint LOAD_LIBRARY_AS_DATAFILE = 0x00000002; + private const uint FORMAT_MESSAGE_FROM_HMODULE = 0x00000800; + + [DllImport(LocalizationDllName, SetLastError = true, CharSet = CharSet.Unicode)] + private static extern uint FormatMessage(uint dwFlags, IntPtr lpSource, + uint dwMessageId, uint dwLanguageId, + [MarshalAs(UnmanagedType.LPWStr)] + StringBuilder lpBuffer, + uint nSize, IntPtr Arguments); + + [DllImport(LibraryLoadDllName, SetLastError = true, CharSet = CharSet.Unicode)] + private static extern IntPtr LoadLibraryEx( + [MarshalAs(UnmanagedType.LPWStr)] string lpFileName, + IntPtr hFile, + uint dwFlags + ); + + [DllImport(LibraryLoadDllName)] + private static extern bool FreeLibrary(IntPtr hModule); + + + [DllImport(LocalizationDllName, EntryPoint = "GetUserDefaultLangID", CallingConvention = CallingConvention.Winapi, SetLastError = true)] + private static extern ushort GetUserDefaultLangID(); + + + public static uint FormatMessageFromModule(uint lastError, string moduleName, out String msg) + { + Debug.Assert(!string.IsNullOrEmpty(moduleName)); + + uint formatError = 0; + msg = String.Empty; + + IntPtr moduleHandle = LoadLibraryEx(moduleName, IntPtr.Zero, LOAD_LIBRARY_AS_DATAFILE); + if (moduleHandle == IntPtr.Zero) + { + return (uint)Marshal.GetLastWin32Error(); + } + + try + { + uint dwFormatFlags = FORMAT_MESSAGE_IGNORE_INSERTS | FORMAT_MESSAGE_FROM_SYSTEM | FORMAT_MESSAGE_FROM_HMODULE; + uint LANGID = (uint)GetUserDefaultLangID(); + uint langError = (uint)Marshal.GetLastWin32Error(); + if (langError != 0) + { + LANGID = 0; // neutral + } + + StringBuilder outStringBuilder = new StringBuilder(1024); + uint nChars = FormatMessage(dwFormatFlags, + moduleHandle, + lastError, + LANGID, + outStringBuilder, + (uint)outStringBuilder.Capacity, + IntPtr.Zero); + + if (nChars == 0) + { + formatError = (uint)Marshal.GetLastWin32Error(); + } + else + { + msg = outStringBuilder.ToString(); + if (msg.EndsWith(Environment.NewLine, StringComparison.Ordinal)) + { + msg = msg.Substring(0, msg.Length - 2); + } + } + } + finally + { + FreeLibrary(moduleHandle); + } + return formatError; + } + public static ResourceManager GetResourceManager() { - // this naming pattern is dictated by the dotnet cli return new ResourceManager("Microsoft.PowerShell.Commands.Diagnostics.resources.GetEventResources", typeof(CommonUtilities).GetTypeInfo().Assembly); } } diff --git a/src/Microsoft.PowerShell.Commands.Diagnostics/GetCounterCommand.cs b/src/Microsoft.PowerShell.Commands.Diagnostics/GetCounterCommand.cs index 0889cdc7f..5cd197d1c 100644 --- a/src/Microsoft.PowerShell.Commands.Diagnostics/GetCounterCommand.cs +++ b/src/Microsoft.PowerShell.Commands.Diagnostics/GetCounterCommand.cs @@ -31,7 +31,7 @@ namespace Microsoft.PowerShell.Commands /// /// Class that implements the Get-Counter cmdlet. /// - [Cmdlet(VerbsCommon.Get, "Counter", DefaultParameterSetName = "GetCounterSet", HelpUri = "https://go.microsoft.com/fwlink/?LinkID=138335")] + [Cmdlet(VerbsCommon.Get, "Counter", DefaultParameterSetName = "GetCounterSet", HelpUri = "https://go.microsoft.com/fwlink/?LinkID=2109647")] public sealed class GetCounterCommand : PSCmdlet { // @@ -211,19 +211,13 @@ namespace Microsoft.PowerShell.Commands protected override void BeginProcessing() { -#if CORECLR if (Platform.IsIoT) { // IoT does not have the '$env:windir\System32\pdh.dll' assembly which is required by this cmdlet. throw new PlatformNotSupportedException(); } - // PowerShell 7 requires at least Windows 7, - // so no version test is needed - _pdhHelper = new PdhHelper(false); -#else - _pdhHelper = new PdhHelper(System.Environment.OSVersion.Version.Major < 6); -#endif + _pdhHelper = new PdhHelper(); _resourceMgr = Microsoft.PowerShell.Commands.Diagnostics.Common.CommonUtilities.GetResourceManager(); uint res = _pdhHelper.ConnectToDataSource(); @@ -570,12 +564,7 @@ namespace Microsoft.PowerShell.Commands break; } -#if CORECLR - // CoreCLR has no overload of WaitOne with (interval, exitContext) - bool cancelled = _cancelEventArrived.WaitOne((int)_sampleInterval * 1000); -#else bool cancelled = _cancelEventArrived.WaitOne((int)_sampleInterval * 1000, true); -#endif if (cancelled) { break; @@ -666,11 +655,7 @@ namespace Microsoft.PowerShell.Commands private static CultureInfo GetCurrentCulture() { -#if CORECLR - return CultureInfo.CurrentCulture; -#else return Thread.CurrentThread.CurrentUICulture; -#endif } } } diff --git a/src/Microsoft.PowerShell.Commands.Diagnostics/Microsoft.PowerShell.Commands.Diagnostics.csproj b/src/Microsoft.PowerShell.Commands.Diagnostics/Microsoft.PowerShell.Commands.Diagnostics.csproj index 09a7c3545..b6e7c6553 100644 --- a/src/Microsoft.PowerShell.Commands.Diagnostics/Microsoft.PowerShell.Commands.Diagnostics.csproj +++ b/src/Microsoft.PowerShell.Commands.Diagnostics/Microsoft.PowerShell.Commands.Diagnostics.csproj @@ -15,19 +15,19 @@ - - - - - + - - - + + + + + + + diff --git a/src/Microsoft.PowerShell.Commands.Diagnostics/PdhHelper.cs b/src/Microsoft.PowerShell.Commands.Diagnostics/PdhHelper.cs index 0814e1b67..83aa93f02 100644 --- a/src/Microsoft.PowerShell.Commands.Diagnostics/PdhHelper.cs +++ b/src/Microsoft.PowerShell.Commands.Diagnostics/PdhHelper.cs @@ -194,13 +194,6 @@ namespace Microsoft.Powershell.Commands.GetCounter.PdhNative internal class PdhHelper : IDisposable { - private bool _isPreVista = false; - - public PdhHelper(bool isPreVista) - { - _isPreVista = isPreVista; - } - [StructLayout(LayoutKind.Sequential, CharSet = CharSet.Unicode)] private struct PDH_COUNTER_PATH_ELEMENTS { @@ -271,7 +264,6 @@ namespace Microsoft.Powershell.Commands.GetCounter.PdhNative public UInt32 SampleCount; } - /* // // This is the structure returned by PdhGetCounterInfo(). // We only need dwType and lDefaultScale fields from this structure. @@ -298,7 +290,7 @@ namespace Microsoft.Powershell.Commands.GetCounter.PdhNative [FieldOffset(96)] public string szExplainText; [FieldOffset(104)]public IntPtr DataBuffer; - }*/ + } [DllImport("pdh.dll", CharSet = CharSet.Unicode)] private static extern uint PdhBindInputDataSource(out PdhSafeDataSourceHandle phDataSource, string szLogFileNameList); @@ -309,12 +301,6 @@ namespace Microsoft.Powershell.Commands.GetCounter.PdhNative [DllImport("pdh.dll", CharSet = CharSet.Unicode)] private static extern uint PdhAddCounter(PdhSafeQueryHandle queryHandle, string counterPath, IntPtr userData, out IntPtr counterHandle); - // Win7+ only - [DllImport("pdh.dll", CharSet = CharSet.Unicode)] - private static extern uint PdhAddRelogCounter(PdhSafeQueryHandle queryHandle, string counterPath, - UInt32 counterType, UInt32 counterDefaultScale, - UInt64 timeBase, out IntPtr counterHandle); - // not on XP [DllImport("pdh.dll")] private static extern uint PdhCollectQueryDataWithTime(PdhSafeQueryHandle queryHandle, ref Int64 pllTimeStamp); @@ -338,23 +324,6 @@ namespace Microsoft.Powershell.Commands.GetCounter.PdhNative out PdhSafeLogHandle phLog ); - // Win7+ only - [DllImport("pdh.dll", CharSet = CharSet.Unicode)] - private static extern void PdhResetRelogCounterValues(PdhSafeLogHandle LogHandle); - - // Win7+ only - [DllImport("pdh.dll", CharSet = CharSet.Unicode)] - private static extern uint PdhSetCounterValue(IntPtr CounterHandle, - ref PDH_RAW_COUNTER Value, /*PPDH_RAW_COUNTER */ - string InstanceName - ); - - // Win7+ only - [DllImport("pdh.dll")] - private static extern uint PdhWriteRelogSample(PdhSafeLogHandle LogHandle, - Int64 Timestamp - ); - [DllImport("pdh.dll", CharSet = CharSet.Unicode)] private static extern uint PdhGetFormattedCounterValue(IntPtr counterHandle, uint dwFormat, out IntPtr lpdwType, out PDH_FMT_COUNTERVALUE_DOUBLE pValue); @@ -404,10 +373,6 @@ namespace Microsoft.Powershell.Commands.GetCounter.PdhNative [DllImport("pdh.dll", CharSet = CharSet.Unicode)] private static extern uint PdhValidatePath(string szFullPathBuffer); - // not available on XP - [DllImport("pdh.dll", CharSet = CharSet.Unicode, CallingConvention = CallingConvention.StdCall, PreserveSig = true)] // private export - private static extern IntPtr PdhGetExplainText(string szMachineName, string szObjectName, string szCounterName); - [DllImport("pdh.dll", CharSet = CharSet.Unicode)] private static extern uint PdhGetCounterInfo(IntPtr hCounter, [MarshalAs(UnmanagedType.U1)]bool bRetrieveExplainText, ref IntPtr pdwBufferSize, IntPtr lpBuffer); @@ -459,11 +424,6 @@ namespace Microsoft.Powershell.Commands.GetCounter.PdhNative // private Dictionary _consumerPathToHandleAndInstanceMap = new Dictionary(); - // - // m_ReloggerPathToHandleAndInstanceMap map is used for writing relog counters. - // - private Dictionary _reloggerPathToHandleAndInstanceMap = new Dictionary(); - /// /// A helper reading in a Unicode string with embedded NULLs and splitting it into a StringCollection. /// @@ -518,10 +478,9 @@ namespace Microsoft.Powershell.Commands.GetCounter.PdhNative res = PdhGetCounterInfo(hCounter, false, ref pBufferSize, bufCounterInfo); if (res == 0 && bufCounterInfo != IntPtr.Zero) { - // PDH_COUNTER_INFO pdhCounterInfo = (PDH_COUNTER_INFO)Marshal.PtrToStructure(bufCounterInfo, typeof(PDH_COUNTER_INFO)); - - counterType = (uint)Marshal.ReadInt32(bufCounterInfo, 4); - defaultScale = (uint)Marshal.ReadInt32(bufCounterInfo, 20); + PDH_COUNTER_INFO pdhCounterInfo = (PDH_COUNTER_INFO)Marshal.PtrToStructure(bufCounterInfo, typeof(PDH_COUNTER_INFO)); + counterType = pdhCounterInfo.dwType; + defaultScale = pdhCounterInfo.lDefaultScale; } } finally @@ -852,14 +811,7 @@ namespace Microsoft.Powershell.Commands.GetCounter.PdhNative { outPath = Marshal.PtrToStringUni(strPath); - if (!_isPreVista) - { - ret = (PdhValidatePathEx(_hDataSource, outPath) == 0); - } - else - { - ret = (PdhValidatePath(outPath) == 0); - } + ret = (PdhValidatePathEx(_hDataSource, outPath) == 0); } } finally @@ -872,17 +824,7 @@ namespace Microsoft.Powershell.Commands.GetCounter.PdhNative public bool IsPathValid(string path) { - if (!_isPreVista) - { - return (PdhValidatePathEx(_hDataSource, path) == 0); - } - else - { - // - // Note: this assumes the paths already contain machine names - // - return (PdhValidatePath(path) == 0); - } + return (PdhValidatePathEx(_hDataSource, path) == 0); } private uint MakePath(PDH_COUNTER_PATH_ELEMENTS pathElts, out string outPath, bool bWildcardInstances) @@ -1196,338 +1138,16 @@ namespace Microsoft.Powershell.Commands.GetCounter.PdhNative return bAtLeastOneAdded ? 0 : res; } - // - // AddRelogCounters combines instances and adds counters to m_hQuery. - // The counter handles and full paths - // - public uint AddRelogCounters(PerformanceCounterSampleSet sampleSet) - { - Debug.Assert(_hQuery != null && !_hQuery.IsInvalid); - - uint res = 0; - - Dictionary> prefixInstanceMap = new Dictionary>(); - - // - // Go through all the samples one, constructing prefixInstanceMap and adding new counters as needed - // - foreach (PerformanceCounterSample sample in sampleSet.CounterSamples) - { - PDH_COUNTER_PATH_ELEMENTS pathElts = new PDH_COUNTER_PATH_ELEMENTS(); - res = ParsePath(sample.Path, ref pathElts); - if (res != 0) - { - // Skipping for now, but should be a non-terminating error - continue; - } - - string lowerCaseMachine = pathElts.MachineName.ToLowerInvariant(); - string lowerCaseObject = pathElts.ObjectName.ToLowerInvariant(); - string lowerCaseCounter = pathElts.CounterName.ToLowerInvariant(); - - string lcPathMinusInstance = @"\\" + lowerCaseMachine + @"\" + lowerCaseObject + @"\" + lowerCaseCounter; - - List sampleList; - if (prefixInstanceMap.TryGetValue(lcPathMinusInstance, out sampleList)) - { - prefixInstanceMap[lcPathMinusInstance].Add(sample); - } - else - { - List newList = new List(); - newList.Add(sample); - prefixInstanceMap.Add(lcPathMinusInstance, newList); - } - - // Console.WriteLine ("Added path " + sample.Path + " to the 1ist map with prefix " + lcPathMinusInstance); - } - - // - // Add counters to the query, consolidating multi-instance with a wildcard path, - // and construct m_ReloggerPathToHandleAndInstanceMap where each full path would be pointing to its counter handle - // and an instance name (might be empty for no-instance counter types). - // You can have multiple full paths inside m_ReloggerPathToHandleAndInstanceMap pointing to the same handle. - // - - foreach (string prefix in prefixInstanceMap.Keys) - { - IntPtr counterHandle; - string unifiedPath = prefixInstanceMap[prefix][0].Path; - - if (prefixInstanceMap[prefix].Count > 1) - { - res = MakeAllInstancePath(prefixInstanceMap[prefix][0].Path, out unifiedPath); - if (res != 0) - { - // Skipping for now, but should be a non-terminating error - continue; - } - } - - res = PdhAddRelogCounter(_hQuery, - unifiedPath, - (UInt32)prefixInstanceMap[prefix][0].CounterType, - prefixInstanceMap[prefix][0].DefaultScale, - prefixInstanceMap[prefix][0].TimeBase, - out counterHandle); - if (res != 0) - { - // Skipping for now, but should be a non-terminating error - // Console.WriteLine ("PdhAddCounter returned " + res + " for counter path " + unifiedPath); - continue; - } - - // now, add all actual paths to m_ReloggerPathToHandleAndInstanceMap - foreach (PerformanceCounterSample sample in prefixInstanceMap[prefix]) - { - PDH_COUNTER_PATH_ELEMENTS pathElts = new PDH_COUNTER_PATH_ELEMENTS(); - res = ParsePath(sample.Path, ref pathElts); - if (res != 0) - { - // Skipping for now, but should be a non-terminating error - continue; - } - - CounterHandleNInstance chi = new CounterHandleNInstance(); - - chi.hCounter = counterHandle; - - if (pathElts.InstanceName != null) - { - chi.InstanceName = pathElts.InstanceName.ToLowerInvariant(); - } - - if (!_reloggerPathToHandleAndInstanceMap.ContainsKey(sample.Path.ToLowerInvariant())) - { - _reloggerPathToHandleAndInstanceMap.Add(sample.Path.ToLowerInvariant(), chi); - // Console.WriteLine ("added map path:" + sample.Path ); - } - } - } - - // TODO: verify that all counters are in the map - - return (_reloggerPathToHandleAndInstanceMap.Keys.Count > 0) ? 0 : res; - } - - // - // AddRelogCountersPreservingPaths preserves all paths and adds as relog counters to m_hQuery. - // The counter handles and full paths are added to m_ReloggerPathToHandleAndInstanceMap - // - public uint AddRelogCountersPreservingPaths(PerformanceCounterSampleSet sampleSet) - { - Debug.Assert(_hQuery != null && !_hQuery.IsInvalid); - - uint res = 0; - - // - // Go through all the samples one, constructing prefixInstanceMap and adding new counters as needed - // - foreach (PerformanceCounterSample sample in sampleSet.CounterSamples) - { - PDH_COUNTER_PATH_ELEMENTS pathElts = new PDH_COUNTER_PATH_ELEMENTS(); - res = ParsePath(sample.Path, ref pathElts); - if (res != 0) - { - // Skipping for now, but should be a non-terminating error - continue; - } - - IntPtr counterHandle; - res = PdhAddRelogCounter(_hQuery, - sample.Path, - (uint)sample.CounterType, - sample.DefaultScale, - sample.TimeBase, - out counterHandle); - if (res != 0) - { - // Skipping for now, but should be a non-terminating error - continue; - } - - CounterHandleNInstance chi = new CounterHandleNInstance(); - - chi.hCounter = counterHandle; - if (pathElts.InstanceName != null) - { - chi.InstanceName = pathElts.InstanceName.ToLowerInvariant(); - } - - if (!_reloggerPathToHandleAndInstanceMap.ContainsKey(sample.Path.ToLowerInvariant())) - { - _reloggerPathToHandleAndInstanceMap.Add(sample.Path.ToLowerInvariant(), chi); - } - } - - return (_reloggerPathToHandleAndInstanceMap.Keys.Count > 0) ? 0 : res; - } - public string GetCounterSetHelp(string szMachineName, string szObjectName) { - if (_isPreVista) - { - return string.Empty; - } - - IntPtr retString = PdhGetExplainText(szMachineName, szObjectName, null); - return Marshal.PtrToStringUni(retString); - } - - public uint ReadNextSetPreVista(out PerformanceCounterSampleSet nextSet, bool bSkipReading) - { - uint res = 0; - nextSet = null; - - res = PdhCollectQueryData(_hQuery); - if (bSkipReading) - { - return res; - } - - if (res != 0 && res != PdhResults.PDH_NO_DATA) - { - return res; - } - - PerformanceCounterSample[] samplesArr = new PerformanceCounterSample[_consumerPathToHandleAndInstanceMap.Count]; - uint sampleIndex = 0; - uint numInvalidDataSamples = 0; - uint lastErr = 0; - - DateTime sampleTimeStamp = DateTime.Now; - - foreach (string path in _consumerPathToHandleAndInstanceMap.Keys) - { - IntPtr counterTypePtr = new IntPtr(0); - UInt32 counterType = (UInt32)PerformanceCounterType.RawBase; - UInt32 defaultScale = 0; - UInt64 timeBase = 0; - - IntPtr hCounter = _consumerPathToHandleAndInstanceMap[path].hCounter; - Debug.Assert(hCounter != null); - - res = GetCounterInfoPlus(hCounter, out counterType, out defaultScale, out timeBase); - if (res != 0) - { - // Console.WriteLine ("GetCounterInfoPlus for " + path + " failed with " + res); - } - - PDH_RAW_COUNTER rawValue; - res = PdhGetRawCounterValue(hCounter, out counterTypePtr, out rawValue); - if (res == PdhResults.PDH_INVALID_DATA || res == PdhResults.PDH_NO_DATA) - { - // Console.WriteLine ("PdhGetRawCounterValue returned " + res); - samplesArr[sampleIndex++] = new PerformanceCounterSample(path, - _consumerPathToHandleAndInstanceMap[path].InstanceName, - 0, - (ulong)0, - (ulong)0, - 0, - PerformanceCounterType.RawBase, - defaultScale, - timeBase, - DateTime.Now, - (UInt64)DateTime.Now.ToFileTime(), - rawValue.CStatus); - - numInvalidDataSamples++; - lastErr = res; - continue; - } - else if (res != 0) - { - return res; - } - - long dtFT = (((long)rawValue.TimeStamp.dwHighDateTime) << 32) + - (uint)rawValue.TimeStamp.dwLowDateTime; - - // - // NOTE: PDH returns the filetime as local time, therefore - // we need to call FromFileTimUtc() to avoid .NET applying the timezone adjustment. - // However, that would result in the DateTime object having Kind.Utc. - // We have to copy it once more to correct that (Kind is a read-only property). - // - sampleTimeStamp = new DateTime(DateTime.FromFileTimeUtc(dtFT).Ticks, DateTimeKind.Local); - - PDH_FMT_COUNTERVALUE_DOUBLE fmtValueDouble; - res = PdhGetFormattedCounterValue(hCounter, - PdhFormat.PDH_FMT_DOUBLE | PdhFormat.PDH_FMT_NOCAP100, - out counterTypePtr, - out fmtValueDouble); - if (res == PdhResults.PDH_INVALID_DATA || res == PdhResults.PDH_NO_DATA) - { - // Console.WriteLine ("PdhGetFormattedCounterValue returned " + res); - samplesArr[sampleIndex++] = new PerformanceCounterSample(path, - _consumerPathToHandleAndInstanceMap[path].InstanceName, - 0, - (ulong)rawValue.FirstValue, - (ulong)rawValue.SecondValue, - rawValue.MultiCount, - (PerformanceCounterType)counterType, - defaultScale, - timeBase, - sampleTimeStamp, - (UInt64)dtFT, - fmtValueDouble.CStatus); - - numInvalidDataSamples++; - lastErr = res; - continue; - } - else if (res != 0) - { - // Console.WriteLine ("PdhGetFormattedCounterValue returned " + res); - return res; - } - - samplesArr[sampleIndex++] = new PerformanceCounterSample(path, - _consumerPathToHandleAndInstanceMap[path].InstanceName, - fmtValueDouble.doubleValue, - (ulong)rawValue.FirstValue, - (ulong)rawValue.SecondValue, - rawValue.MultiCount, - (PerformanceCounterType)counterTypePtr.ToInt32(), - defaultScale, - timeBase, - sampleTimeStamp, - (UInt64)dtFT, - fmtValueDouble.CStatus); - } - - // - // Prior to Vista, PdhCollectQueryDataWithTime() was not available, - // so we could not collect a timestamp for the entire sample set. - // We will use the last sample's timestamp instead. - // - nextSet = new PerformanceCounterSampleSet(sampleTimeStamp, samplesArr, _firstReading); - _firstReading = false; - - if (numInvalidDataSamples == samplesArr.Length) - { - res = lastErr; - } - else - { - // - // Reset the error - any errors are saved per sample in PerformanceCounterSample.Status for kvetching later - // - res = 0; - } - - return res; + // API not available to retrieve + return string.Empty; } public uint ReadNextSet(out PerformanceCounterSampleSet nextSet, bool bSkipReading) { Debug.Assert(_hQuery != null && !_hQuery.IsInvalid); - if (_isPreVista) - { - return ReadNextSetPreVista(out nextSet, bSkipReading); - } - uint res = 0; nextSet = null; @@ -1661,29 +1281,6 @@ namespace Microsoft.Powershell.Commands.GetCounter.PdhNative return res; } - public uint GetFilesSummary(out CounterFileInfo summary) - { - IntPtr pNumEntries = new IntPtr(0); - PDH_TIME_INFO pInfo = new PDH_TIME_INFO(); - IntPtr bufSize = new IntPtr(System.Runtime.InteropServices.Marshal.SizeOf(pInfo)); - - uint res = PdhGetDataSourceTimeRangeH(_hDataSource, - ref pNumEntries, - ref pInfo, - ref bufSize); - if (res != 0) - { - summary = new CounterFileInfo(); - return res; - } - - summary = new CounterFileInfo(new DateTime(DateTime.FromFileTimeUtc(pInfo.StartTime).Ticks, DateTimeKind.Local), - new DateTime(DateTime.FromFileTimeUtc(pInfo.EndTime).Ticks, DateTimeKind.Local), - pInfo.SampleCount); - - return res; - } - public uint ExpandWildCardPath(string path, out StringCollection expandedPaths) { expandedPaths = new StringCollection(); @@ -1718,44 +1315,5 @@ namespace Microsoft.Powershell.Commands.GetCounter.PdhNative return res; } - - public void ResetRelogValues() - { - Debug.Assert(_hOutputLog != null && !_hOutputLog.IsInvalid); - PdhResetRelogCounterValues(_hOutputLog); - } - - public uint WriteRelogSample(DateTime timeStamp) - { - Debug.Assert(_hOutputLog != null && !_hOutputLog.IsInvalid); - return PdhWriteRelogSample(_hOutputLog, (new DateTime(timeStamp.Ticks, DateTimeKind.Utc)).ToFileTimeUtc()); - } - - public uint SetCounterValue(PerformanceCounterSample sample, out bool bUnknownPath) - { - Debug.Assert(_hOutputLog != null && !_hOutputLog.IsInvalid); - - bUnknownPath = false; - - string lcPath = sample.Path.ToLowerInvariant(); - - if (!_reloggerPathToHandleAndInstanceMap.ContainsKey(lcPath)) - { - bUnknownPath = true; - return 0; - } - - PDH_RAW_COUNTER rawStruct = new PDH_RAW_COUNTER(); - rawStruct.FirstValue = (long)sample.RawValue; - rawStruct.SecondValue = (long)sample.SecondValue; - rawStruct.MultiCount = sample.MultipleCount; - rawStruct.TimeStamp.dwHighDateTime = (int)((new DateTime(sample.Timestamp.Ticks, DateTimeKind.Utc).ToFileTimeUtc() >> 32) & 0xFFFFFFFFL); - rawStruct.TimeStamp.dwLowDateTime = (int)(new DateTime(sample.Timestamp.Ticks, DateTimeKind.Utc).ToFileTimeUtc() & 0xFFFFFFFFL); - rawStruct.CStatus = sample.Status; - - return PdhSetCounterValue(_reloggerPathToHandleAndInstanceMap[lcPath].hCounter, - ref rawStruct, /*PPDH_RAW_COUNTER */ - _reloggerPathToHandleAndInstanceMap[lcPath].InstanceName); - } } } diff --git a/src/Modules/Windows/Microsoft.PowerShell.Diagnostics/Microsoft.PowerShell.Diagnostics.psd1 b/src/Modules/Windows/Microsoft.PowerShell.Diagnostics/Microsoft.PowerShell.Diagnostics.psd1 index fc22fe605..8739c54be 100644 --- a/src/Modules/Windows/Microsoft.PowerShell.Diagnostics/Microsoft.PowerShell.Diagnostics.psd1 +++ b/src/Modules/Windows/Microsoft.PowerShell.Diagnostics/Microsoft.PowerShell.Diagnostics.psd1 @@ -7,7 +7,7 @@ ModuleVersion="7.0.0.0" CompatiblePSEditions = @("Core") PowerShellVersion="3.0" FunctionsToExport = @() -CmdletsToExport="Get-WinEvent", "New-WinEvent" # Counter CmdLets Disabled #4272: "Get-Counter", "Import-Counter", "Export-Counter" +CmdletsToExport="Get-WinEvent", "New-WinEvent", "Get-Counter" AliasesToExport = @() NestedModules="Microsoft.PowerShell.Commands.Diagnostics.dll" TypesToProcess="GetEvent.types.ps1xml" diff --git a/test/powershell/Modules/Microsoft.PowerShell.Diagnostics/Get-Counter.Tests.ps1 b/test/powershell/Modules/Microsoft.PowerShell.Diagnostics/Get-Counter.Tests.ps1 index 6489ae65b..9d378908e 100644 --- a/test/powershell/Modules/Microsoft.PowerShell.Diagnostics/Get-Counter.Tests.ps1 +++ b/test/powershell/Modules/Microsoft.PowerShell.Diagnostics/Get-Counter.Tests.ps1 @@ -1,13 +1,5 @@ # Copyright (c) Microsoft Corporation. All rights reserved. # Licensed under the MIT License. -<############################################################################################ - # File: Get-Counter.Tests.ps1 - # Provides Pester tests for the Get-Counter cmdlet. - ############################################################################################> - -# Counter CmdLets are removed see issue #4272 -# Tests are disabled - return $cmdletName = "Get-Counter" diff --git a/test/powershell/engine/Basic/DefaultCommands.Tests.ps1 b/test/powershell/engine/Basic/DefaultCommands.Tests.ps1 index 27980fcef..0f07090ff 100644 --- a/test/powershell/engine/Basic/DefaultCommands.Tests.ps1 +++ b/test/powershell/engine/Basic/DefaultCommands.Tests.ps1 @@ -266,7 +266,7 @@ Describe "Verify approved aliases list" -Tags "CI" { "Cmdlet", "Get-ComputerRestorePoint", "", $($FullCLR ), "", "", "" "Cmdlet", "Get-Content", "", $($FullCLR -or $CoreWindows -or $CoreUnix), "", "", "None" "Cmdlet", "Get-ControlPanelItem", "", $($FullCLR ), "", "", "" -"Cmdlet", "Get-Counter", "", $($FullCLR ), "", "", "" +"Cmdlet", "Get-Counter", "", $($FullCLR -or $CoreWindows ), "", "", "None" "Cmdlet", "Get-Credential", "", $($FullCLR -or $CoreWindows -or $CoreUnix), "", "", "None" "Cmdlet", "Get-Culture", "", $($FullCLR -or $CoreWindows -or $CoreUnix), "", "", "None" "Cmdlet", "Get-Date", "", $($FullCLR -or $CoreWindows -or $CoreUnix), "", "", "None" diff --git a/test/powershell/engine/Help/assets/HelpURI/V2Cmdlets.csv b/test/powershell/engine/Help/assets/HelpURI/V2Cmdlets.csv index 472fc70c0..f9beccad6 100644 --- a/test/powershell/engine/Help/assets/HelpURI/V2Cmdlets.csv +++ b/test/powershell/engine/Help/assets/HelpURI/V2Cmdlets.csv @@ -58,6 +58,7 @@ Get-ChildItem,https://go.microsoft.com/fwlink/?LinkID=113308 Get-Command,https://go.microsoft.com/fwlink/?LinkID=113309 Get-ComputerRestorePoint,https://go.microsoft.com/fwlink/?LinkID=135215 Get-Content,https://go.microsoft.com/fwlink/?LinkID=113310 +Get-Counter,https://go.microsoft.com/fwlink/?LinkID=2109647 Get-Credential,https://go.microsoft.com/fwlink/?LinkID=113311 Get-Culture,https://go.microsoft.com/fwlink/?LinkID=113312 Get-Date,https://go.microsoft.com/fwlink/?LinkID=113313