[英]Conversion from Powershell V5 to Powershell V2
According to my last question Compare two Files I finally managed to get all to work inside my.bat.根据我的最后一个问题比较两个文件,我终于设法让所有在 my.bat 中工作。 Thanks again for all your support.再次感谢大家的支持。
However, as I find out today my supervisor is using Powershell in Version 2 instead of 5.1 than I do.但是,正如我今天发现的那样,我的主管在版本 2 中使用 Powershell 而不是 5.1。 The problem now is that the -Raw paramterer of this code:现在的问题是此代码的 -Raw 参数:
$target = Get-Content "C:/pbr_tmp/PBreport/trc/TlsTrace.prn" -Raw
I changed this to:我将其更改为:
$target = [System.IO.File]::ReadAllText("C:/pbr_tmp/PBreport/trc/TlsTrace.prn")
Unfortunately, the results are incorrect and I receive following error:不幸的是,结果不正确,我收到以下错误:
GetEnumerator : Fehler beim Aufrufen der Methode, da [System.Collections.DictionaryEntry] keine Methode mit dem Namen "GetEnumerator" enthält.
Does anyone know if there is something in the complete code snippet which can cause this?有谁知道完整的代码片段中是否有可能导致这种情况的东西?
$data = Import-Csv "C:/trc/ModulID.txt" -Delimiter ";" -Header ID,Term
$target = [System.IO.File]::ReadAllText("C:/pbr_tmp/PBreport/trc/TlsTrace.prn")
$counts = @{}
foreach ($term in $data.Term) {
$term = $term + " "
$index = -1
$count = 0
do {
$index = $target.IndexOf($term, $index + 1)
if ($index -gt -1) { $count++ } else { break; }
} while ($true);
if ($count -gt 0) {$counts[$term] = $count}
}
$counts = $counts.GetEnumerator() | sort name
$counts.GetEnumerator() |ForEach-Object {$_.Key, $_.Value -join '' } |Set-Content C:/pbr_tmp/PBreport/trace_results.txt
For example,例如,
$counts = $counts.GetEnumerator() | sort name
Throws no exception.不抛出异常。
Before jumping to the answer, let me just suggest one actual solution :在跳到答案之前,让我建议一个实际的解决方案:
It's old, it's slow, it doesn't have the nice features you're used to and it doesn't ship with the logging features that actually make PowerShell >5 what we might call a securable runtime.它很旧,很慢,没有你习惯的很好的功能,也没有实际使 PowerShell >5 的日志记录功能,我们可以称之为安全运行时。
If the choice of operating environment is not yours to influence, read ahead below如果操作环境的选择不受您的影响,请阅读下文
After the loop ends, $counts
holds an object of type [hashtable]
.循环结束后, $counts
保存一个[hashtable]
类型的 object 。
But after running this statement:但运行此语句后:
$counts = $counts.GetEnumerator() | sort name
$counts
is no longer a [hashtable]
- it's an array of individual key-value entries, as spat out by the enumerator. $counts
不再是[hashtable]
- 它是由枚举器吐出的单个键值条目的数组。
So, to solve this, remove the GetEnumerator()
call on $counts
in the last statement:因此,要解决这个问题,请删除最后一条语句中对$counts
的GetEnumerator()
调用:
$counts = $counts.GetEnumerator() | sort name
$counts.GetEnumerator() |ForEach-Object {$_.Key, $_.Value -join '' } |Set-Content C:/pbr_tmp/PBreport/trace_results.txt
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.