BigRedEO
BigRedEO

Reputation: 847

Powershell equivalent of the "tail" command ALMOST works remotely

I am looking for a Windows 7 equivalent of the "tail" command and thought I had found it with this Powershell equivalent -

    C:\>powershell -command "& {Get-Content file.txt | Select-Object -last 100}"

If I use this in the CMD prompt on my own Windows 7 PC, returns the info just fine. I can even input/append it into another file.

However, when I log on remotely to another PC (via openSSH), the command works, but it never drops me back to a command prompt - just hangs after showing me the last 100 lines of the file. Which means this won't work for a batch file I'm trying to edit for about 300 remote Windows 7 PCs.

Any ideas?

Upvotes: 4

Views: 9073

Answers (2)

BigRedEO
BigRedEO

Reputation: 847

After trying MANY different suggestions found all over online, FINALLY found one that worked!

And the answer is within the Batch file itself. My batch file to call this Powershell line was just this:

    Powershell.exe -noprofile -executionpolicy Bypass C:\log\Tail.ps1
    :end

Again, works great if you're using it on the very PC from which you want it to run/get the information. But not remotely. Finally found you just need to add "< nul" to the end of your call to Powershell in your batch file, just like this

    Powershell.exe -noprofile -executionpolicy Bypass C:\log\Tail.ps1 <nul
    :end

What the other person wrote is what finally made sense: "My research has shown that PowerShell runs the commands in the script indicated through the -File switch and then waits for additional PowerShell commands from the standard input (my brief experimentation with the -Command switch demonstrated similar behavior). By redirecting the standard input to nul, once PowerShell finishes executing the script and 'reads end-of-file' from the standard input, PowerShell exits."

Found here at this page - Powershell script gets stuck, doesn't exit when called from batch file so credit actually goes to @Gordon Smith

Upvotes: 2

Since your running the command with -command "...", according to the docs, you need to specify the -noexit flag to stop powershell from exiting after the command is run.

powershell -command "& {Get-Content file.txt | Select-Object -last 100}" -noexit

When you add this to a batch file you'll probably need -noprofile and -noninteractive as well - though for remote commands you might want to spawn a process for better control and error handling. Also, if this doesn't work the problem would probably be with how OpenSSH is handling something (this worked for me on a test-server with remote connect)

Upvotes: 0

Related Questions