Author Posts

March 13, 2017 at 6:58 am

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

please help

March 13, 2017 at 8:09 am

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/

March 13, 2017 at 12:26 pm

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

March 13, 2017 at 2:05 pm

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