1

I am new to powershell and i am trying to get the output of foreach loop into a file

the foreach loop looks like:

foreach ($element in $ashost)  {get_alerts($element)}

i am getting a list of alerts in to the standard output

so i have tried to write the for loop output to a file in the below way:

$output =
foreach ($element in $ashost)  {get_alerts($element)}|
$output | Out-File "C:\File.txt"

but the file is created empty

note : the "get_alerts" is a function to capture each alert which i didn't specify a particular return value its just printing to the screen from the Write-Host inside the function

koren ber
  • 41
  • 6
  • Are you able to modify `get_alerts` function? – Robert Dyjas Oct 16 '19 at 08:22
  • Possible duplicate of [Powershell Write-Host append to text file - computer name and time stamp](https://stackoverflow.com/questions/28812239/powershell-write-host-append-to-text-file-computer-name-and-time-stamp) – Robert Dyjas Oct 16 '19 at 08:30
  • If you _didn't specify a particular return value_ in the `get_alerts` function, there won't be anything to save.. Update that function to return the alert(s) to the calling script – Theo Oct 16 '19 at 10:05
  • 1
    if you are storing the `foreach` output into `$output`, why are you then piping that into `$output` after the `foreach`? You should remove the `|` from the second line or there is something else missing from the code in the post. – AdminOfThings Oct 16 '19 at 11:11
  • As an aside: In PowerShell, functions are invoked _like shell commands_ - `get_alerts $element $arg2` - _not_ like C# methods - `get_alerts($element, $arg2)`; see [`Get-Help about_Parsing`](https://learn.microsoft.com/en-us/powershell/module/microsoft.powershell.core/about/about_parsing). To prevent accidental use of method syntax, use [`Set-StrictMode -Version 2`](https://learn.microsoft.com/en-us/powershell/module/microsoft.powershell.core/set-strictmode) or higher, but note its other effects. – mklement0 Oct 16 '19 at 13:17
  • I see that you've at least once before accepted an answer, but - as a refresher - let me recapitulate the advice to newcomers in the next comment. I also encourage you to revisit your previous questions and accept answers there, as appropriate. – mklement0 Oct 17 '19 at 01:33

2 Answers2

0

if you return $element your $output variable should contain the requested data:

$output = foreach ($element in $ashost)  {get_alerts($element);$element;}|
$output | Out-File "C:\File.txt"
Guenther Schmitz
  • 1,955
  • 1
  • 9
  • 23
0

You state that your get_alerts function uses Write-Host to print to the screen.

Write-Host's output by design isn't sent to PowerShell's success output stream[1], so nothing is captured in $output, so nothing is sent to Out-File, which is why you end up with an empty file.

  • If you can modify the get_alerts function, replace Write-Host calls with Write-Output calls, or just use implicit output (e.g., instead of Write-Host $foo, use Write-Output $foo or simply $foo).

  • If you cannot, and if you're using PowerShell v5 or higher, you can use redirection 6>&1 to redirect Write-Host output to the success output stream, which allows you to capture it:

& { foreach ($elem in $ashost) { get_alerts $elem } } 6>&1 | Out-File C:\File.txt

Or, using a single pipeline:

$ashost | ForEach-Object { get_alerts $_ } 6>&1 | Out-File C:\File.txt

See this answer for more information.


[1] Write-Host is typically the wrong tool to use, unless the intent is to write to the display only, bypassing the success output stream and with it the ability to send output to other commands, capture it in a variable, redirect it to a file. In PSv5+ Write-Host writes to the information stream, whose output can be captured, but only via 6>. See also: the last section of https://stackoverflow.com/a/50416448/45375

mklement0
  • 382,024
  • 64
  • 607
  • 775