0

According to my last question Compare two Files I finally managed to get all to work inside 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. The problem now is that the -Raw paramterer of this code:

$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.

SRel
  • 383
  • 1
  • 11
  • 5
    For the love of everything holy, I implore you to abandon this wicked quest and help your supervisor upgrade Windows before an accident occurs ^_^ – Mathias R. Jessen May 26 '20 at 16:18
  • I will do so :) We are working in a sensitive environment, I am a bit nervous that installing .net framework may harm his PC. Or is my fear Unjustified? – SRel May 26 '20 at 16:21
  • 3
    WMF 5.1 is _5 years old_ - if your manager is running Windows 7, _that_ should be your biggest worry :) – Mathias R. Jessen May 26 '20 at 16:23

1 Answers1

6

Before jumping to the answer, let me just suggest one actual solution:

Abandon PowerShell 2.0 ASAP!

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.

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].

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.

So, to solve this, remove the GetEnumerator() call on $counts in the last statement:

$counts = $counts.GetEnumerator() | sort name
$counts.GetEnumerator() |ForEach-Object {$_.Key, $_.Value -join '' } |Set-Content C:/pbr_tmp/PBreport/trace_results.txt
Mathias R. Jessen
  • 157,619
  • 12
  • 148
  • 206
  • Thank you :) this works. However it seems that some behaviour of the commands has changed as the output only contains a number value. Before (running in PSV5) i received on the left the searchname and one the right the countvalue. Maybe my only way is to install PS5 :) – SRel May 26 '20 at 16:33
  • It might be that `Key` on the key-value pairs is actually `Name` in 2.0, I'm afraid I don't have access to test that atm. – Mathias R. Jessen May 26 '20 at 16:35