Есть ли способ объединить массивы в PowerShell, аналогично объединению столбцов DataFrame в Python Pandas с concat
или в R с cbind
вместо перебора каждого элемента?
Ниже приведен воспроизводимый пример того, чтосвязывает четыре массива вместе как четыре столбца в объекте PowerShell.Как бы мне избавиться от цикла for
и получить те же результаты?
$LogicalProcessors = (Get-WmiObject –class Win32_processor
-Property NumberOfLogicalProcessors).NumberOfLogicalProcessors;
function myTop([String]$SortCol='CPU', [Int32]$top=30) {
$NameArray = get-counter '\Process(*)\ID Process' -EA SilentlyContinue |
Select -Expand CounterSamples | Select InstanceName, CookedValue
$CpuArray = get-counter '\Process(*)\% Processor Time' -EA SilentlyContinue |
Select -Expand CounterSamples | Select CookedValue
$MemArray = get-counter '\Process(*)\Working Set - Private' -EA SilentlyContinue |
Select -Expand CounterSamples | Select CookedValue
$TopTable = For ($i=0; $i -lt $NameArray.Length; $i++) {
if ($NameArray[$i].InstanceName -eq '_total') {continue}
if ($NameArray[$i].InstanceName -eq 'memory compression') {continue}
if ($NameArray[$i].InstanceName -eq 'idle') {
$CPU = ($CpuArray[$i].CookedValue) / $LogicalProcessors;
} else {
$CPU = $CpuArray[$i].CookedValue;
}
[PSCustomObject]@{
Name = $NameArray[$i].InstanceName;
ID = $NameArray[$i].CookedValue;
CPU = $CPU;
Memory = $MemArray[$i].CookedValue;
}
}
$TopTable | sort -des $SortCol | select -f $top |`
select Name, ID,`
@{Name='CPU'; Expression = {("{0:N1}%" -f $_.CPU) } },`
@{Name='Memory'; Expression = {("{0:N0} K" -f ($_.Memory /1kb) )} }
}
myTop -SortCol Memory -top 30 | ft -a