0

I wrote a PowerShell script that looks like this:

foreach ($li in  $list) {
    try {
        #check ID in the hashtable for new topics
        if ($TopicUpdates.ContainsKey($li["ID"].ToString())) {
            $li["Topics"] = $TopicUpdates[($li["ID"].ToString())]
            $li.SystemUpdate($false)
        } else {
            $li["Topics"] = 'About'
            $temp = "" | select "Title", "ID"
            $temp.Title= $li["Title"]
            $temp.ID= $li["ID"]

            if ($li.File.CheckOutStatus -eq "None") {
                $li.SystemUpdate($false)
            } else {
                $CheckedOutAlbums.Add($temp)
            }

            $NonExistingTopics.Add($temp)
        }
    } catch {
        $isError = $true
        $time = Get-Date
        $ErrorMessage = $_.Exception.Message
        "Error Occurred ($time) with error message: $ErrorMessage" |
            Out-File ".\error.log" -Append
    }
}

I noticed that whilst the script is running, it logs integers in the console:

1
2
3
etc.

What could cause this? I haven't written any Write-Host statements.

Ansgar Wiechers
  • 193,178
  • 25
  • 254
  • 328
bier hier
  • 20,970
  • 42
  • 97
  • 166
  • Its hard to say without seeing your full code. Some methods generate output. It isn't only `write-host` that can cause output to occur, anything output to the pipeline goes to the host by default (via `out-host`). – Mark Wragg May 03 '18 at 08:16
  • I put a breakpoint on the foreach statement and it generates a log. – bier hier May 03 '18 at 08:18
  • If you try `foreach ($li in 1..10) { $li }`, you'll see PowerShell writes output despite not having any `Write-Host`. Most likely, something you've hidden under `//do something updates or something` has code that's inadvertently not just a statement but an expression that produces a result. `foreach` itself does not write output; you can verify that with `foreach ($li in 1..10) { }`. – Jeroen Mostert May 03 '18 at 08:25
  • You're probably doing something like adding elements to an ArrayList, which outputs the index of the added item. For further help please show the loop body. The problem has nothing to do with the loop itself. – Ansgar Wiechers May 03 '18 at 08:29
  • @AnsgarWiechers I updated the code sample. – bier hier May 03 '18 at 08:33
  • See: https://stackoverflow.com/questions/10832000/best-way-to-write-to-the-console-in-powershell as well. – TessellatingHeckler May 03 '18 at 08:37

1 Answers1

3

These

$CheckedOutAlbums.Add($temp) 
$NonExistingTopics.Add($temp)
$li.SystemUpdate($false)

Likely return a value. By default all 'free' values in a powershell script are sent to the pipeline, and end up on the screen (or redirected to a file, etc).

Change them to one of these:

$null = $CheckedOutAlbums.Add($temp) 

[void]$CheckedOutAlbums.Add($temp) 

$CheckedOutAlbums.Add($temp) | out-null

to suppress the return value going to the output stream.

TessellatingHeckler
  • 27,511
  • 4
  • 48
  • 87
  • Might be useful to note that the last one is significantly slower than the first two. If it's inside a loop, I'd avoid piping to `Out-Null`. – Bacon Bits May 03 '18 at 12:04