Solved

In the following function in powershell how can I write to console and write to the file $filename ?

Posted on 2015-01-16
3
74 Views
Last Modified: 2015-01-19
Hello,

in the following function, how to use the redirection in order to write to the file - the file is created - $filename ?

my files are always empty.

here's my function

function Get_Tshr_WhatTestingMachineHasTester( [string[]]$arrayOfMachinesNames= $(Get_tshr_MachinesNames_OutOfDataFile_InArray) ) 
{

    $filename = "testersOnMachines" + $(get-date -Format yyyy-MM-dd_HH_mm_ss) 
    
    $toto = New-Item -Path c:\temp\testersIE -name $filename -ItemType file
    
	foreach($aHostName in $arrayOfMachinesNames) 
    { 
        
         $sw = [Diagnostics.Stopwatch]::StartNew()

         if(Test-Connection $aHostName -Count 1 -Quiet) {
            Get_Tshr_InfosOnTesterMachineUser($aHostName)
         }
         else{
            Write-Host "host :" $aHostName " n'est pas atteignable"
         }
        
         $sw.Stop()
         Write-Host "time-elapsed : " -NoNewline >> $toto
         Write-Host $sw.Elapsed  -ForegroundColor Cyan

	}
}

Open in new window



thanks in advance.
toshi
0
Comment
Question by:toshi_
3 Comments
 
LVL 9

Expert Comment

by:Christopher Jay Wolff
ID: 40553716
I believe you have to use the Out-File commandlet as shown in the help screen snipet below for a output to a file.  Have to go so could look at your code example specifically later, but maybe this gets you going.

PS Help Screen for Out-File.
0
 
LVL 68

Accepted Solution

by:
Qlemo earned 500 total points
ID: 40553727
Write-Host is a console-only cmdlet, not allowing redirection. I recommend to replace
         Write-Host "time-elapsed : " -NoNewline >> $toto
         Write-Host $sw.Elapsed  -ForegroundColor Cyan

Open in new window

with
         Out-File -InputObject "time-elapsed : $($sw.Elapsed)" -Append $toto

Open in new window

If you want/need to, use a similar command for the "not available" message en france.
0
 

Author Comment

by:toshi_
ID: 40557585
Thanks a lot ,
your solution does write to the file.

toshi.
0

Featured Post

How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

Join & Write a Comment

Hi all.   The other day I had to change the passwords for a bunch of users on the fly. Because they were so many, I decided to do it in an automated way and I would like to share it with you all.   If you are not doing it directly in a Domain Co…
"Migrate" an SMTP relay receive connector to a new server using info from an old server.
Internet Business Fax to Email Made Easy - With eFax Corporate (http://www.enterprise.efax.com), you'll receive a dedicated online fax number, which is used the same way as a typical analog fax number. You'll receive secure faxes in your email, fr…
In this seventh video of the Xpdf series, we discuss and demonstrate the PDFfonts utility, which lists all the fonts used in a PDF file. It does this via a command line interface, making it suitable for use in programs, scripts, batch files — any pl…

758 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

22 Experts available now in Live!

Get 1:1 Help Now