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
84 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_
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
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 70

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

Are You Headed to Black Hat USA 2017?

Getting ready for Black Hat next week? Kick things off with the WatchGuard Badge Challenge and test your puzzle and cipher skills. Do you have what it takes to earn our limited edition Firebox Badge? Get started today - https://crimsonthorn.net

Question has a verified solution.

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

Synchronize a new Active Directory domain with an existing Office 365 tenant
My attempt to use PowerShell and other great resources found online to simplify the deployment of Office 365 ProPlus client components to any workstation that needs it, regardless of existing Office components that may be needing attention.
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 antispam), the admini…
Monitoring a network: why having a policy is the best policy? Michael Kulchisky, MCSE, MCSA, MCP, VTSP, VSP, CCSP outlines the enormous benefits of having a policy-based approach when monitoring medium and large networks. Software utilized in this v…

636 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