3

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?

BigRedEO
  • 807
  • 4
  • 13
  • 33
  • 2
    Which version of powershell? 3 or higher can just do: Get-Content file.txt -Tail 100 – LinkBerest Apr 29 '15 at 14:27
  • 2.0 (unfortunately). But even then, I'm not sure if that would drop me back to the C:\> prompt. – BigRedEO Apr 29 '15 at 14:33
  • 1
    Don't use `cmd.exe` and run `powershell -command ...`. Run PowerShell instead of `cmd.exe`, and use the `Get-Content` cmdlet directly in PowerShell. – Bill_Stewart Apr 29 '15 at 16:13
  • @Bill_Stewart - Not able to do so due to tight restrictions - batch files only. But I found the answer and posted below. – BigRedEO Apr 29 '15 at 16:18
  • 1
    But you're running PowerShell anyway. The different is that if you run PowerShell, you don't need a batch file at all. Just run `Get-Content` directly from PowerShell. There is no need for `cmd.exe` at all. – Bill_Stewart Apr 29 '15 at 20:19
  • No - I have a batch script already running on these systems that runs every night overnight and does many different things - I just need to add one command that would "shrink" a file, but since converting all these systems to Windows 7, "tail" no longer works. But this does. – BigRedEO Apr 30 '15 at 12:11

2 Answers2

2

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

Community
  • 1
  • 1
BigRedEO
  • 807
  • 4
  • 13
  • 33
0

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)

LinkBerest
  • 1,281
  • 3
  • 22
  • 30
  • Thanks - I tried several different ways with those, but none would return me to my command prompt, so I'm guessing it's the OpenSSH. – BigRedEO Apr 29 '15 at 14:55
  • @BigRedEO You might check out [the .NET tie-in for SSH](http://www.powershelladmin.com/wiki/SSH_from_PowerShell_using_the_SSH.NET_library) it sometimes corrects SSH issues with Powershell 2.0 when running simple commands or just multiple single commands and includes a work-around for 2.0 SCP commands. – LinkBerest Apr 29 '15 at 15:03