The Author Online Book Forums are Moving

The Author Online Book Forums will soon redirect to Manning's liveBook and liveVideo. All book forum content will migrate to liveBook's discussion forum and all video forum content will migrate to liveVideo. Log in to liveBook or liveVideo with your Manning credentials to join the discussion!

Thank you for your engagement in the AoF over the years! We look forward to offering you a more enhanced forum experience.

kryten (2) [Avatar] Offline
#1
Hi All,
First of all please allow me to congratulate and thank, most sincerly, Bruce Payette and the team responsible for PowerShell. For the first time since my Commodore 64 days I feel as though _I'm_ the one driving what my 'puter is doing.

I speak as one who tried, really tried, to get VBScript to do what I wanted it to do and failed miserably. With PowerShell I now have the luxury of being able to just type it in and see if it works before committing anything to script and onward distribution.

I'm finding the language and syntax to be quite natural and although I'm really just a novice with Windows programming, I'm thoroughly enjoying it. As I am enjoying working my way through the PowerShell In Action book. Which I cannot recommend highly enough to anyone who can relate to my feelings of cluelessness.

Now if I may ask a couple of questions?

(a) I wrote this script:-

$loop = Read-Host "Please enter the loop number:"
$shelf = Read-Host "Please enter the shelf:"
$card = Read-Host "please enter the card:"
[int]$unitstart = Read-Host "Please enter the start unit:"
[int]$unitend = Read-Host "Please enter the final unit:"
#
# Start the loop to print the 1st value of unit then increase
# until value of unitend is reached
#
while ($unitstart -le $unitend)
{
[string]$tn = $loop + " " + $shelf + " " + $card + " " + $unitstart
write-output $tn
$unitstart++
}

This writes to the shell window a series of numbers. Eg if you enter 148 for Loop, 0 for Shelf, 4 for Card and 0 for startunit then 31 for endunit; you will see what I mean.

How can I get this to write to a text file instead (or as well as) the shell window. So far the best I was able to do was get only the first line of output written into the text file! - Like I say I AM a beginner!

(b) Is there any way, in Powershell, to control what would be sent to a remote terminal session, being controlled via a 3rd party app ( Procomm Plus ) via TELNET? Alternatively, the same question, but when the connection to the remote terminal is via a local COM port. I would need to be able to read incoming ASCII strings as well as sending them. Just wondering if it is possible, if so a push in the right direction would be great. Thanks.

Regards,
Kryten
jhammer (5) [Avatar] Offline
#2
Re: Newbie Comments and Advice.
Hi Kryten,

To answer your first question, you can redirect the output of your script to a file in the following manner:

PS> ./yourscript.ps1 > out.txt

This will still prompt you for the five inputs, but the output will be written to out.txt.
If you then want to see this in the console prompt, you can then use the type command:

PS> type out.txt

That will print the file in the console window.

You may want to look at section 2.4.2 for some info on output methods (in case you wanted to do anything more fancy than what I have above).

I'm sorry but I can't offer any help on your second question (I'm just starting myself).
Good Luck!

- Joey