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
76 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 69

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

Courses: Start Training Online With Pros, Today

Brush up on the basics or master the advanced techniques required to earn essential industry certifications, with Courses. Enroll in a course and start learning today. Training topics range from Android App Dev to the Xen Virtualization Platform.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

"Migrate" an SMTP relay receive connector to a new server using info from an old server.
The following article is intended as a guide to using PowerShell as a more versatile and reliable form of application detection in SCCM.
This tutorial gives a high-level tour of the interface of Marketo (a marketing automation tool to help businesses track and engage prospective customers and drive them to purchase). You will see the main areas including Marketing Activities, Design …
Two types of users will appreciate AOMEI Backupper Pro: 1 - Those with PCIe drives (and haven't found cloning software that works on them). 2 - Those who want a fast clone of their boot drive (no re-boots needed) and it can clone your drive wh…

776 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