Link to home
Start Free TrialLog in
Avatar of Erwin Pombett
Erwin PombettFlag for Switzerland

asked on

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

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
Avatar of Christopher Jay Wolff
Christopher Jay Wolff
Flag of United States of America image

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.

User generated image
ASKER CERTIFIED SOLUTION
Avatar of Qlemo
Qlemo
Flag of Germany image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of Erwin Pombett

ASKER

Thanks a lot ,
your solution does write to the file.

toshi.