13

I have wrote the following script to read the CSV file to perform the custom format of output.

Script is below:

$Content = Import-Csv Alert.csv
foreach ($Data in $Content) {
    $First = $Data.DisplayName
    $Second = $Data.ComputerName
    $Third = $Data.Description
    $Four = $Data.Name
    $Five = $Data.ModifiedBy
    $Six = $Data.State
    $Seven = $Data.Sev
    $Eight = $Data.Id
    $Nine = $Data.Time

    Write-Host "START;"
    Write-Host "my_object="`'$First`'`;
    Write-Host "my_host="`'$Second`'`;
    Write-Host "my_long_msg="`'$Third`'`;
    Write-Host "my_tool_id="`'$Four`'`;
    Write-Host "my_owner="`'$Five`'`;
    Write-Host "my_parameter="`'$Four`'`;
    Write-Host "my_parameter_value="`'$Six`'`;
    Write-Host "my_tool_sev="`'$Seven`'`;
    Write-Host "my_tool_key="`'$Eight`'`;
    Write-Host "msg="`'$Four`'`;
    Write-Host "END"
}

The above script executing without any error.

Tried with Out-File and redirection operator in PowerShell to dump the output into a file, but I'm not finding any solution.

4
  • 4
    remove the write-host cmdlet to pipe to out-file! ( or at least use Write-Output) Commented Jun 27, 2014 at 13:46
  • @CB is right. Write-Host writes directly to the screen and not to the output stream so anything written with write-host cannot be redirected to a file. Commented Jun 27, 2014 at 15:02
  • @CB, I followed your suggestion, but still facing some issue. write-output prints extra line, which I don't want in that file. out-file option giving following error, just a sample one Unexpected token ''$First';' in expression or statement. At C:\Users\Demo\Desktop\2parse.ps1:20 char:14 + "mc_output= "'$Second'; |out-file -filepath "C:\Users\Demo\Desktop\dump.txt ... Commented Jun 27, 2014 at 15:17
  • When you say that you don't want extra lines, what extra lines are you referring to? Do you want all the text on the same line? If so, if sounds as though you are trying to create something similar to a csv file and could potentially look at the Export-Csv cmdlet. Commented Jun 28, 2014 at 9:36

1 Answer 1

32

Write-Host writes to the console. That output cannot be redirected unless you run the code in another process. Either remove Write-Host entirely or replace it with Write-Output, so that the messages are written to the Success output stream.

Using a foreach loop also requires additional measures, because that loop type doesn't support pipelining. Either run it in a subexpression:

(foreach ($Data in $Content) { ... }) | Out-File ...

or assign its output to a variable:

$output = foreach ($Data in $Content) { ... }
$output | Out-File ...

Another option would be replacing the foreach loop with a ForEach-Object loop, which supports pipelining:

$Content | ForEach-Object {
  $First = $_.DisplayName
  $Second = $_.ComputerName
  ...
} | Out-File ...

Don't use Out-File inside the loop, because repeatedly opening the file will perform poorly.

Sign up to request clarification or add additional context in comments.

3 Comments

I would also suggest removing all backticks and moving the double quote to the end of the line. For example: "my_object='$First';"
@TheMadTechnician A here string would be an even better option.
@AnsgarWiechers i took your ForEach-Object with pipeline option. it really works well :)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.