Powershell equivalent to Perl Open() Command

This topic contains 3 replies, has 2 voices, and was last updated by  Max Kozlov 7 months, 1 week ago.

  • Author
    Posts
  • #66196

    Dennis Garbin
    Participant

    Coming from Perl environment, im trying to explore powershell and looking for a command similar to Perl Open() command,

    please help

  • #66198

    Max Kozlov
    Participant

    there is no direct equivalent but open() often used in cycles like

    open($fh,'file.txt')
    while ( {$fh} ) {
     $_
    }
    

    and can be easily changed to pipelined equivalent

    Get-Content 'file.txt' | Foreach-Object {
     $_
    }
    

    /I'm change angle to curly brackets/

  • #66219

    Dennis Garbin
    Participant

    i am streaming data from a linux server with something like below

    ########
    open(OUT, "plink.exe -ssh -2 -t -l $user -pw $pass $host $cmd|");

    while () {
    some parsing done here
    }

    #####

    if i do something like the above suggestion
    #### test.ps1 ###
    & \tools\plink.exe $arg1 $arg2 $arg3 $arg4 $user $arg5 $pass $hostaddress $cmd | out-file .\tools\tmp.txt

    #### test2.ps1 ######
    get-content .\tools\tmp.txt -wait -tail 1 | foreach-object
    {
    write-host $_
    }

    .\tools\tmp.txt file size increases overtime and the ForEach-object seems to fail

  • #66267

    Max Kozlov
    Participant

    can't construct live example but may be something like

    plink $arg1 $arg2 $arg3 $arg4 $user $arg5 $pass $hostaddress $cmd | foreach-object {
    Write-Host $_
    }
    

    can work ?

    at least this test work

     D:\> cat d:\cmd.ps1
    1..10 | %{
       $_
       start-sleep -sec 1
    }
     D:\> powershell -nologo -noprofile d:\cmd.ps1 | %{ write-host "Data; $_" }
    Data; 1
    Data; 2
    Data; 3
    Data; 4
    Data; 5
    Data; 6
    Data; 7
    Data; 8
    Data; 9
    Data; 10
    

You must be logged in to reply to this topic.