Search code examples
powershellpowershell-2.0powershell-3.0

Concatenate/merge/join/add-content not working properly


I have a text file having some content like session IDs mentioned below, this can be n numbers.

session1
Session2
Session3
.
.
Session n

By using above session IDs I need to generate and fire command and extract the output in another file.


    omnidb -session session1 -detail | findstr "Object"
    omnidb -session session2 -detail | findstr "Object"
    omnidb -session sessionn -detail | findstr "Object"

Please help me to write script in PowerShell using text file contenet it generate and fire the command. update I tried with lots of parameter like Join & Concatenate but i did not get desired output. below script i used but won't work.


    $line1= get-content C:\line1.txt $line2= get-content C:\line2.txt 
    $linesession= get-content C:\sesopt0.txt 
    $( $line1," ",$linesession," ",$line2) | Set-Content C:\sesopt1.txt 

By using this parameter, i am getting output like.

omnidb -session 
session1 
session2 
session3 
. 
session n 
-detail | findstr "Object"

Which is not as expected. it should use file to fire single line command and generate the output. Please provide your expert view. thanks in advance update Just now i tried with below script, its generating the output in console but not sending to file, so that i can use that file to run commands and generate the output.

foreach ($server in (Get-Content C:\sesopt1.txt)) 
{ write-host "omnidb -session $server -detail | findstr 'Object'" > C:\sesopt2.txt } 

Please help thanks.


Solution

  • Bingo !!

    i tried self and found the result. thanks everyone.

    $seopt2= Get-Content C:\sesopt1.txt
    $line2= Get-Content C:\line2.txt
    $result=foreach ($server in $seopt2) 
    { Write-Output "omnidb -session $server $line2" } 
    $result | Set-Content C:\sesopt2.bat