在PowerShell中连接阵列,类似于连接数据帧列
有没有一种方法可以在PowerShell中连接数组,类似于在Python Pandas中用在PowerShell中连接阵列,类似于连接数据帧列,powershell,optimization,vectorization,Powershell,Optimization,Vectorization,有没有一种方法可以在PowerShell中连接数组,类似于在Python Pandas中用concat连接数据帧列,或者在R中用cbind连接数据帧列,而不是遍历每个项 下面是一个可复制的示例,它将四个数组作为PowerShell对象中的四列绑定在一起。如何摆脱for循环并获得相同的结果 $LogicalProcessors=(获取WmiObject–类Win32\U处理器) -属性NumberOfLogicalProcessors); 函数myTop([String]$SortCol='CPU
concat
连接数据帧列,或者在R中用cbind
连接数据帧列,而不是遍历每个项
下面是一个可复制的示例,它将四个数组作为PowerShell对象中的四列绑定在一起。如何摆脱for
循环并获得相同的结果
$LogicalProcessors=(获取WmiObject–类Win32\U处理器)
-属性NumberOfLogicalProcessors);
函数myTop([String]$SortCol='CPU',[Int32]$top=30){
$NameArray=get counter'\Process(*)\ID Process'-EA SilentlyContinue|
选择-展开反采样|选择InstanceName,CookedValue
$CpuArray=get counter'\Process(*)\%Processor Time'-EA SilentlyContinue|
选择-展开反采样|选择CookedValue
$MemArray=get counter'\Process(*)\Working Set-Private'-EA SilentlyContinue |
选择-展开反采样|选择CookedValue
$TopTable=For($i=0;$i-lt$NameArray.Length;$i++){
if($NameArray[$i].InstanceName-eq'\u total'){continue}
if($NameArray[$i].InstanceName-eq‘内存压缩’){continue}
if($NameArray[$i].InstanceName-eq'idle'){
$CPU=($CpuArray[$i].CookedValue)/$LogicalProcessors;
}否则{
$CPU=$CpuArray[$i].CookedValue;
}
[PSCustomObject]@{
Name=$NameArray[$i].InstanceName;
ID=$NameArray[$i].CookedValue;
CPU=$CPU;
内存=$MemArray[$i].CookedValue;
}
}
$TopTable |排序-des$SortCol |选择-f$top|`
选择名称、ID、`
@{Name='CPU';表达式={(“{0:N1}%”-f$\u.CPU)}`
@{Name='Memory';Expression={(“{0:N0}K”-f($\.Memory/1kb))}
}
myTop-SortCol内存-最高30英尺-a
我认为PowerShell没有提供合并列的方法。在这种情况下,它可以通过组对象
进行对应
function myTop([string]$SortCol = "CPU", [int]$Top = 30)
{
$LogicalProcessors = (Get-WmiObject Win32_processor NumberOfLogicalProcessors).NumberOfLogicalProcessors
Get-Counter '\Process(*)\ID Process','\Process(*)\% Processor Time','\Process(*)\Working Set - Private' -ea SilentlyContinue |
foreach CounterSamples |
where InstanceName -notin "_total","memory compression" |
group { $_.Path.Split("\\")[3] } |
foreach {
[pscustomobject]@{
Name = $_.Group[0].InstanceName
ID = $_.Group[0].CookedValue
CPU = if($_.Name -eq "idle") { $_.Group[1].CookedValue / $LogicalProcessors } else { $_.Group[1].CookedValue }
Memory = $_.Group[2].CookedValue / 1KB
}
} |
sort -des $SortCol |
select -f $Top @(
"Name","ID"
@{ n = "CPU"; e = { ("{0:N1}%" -f $_.CPU) } }
@{ n = "Memory"; e = { ("{0:N0} K" -f $_.Memory) } }
)
}
myTop -SortCol Memory -top 10 | ft -a
对
Get Counter
的单次调用要比多次调用快得多。需要将CPU=if($\.Name-eq“idle”)
更改为CPU=if($\.Group[0].InstanceName-eq“idle”)
,以确保“idle”进程CPU除以逻辑处理器的数量。