Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/powershell/11.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/oop/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Powershell优雅地枚举了几个变量_Powershell - Fatal编程技术网

Powershell优雅地枚举了几个变量

Powershell优雅地枚举了几个变量,powershell,Powershell,我有一个文本输出,它显示我在脚本中所做的每个选择的运行时 检查级别:0,38.99607466333 检查级别:160.9354064605553 等等 我想做的是有一些读取主机行,显示我想进入的级别的选择,旁边的变量显示平均需要多长时间,即“Checklevel 1需要60分钟” 下面的脚本很有效,但我不禁想到还有更好的选择: $CheckLevel0 = Get-Content $RuntimeFile | Where {$_ -like "Check Level: 0,*&quo

我有一个文本输出,它显示我在脚本中所做的每个选择的运行时

检查级别:0,38.99607466333

检查级别:160.9354064605553

等等

我想做的是有一些读取主机行,显示我想进入的级别的选择,旁边的变量显示平均需要多长时间,即“Checklevel 1需要60分钟”

下面的脚本很有效,但我不禁想到还有更好的选择:

$CheckLevel0 = Get-Content $RuntimeFile | Where {$_ -like "Check Level: 0,*"}
$CheckLevel1 = Get-Content $RuntimeFile | Where {$_ -like "Check Level: 1,*"}
$CheckLevel2 = Get-Content $RuntimeFile | Where {$_ -like "Check Level: 2,*"}
$CheckLevel3 = Get-Content $RuntimeFile | Where {$_ -like "Check Level: 3,*"}
$CheckLevel4 = Get-Content $RuntimeFile | Where {$_ -like "Check Level: 4,*"}
$CheckLevel5 = Get-Content $RuntimeFile | Where {$_ -like "Check Level: 5,*"}
理想情况下,我希望所有$CheckLevelx变量都填充一行或两行。。。我尝试过各种方法。

Whlist gvee的解决方案简单而优雅,如果你想显示一个显示执行时间的菜单,它就不起作用了

也就是说,您正在寻找一个更简单的解决方案。当一个人有三个以上的变量时,比如value0,value1。。。valuen,通常是时候使用数据结构了。数组是一个明显的选择,通常哈希表也可以。通过.Net,有更多的特殊需求

如果需要对数据文件进行更复杂的处理,请考虑预处理。让我们像这样使用正则表达式和哈希表

# Some dummy data. Note the duplicate entry for level 1
$d = ('Check Level: 0, 38.99607466333333',` 
'Check Level: 1, 60.93540646055553',`
'Check Level: 2, 34.43543543967473',`
'Check Level: 1, 99.99990646055553')

# A regular expression to match strings
$rex = [regex]::new('Check Level: (\d+),.*')

# Populate a hashtable with contents
$ht = @{}
$d | % {
    $level = $rex.Match($_).groups[1].value
    $line = $rex.Match($_).groups[0].value
    if( $ht.ContainsKey($level)) {
        # Handle duplicates here.
        $ht[$level] = $line
    }
    else {
        $ht.Add($level, $line)
    }
}
# Print the hashtable in key order.
$ht.GetEnumerator() | sort

Name                           Value
----                           -----
0                              Check Level: 0, 38.99607466333333
1                              Check Level: 1, 99.99990646055553
2                              Check Level: 2, 34.43543543967473

是否会有重复的检查级别n条目?
$n=读取主机-提示“n”;获取内容$RuntimeFile |其中{$\类似于“检查级别:$n,*”}