From 5a064078b2d6d58e738745f0e4f2d9d574828e83 Mon Sep 17 00:00:00 2001 From: Lord Hepipud Date: Wed, 29 Jul 2020 11:03:19 +0200 Subject: [PATCH] Adds new Cmdlet Show-IcingaPerformanceCounterInstances --- doc/31-Changelog.md | 4 ++ ...how-IcingaPerformanceCounterInstances.psm1 | 58 +++++++++++++++++++ 2 files changed, 62 insertions(+) create mode 100644 lib/core/perfcounter/Show-IcingaPerformanceCounterInstances.psm1 diff --git a/doc/31-Changelog.md b/doc/31-Changelog.md index 9b32a6e..abec908 100644 --- a/doc/31-Changelog.md +++ b/doc/31-Changelog.md @@ -15,6 +15,10 @@ Released closed milestones can be found on [GitHub](https://github.com/Icinga/ic * [#80](https://github.com/Icinga/icinga-powershell-framework/issues/80) Adds wrapper function `Get-IcingaWindowsInformation` for WMI and CIM calls to properly handle config/permission errors +### Enhancements + +* Adds new Cmdlet `Show-IcingaPerformanceCounterInstances` to display all avaiable instances for Performance Counters + ### Bugfixes * [#78](https://github.com/Icinga/icinga-powershell-framework/issues/78) Fix Icinga Agent package fetching for x86 architecture diff --git a/lib/core/perfcounter/Show-IcingaPerformanceCounterInstances.psm1 b/lib/core/perfcounter/Show-IcingaPerformanceCounterInstances.psm1 new file mode 100644 index 0000000..7bb9aba --- /dev/null +++ b/lib/core/perfcounter/Show-IcingaPerformanceCounterInstances.psm1 @@ -0,0 +1,58 @@ +<# +.SYNOPSIS + Displays all available instances for a provided Performance Counter +.DESCRIPTION + Displays all available instances for a provided Performance Counter +.FUNCTIONALITY + Displays all available instances for a provided Performance Counter +.PARAMETER Counter + The name of the Performance Counter to fetch data for +.EXAMPLE + PS>Show-IcingaPerformanceCounterInstances -Counter '\Processor(*)\% processor time'; + + Name Value + ---- ----- + _Total \Processor(_Total)\% processor time + 0 \Processor(0)\% processor time + 1 \Processor(1)\% processor time + 2 \Processor(2)\% processor time + 3 \Processor(3)\% processor time + ... +.LINK + https://github.com/Icinga/icinga-powershell-framework +#> + +function Show-IcingaPerformanceCounterInstances() +{ + param ( + [string]$Counter + ); + + [hashtable]$Instances = @{}; + + if ([string]::IsNullOrEmpty($Counter)) { + Write-IcingaConsoleError 'Please enter a Performance Counter'; + return; + } + + $PerfCounter = New-IcingaPerformanceCounter -Counter $Counter -SkipWait $TRUE; + + foreach ($entry in $PerfCounter.Counters) { + $Instances.Add( + $entry.Instance, + ($Counter.Replace('(*)', ([string]::Format('({0})', $entry.Instance)))) + ); + } + + if ($Instances.Count -eq 0) { + Write-IcingaConsoleNotice ` + -Message 'No instances were found for Performance Counter "{0}". Please ensure the provided counter has instances and you are using "*" for the instance name.' ` + -Objects $Counter; + + return; + } + + return ( + $Instances.GetEnumerator() | Sort-Object Name + ); +}