Go Premium for a chance to win a PS4. Enter to Win

x
?
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
Medium Priority
?
90 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 71

Accepted Solution

by:
Qlemo earned 2000 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

 The Evil-ution of Network Security Threats

What are the hacks that forever changed the security industry? To answer that question, we created an exciting new eBook that takes you on a trip through hacking history. It explores the top hacks from the 80s to 2010s, why they mattered, and how the security industry responded.

Question has a verified solution.

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

In previous parts of this Nano Server deployment series, we learned how to create, deploy and configure Nano Server as a Hyper-V host. In this part, we will look for a clustering option. We will create a Hyper-V cluster of 3 Nano Server host nodes w…
Transferring FSMO roles is done when an admin wants to split roles between certain Domain Controllers or the Domain Controller holding the Roles has been forcefully demoted using dcpromo / forceremoval
Exchange organizations may use the Journaling Agent of the Transport Service to archive messages going through Exchange. However, if the Transport Service is integrated with some email content management application (such as an anti-spam), the admin…
Loops Section Overview

824 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