File size: 24,376 Bytes
8c763fb | 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 | // Copyright (c) Microsoft Corporation.
// Licensed under the MIT License.
using System;
using System.Collections;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.Collections.Specialized;
using System.ComponentModel;
using System.Diagnostics;
using System.Diagnostics.CodeAnalysis;
using System.Globalization;
using System.IO;
using System.Management.Automation;
using System.Management.Automation.Runspaces;
using System.Net;
using System.Reflection;
using System.Resources;
using System.Security;
using System.Security.Principal;
using System.Text;
using System.Threading;
using System.Xml;
using Microsoft.PowerShell.Commands.Diagnostics.Common;
using Microsoft.PowerShell.Commands.GetCounter;
using Microsoft.Powershell.Commands.GetCounter.PdhNative;
namespace Microsoft.PowerShell.Commands
{
///
/// Class that implements the Get-Counter cmdlet.
///
[Cmdlet(VerbsData.Import, "Counter", DefaultParameterSetName = "GetCounterSet", HelpUri = "https://go.microsoft.com/fwlink/?LinkID=138338")]
public sealed class ImportCounterCommand : PSCmdlet
{
//
// Path parameter
//
[Parameter(
Position = 0,
Mandatory = true,
ValueFromPipeline = true,
ValueFromPipelineByPropertyName = true,
HelpMessageBaseName = "GetEventResources")]
[Alias("PSPath")]
[SuppressMessage("Microsoft.Performance", "CA1819:PropertiesShouldNotReturnArrays",
Scope = "member",
Target = "Microsoft.PowerShell.Commands.GetCounterCommand.ListSet",
Justification = "A string[] is required here because that is the type Powershell supports")]
public string[] Path
{
get { return _path; }
set { _path = value; }
}
private string[] _path;
private StringCollection _resolvedPaths = new StringCollection();
private List<string> _accumulatedFileNames = new List<string>();
//
// ListSet parameter
//
[Parameter(
Mandatory = true,
ParameterSetName = "ListSetSet",
ValueFromPipeline = false,
ValueFromPipelineByPropertyName = false,
HelpMessageBaseName = "GetEventResources")]
[AllowEmptyCollection]
[SuppressMessage("Microsoft.Performance", "CA1819:PropertiesShouldNotReturnArrays",
Scope = "member",
Target = "Microsoft.PowerShell.Commands.GetCounterCommand.ListSet",
Justification = "A string[] is required here because that is the type Powershell supports")]
public string[] ListSet
{
get { return _listSet; }
set { _listSet = value; }
}
private string[] _listSet = Array.Empty<string>();
//
// StartTime parameter
//
[Parameter(
ValueFromPipeline = false,
ValueFromPipelineByPropertyName = false,
ParameterSetName = "GetCounterSet",
HelpMessageBaseName = "GetEventResources")]
public DateTime StartTime
{
get { return _startTime; }
set { _startTime = value; }
}
private DateTime _startTime = DateTime.MinValue;
//
// EndTime parameter
//
[Parameter(
ValueFromPipeline = false,
ValueFromPipelineByPropertyName = false,
ParameterSetName = "GetCounterSet",
HelpMessageBaseName = "GetEventResources")]
public DateTime EndTime
{
get { return _endTime; }
set { _endTime = value; }
}
private DateTime _endTime = DateTime.MaxValue;
//
// Counter parameter
//
[Parameter(
Mandatory = false,
ParameterSetName = "GetCounterSet",
ValueFromPipeline = false,
HelpMessageBaseName = "GetEventResources")]
[AllowEmptyCollection]
[SuppressMessage("Microsoft.Performance", "CA1819:PropertiesShouldNotReturnArrays",
Scope = "member",
Target = "Microsoft.PowerShell.Commands.GetCounterCommand.ListSet",
Justification = "A string[] is required here because that is the type Powershell supports")]
public string[] Counter
{
get { return _counter; }
set { _counter = value; }
}
private string[] _counter = Array.Empty<string>();
//
// Summary switch
//
[Parameter(ParameterSetName = "SummarySet")]
public SwitchParameter Summary
{
get { return _summary; }
set { _summary = value; }
}
private SwitchParameter _summary;
//
// MaxSamples parameter
//
private const Int64 KEEP_ON_SAMPLING = -1;
[Parameter(
ParameterSetName = "GetCounterSet",
ValueFromPipeline = false,
ValueFromPipelineByPropertyName = false,
HelpMessageBaseName = "GetEventResources")]
[ValidateRange((Int64)1, Int64.MaxValue)]
public Int64 MaxSamples
{
get { return _maxSamples; }
set { _maxSamples = value; }
}
private Int64 _maxSamples = KEEP_ON_SAMPLING;
private ResourceManager _resourceMgr = null;
private PdhHelper _pdhHelper = null;
private bool _stopping = false;
//
// AccumulatePipelineFileNames() accumulates counter file paths in the pipeline scenario:
// we do not want to construct a Pdh query until all the file names are supplied.
//
private void AccumulatePipelineFileNames()
{
_accumulatedFileNames.AddRange(_path);
}
//
// BeginProcessing() is invoked once per pipeline
//
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
_resourceMgr = Microsoft.PowerShell.Commands.Diagnostics.Common.CommonUtilities.GetResourceManager();
}
//
// EndProcessing() is invoked once per pipeline
//
protected override void EndProcessing()
{
//
// Resolve and validate the Path argument: present for all parametersets.
//
if (!ResolveFilePaths())
{
return;
}
ValidateFilePaths();
switch (ParameterSetName)
{
case "ListSetSet":
ProcessListSet();
break;
case "GetCounterSet":
ProcessGetCounter();
break;
case "SummarySet":
ProcessSummary();
break;
default:
Debug.Assert(false, $"Invalid parameter set name: {ParameterSetName}");
break;
}
_pdhHelper.Dispose();
}
//
// Handle Control-C
//
protected override void StopProcessing()
{
_stopping = true;
_pdhHelper.Dispose();
}
//
// ProcessRecord() override.
// This is the main entry point for the cmdlet.
//
protected override void ProcessRecord()
{
AccumulatePipelineFileNames();
}
//
// ProcessSummary().
// Does the work to process Summary parameter set.
//
private void ProcessSummary()
{
uint res = _pdhHelper.ConnectToDataSource(_resolvedPaths);
if (res != 0)
{
ReportPdhError(res, true);
return;
}
CounterFileInfo summaryObj;
res = _pdhHelper.GetFilesSummary(out summaryObj);
if (res != 0)
{
ReportPdhError(res, true);
return;
}
WriteObject(summaryObj);
}
//
// ProcessListSet().
// Does the work to process ListSet parameter set.
//
private void ProcessListSet()
{
uint res = _pdhHelper.ConnectToDataSource(_resolvedPaths);
if (res != 0)
{
ReportPdhError(res, true);
return;
}
StringCollection machineNames = new StringCollection();
res = _pdhHelper.EnumBlgFilesMachines(ref machineNames);
if (res != 0)
{
ReportPdhError(res, true);
return;
}
foreach (string machine in machineNames)
{
StringCollection counterSets = new StringCollection();
res = _pdhHelper.EnumObjects(machine, ref counterSets);
if (res != 0)
{
return;
}
StringCollection validPaths = new StringCollection();
foreach (string pattern in _listSet)
{
bool bMatched = false;
WildcardPattern wildLogPattern = new WildcardPattern(pattern, WildcardOptions.IgnoreCase);
foreach (string counterSet in counterSets)
{
if (!wildLogPattern.IsMatch(counterSet))
{
continue;
}
StringCollection counterSetCounters = new StringCollection();
StringCollection counterSetInstances = new StringCollection();
res = _pdhHelper.EnumObjectItems(machine, counterSet, ref counterSetCounters, ref counterSetInstances);
if (res != 0)
{
ReportPdhError(res, false);
continue;
}
string[] instanceArray = new string[counterSetInstances.Count];
int i = 0;
foreach (string instance in counterSetInstances)
{
instanceArray[i++] = instance;
}
Dictionary<string, string[]> counterInstanceMapping = new Dictionary<string, string[]>();
foreach (string counter in counterSetCounters)
{
counterInstanceMapping.Add(counter, instanceArray);
}
PerformanceCounterCategoryType categoryType = PerformanceCounterCategoryType.Unknown;
if (counterSetInstances.Count > 1)
{
categoryType = PerformanceCounterCategoryType.MultiInstance;
}
else // if (counterSetInstances.Count == 1) //???
{
categoryType = PerformanceCounterCategoryType.SingleInstance;
}
string setHelp = _pdhHelper.GetCounterSetHelp(machine, counterSet);
CounterSet setObj = new CounterSet(counterSet, machine, categoryType, setHelp, ref counterInstanceMapping);
WriteObject(setObj);
bMatched = true;
}
if (!bMatched)
{
string msg = _resourceMgr.GetString("NoMatchingCounterSetsInFile");
Exception exc = new Exception(string.Format(CultureInfo.InvariantCulture, msg,
CommonUtilities.StringArrayToString(_resolvedPaths),
pattern));
WriteError(new ErrorRecord(exc, "NoMatchingCounterSetsInFile", ErrorCategory.ObjectNotFound, null));
}
}
}
}
//
// ProcessGetCounter()
// Does the work to process GetCounterSet parameter set.
//
private void ProcessGetCounter()
{
// Validate StartTime-EndTime, if present
if (_startTime != DateTime.MinValue || _endTime != DateTime.MaxValue)
{
if (_startTime >= _endTime)
{
string msg = string.Format(CultureInfo.InvariantCulture, _resourceMgr.GetString("CounterInvalidDateRange"));
Exception exc = new Exception(msg);
ThrowTerminatingError(new ErrorRecord(exc, "CounterInvalidDateRange", ErrorCategory.InvalidArgument, null));
return;
}
}
uint res = _pdhHelper.ConnectToDataSource(_resolvedPaths);
if (res != 0)
{
ReportPdhError(res, true);
return;
}
StringCollection validPaths = new StringCollection();
if (_counter.Length > 0)
{
foreach (string path in _counter)
{
StringCollection expandedPaths;
res = _pdhHelper.ExpandWildCardPath(path, out expandedPaths);
if (res != 0)
{
WriteDebug(path);
ReportPdhError(res, false);
continue;
}
foreach (string expandedPath in expandedPaths)
{
if (!_pdhHelper.IsPathValid(expandedPath))
{
string msg = string.Format(CultureInfo.InvariantCulture, _resourceMgr.GetString("CounterPathIsInvalid"), path);
Exception exc = new Exception(msg);
WriteError(new ErrorRecord(exc, "CounterPathIsInvalid", ErrorCategory.InvalidResult, null));
continue;
}
validPaths.Add(expandedPath);
}
}
if (validPaths.Count == 0)
{
return;
}
}
else
{
res = _pdhHelper.GetValidPathsFromFiles(ref validPaths);
if (res != 0)
{
ReportPdhError(res, false);
}
}
if (validPaths.Count == 0)
{
string msg = string.Format(CultureInfo.InvariantCulture, _resourceMgr.GetString("CounterPathsInFilesInvalid"));
Exception exc = new Exception(msg);
ThrowTerminatingError(new ErrorRecord(exc, "CounterPathsInFilesInvalid", ErrorCategory.InvalidResult, null));
}
res = _pdhHelper.OpenQuery();
if (res != 0)
{
ReportPdhError(res, false);
}
if (_startTime != DateTime.MinValue || _endTime != DateTime.MaxValue)
{
res = _pdhHelper.SetQueryTimeRange(_startTime, _endTime);
if (res != 0)
{
ReportPdhError(res, true);
}
}
res = _pdhHelper.AddCounters(ref validPaths, true);
if (res != 0)
{
ReportPdhError(res, true);
}
PerformanceCounterSampleSet nextSet;
uint samplesRead = 0;
while (!_stopping)
{
res = _pdhHelper.ReadNextSet(out nextSet, false);
if (res == PdhResults.PDH_NO_MORE_DATA)
{
break;
}
if (res != 0 && res != PdhResults.PDH_INVALID_DATA)
{
ReportPdhError(res, false);
continue;
}
//
// Display data
//
WriteSampleSetObject(nextSet, (samplesRead == 0));
samplesRead++;
if (_maxSamples != KEEP_ON_SAMPLING && samplesRead >= _maxSamples)
{
break;
}
}
}
//
// ValidateFilePaths() helper.
// Validates the _resolvedPaths: present for all parametersets.
// We cannot have more than 32 blg files, or more than one CSV or TSC file.
// Files have to all be of the same type (.blg, .csv, .tsv).
//
private void ValidateFilePaths()
{
Debug.Assert(_resolvedPaths.Count > 0);
string firstExt = System.IO.Path.GetExtension(_resolvedPaths[0]);
foreach (string fileName in _resolvedPaths)
{
WriteVerbose(fileName);
string curExtension = System.IO.Path.GetExtension(fileName);
if (!curExtension.Equals(".blg", StringComparison.OrdinalIgnoreCase)
&& !curExtension.Equals(".csv", StringComparison.OrdinalIgnoreCase)
&& !curExtension.Equals(".tsv", StringComparison.OrdinalIgnoreCase))
{
string msg = string.Format(CultureInfo.InvariantCulture, _resourceMgr.GetString("CounterNotALogFile"), fileName);
Exception exc = new Exception(msg);
ThrowTerminatingError(new ErrorRecord(exc, "CounterNotALogFile", ErrorCategory.InvalidResult, null));
return;
}
if (!curExtension.Equals(firstExt, StringComparison.OrdinalIgnoreCase))
{
string msg = string.Format(CultureInfo.InvariantCulture, _resourceMgr.GetString("CounterNoMixedLogTypes"), fileName);
Exception exc = new Exception(msg);
ThrowTerminatingError(new ErrorRecord(exc, "CounterNoMixedLogTypes", ErrorCategory.InvalidResult, null));
return;
}
}
if (firstExt.Equals(".blg", StringComparison.OrdinalIgnoreCase))
{
if (_resolvedPaths.Count > 32)
{
string msg = string.Format(CultureInfo.InvariantCulture, _resourceMgr.GetString("Counter32FileLimit"));
Exception exc = new Exception(msg);
ThrowTerminatingError(new ErrorRecord(exc, "Counter32FileLimit", ErrorCategory.InvalidResult, null));
return;
}
}
else if (_resolvedPaths.Count > 1)
{
string msg = string.Format(CultureInfo.InvariantCulture, _resourceMgr.GetString("Counter1FileLimit"));
Exception exc = new Exception(msg);
ThrowTerminatingError(new ErrorRecord(exc, "Counter1FileLimit", ErrorCategory.InvalidResult, null));
return;
}
}
//
// ResolveFilePath helper.
// Returns a string collection of resolved file paths.
// Writes non-terminating errors for invalid paths
// and returns an empty collection.
//
private bool ResolveFilePaths()
{
StringCollection retColl = new StringCollection();
foreach (string origPath in _accumulatedFileNames)
{
Collection<PathInfo> resolvedPathSubset = null;
try
{
resolvedPathSubset = SessionState.Path.GetResolvedPSPathFromPSPath(origPath);
}
catch (PSNotSupportedException notSupported)
{
WriteError(new ErrorRecord(notSupported, string.Empty, ErrorCategory.ObjectNotFound, origPath));
continue;
}
catch (System.Management.Automation.DriveNotFoundException driveNotFound)
{
WriteError(new ErrorRecord(driveNotFound, string.Empty, ErrorCategory.ObjectNotFound, origPath));
continue;
}
catch (ProviderNotFoundException providerNotFound)
{
WriteError(new ErrorRecord(providerNotFound, string.Empty, ErrorCategory.ObjectNotFound, origPath));
continue;
}
catch (ItemNotFoundException pathNotFound)
{
WriteError(new ErrorRecord(pathNotFound, string.Empty, ErrorCategory.ObjectNotFound, origPath));
continue;
}
catch (Exception exc)
{
WriteError(new ErrorRecord(exc, string.Empty, ErrorCategory.ObjectNotFound, origPath));
continue;
}
foreach (PathInfo pi in resolvedPathSubset)
{
//
// Check the provider: only FileSystem provider paths are acceptable.
//
if (pi.Provider.Name != "FileSystem")
{
string msg = _resourceMgr.GetString("NotAFileSystemPath");
Exception exc = new Exception(string.Format(CultureInfo.InvariantCulture, msg, origPath));
WriteError(new ErrorRecord(exc, "NotAFileSystemPath", ErrorCategory.InvalidArgument, origPath));
continue;
}
_resolvedPaths.Add(pi.ProviderPath.ToLowerInvariant());
}
}
return (_resolvedPaths.Count > 0);
}
private void ReportPdhError(uint res, bool bTerminate)
{
string msg;
uint formatRes = CommonUtilities.FormatMessageFromModule(res, "pdh.dll", out msg);
if (formatRes != 0)
{
msg = string.Format(CultureInfo.InvariantCulture, _resourceMgr.GetString("CounterApiError"), res);
}
Exception exc = new Exception(msg);
if (bTerminate)
{
ThrowTerminatingError(new ErrorRecord(exc, "CounterApiError", ErrorCategory.InvalidResult, null));
}
else
{
WriteError(new ErrorRecord(exc, "CounterApiError", ErrorCategory.InvalidResult, null));
}
}
//
// WriteSampleSetObject() helper.
// In addition to writing the PerformanceCounterSampleSet object,
// it writes a single error if one of the samples has an invalid (non-zero) status.
// The only exception is the first set, where we allow for the formatted value to be 0 -
// this is expected for CSV and TSV files.
private void WriteSampleSetObject(PerformanceCounterSampleSet set, bool firstSet)
{
if (!firstSet)
{
foreach (PerformanceCounterSample sample in set.CounterSamples)
{
if (sample.Status != 0)
{
string msg = string.Format(CultureInfo.InvariantCulture, _resourceMgr.GetString("CounterSampleDataInvalid"));
Exception exc = new Exception(msg);
WriteError(new ErrorRecord(exc, "CounterApiError", ErrorCategory.InvalidResult, null));
break;
}
}
}
WriteObject(set);
}
}
}
|