Google
 

Friday, May 24, 2013

Output to multiple destinations in PowerShell

Sometimes you need to output a result of a Cmdlet execution or a variable to screen and a file, for example if you want to log all operations in a script in addition to showing the output to the user.
Calling both Out-Host and Out-File for each operation is clearly not a good option.
The nice Tee-Object Cmdlet can perform this functionality, but till before version 3.0, it cannot append output to an existing file. So be it, I have to code it:


function Out-All([string]$FilePath, [switch]$Append)
{
    Begin
    {
        if($Append -eq $False)
        {
            New-Item -Path $FilePath -ItemType File -Force
        }
    }
    Process
    {
        $_ | Out-Host
        $_ | Out-File -FilePath $FilePath -Append
    }
}



The above function writes the pipline input to both a file using Out-File and screen (or whatever taking the output) using Out-Host. The advantage is that it has the Append  switch.
The Begin block which executes before processing the pipline data checks  the Append switch and creates a new file (or not) accordingly.
The Process block is responsible for the actual writing of data.

Sample use:

(1..100) | Out-All -FilePath "C:\log\data.log"

(1..100) | Out-All -FilePath "C:\log\data.log" -Append

dir | Out-All -FilePath "C:\log\data.log" -Append

Thursday, January 3, 2013

Articles I read in 2012


So it's a new year!! 2012 was a year full of excitement for software lovers. The cloud service providers are competing for the good of the developers and businesses. Mobile technologies continue to emerge and steadily taking more market share from PCs. Also Microsoft released a series of new products.
In this list of articles I read in 2012, you might notice that not all articles are purely technical as I'm trying to find a way in the startup world.

2011's list can be found here
2010's list can be found here
2009's list can be found here
2008's list can be found here 
2007's list can be found here