I am trying to write some larger scripts that log every step along the way. What I am planning on doing is just tagging this function onto the end of a previous function. So, for example, if I have a function called Create-Directory that has some Write-Host commands, I want to capture these lines to an output file. Right now I am trying to create this section function like this, but, it is not working:
function Log-Process
{
param(
[parameter(Mandatory=$true)]
[string]
$logPath
)
$_ | Out-File -FilePath $filePath -Encoding ASCII -Append
}
I was trying to do this by using logic like:
Create-Folder -folderPath C: -folderName test | Out-Process
Can someone suggest a better approach? I can write the whole thing out each time
Create-Folder -folderPath C: -folderName test | Write-File -Path C:\test\log.txt -Encoding ASCII -Append
But, being able to shorten it up, perhaps even to an an alias (like lp) would be great. Please feel free to make suggestions.
might be you can write the output to the log file directly, like
Create-Folder -folderPath c: -folderName test >> $logfile
# $logfile contains the file path of the log file
Does this approach solve your problem?
As I am working on it more I realized using Write-Host, as I am, pushes the output of my functions to the current host. I need to find a way to pass the host output to the file. Sometimes it works, sometimes it doesn't and I'm not sure what determines which. I'll try outputting directly to the file, but, I believe I tried this and it didn't work.
Can you explain more on "it didn't work"? the output is not written to the log file as expected? if so, can you share the code where the output failed at?
I am trying to build some functions that create/validate folders. Here is the code:
function Create-Folder{ param( [parameter(Mandatory=$true)] [string] $folderPath, [parameter(Mandatory=$true)] [string] $folderName ) # Set folder context cd $folderPath; # Combine folderPath and folderName. $folder = "$folderPath\$folderName"; # Check to see if folder already exists if([System.IO.Directory]::Exists($folder)) { # Notify host folder already exists. Write-Output "$(Time-Stamp): $folder already exists."; } else { # Attempt to create folder. Try { # Notify host of folder creation Write-Output "$(Time-Stamp): Creating folder: $folder."; # Create folder $folderStatus = New-Item -Path $folder -ItemType Directory; } # Catch exceptions Catch [System.Exception] { # Notify host of exception Write-Output "$(Time-Stamp): Error creating folder $folder."; } Finally { # Validate folder was created Write-Output "$(Time-Stamp): Validating folder: $folder."; if([System.IO.Directory]::Exists($folder)) { Write-Output "$(Time-Stamp): $folder was created."; } else { Write-Output "$(Time-Stamp): $folder was not created."; } } } } function Time-Stamp{ ################################################# # # NOTE: This function returns a DateTime for Now as a string. # # USAGE: Time-Stamp # ################################################# return [System.DateTime]::Now.ToString("yyyy.MM.dd hh:mm:ss"); }
When I call the Create-Folder function it writes to the host instead of to the file specified in the | out-file call. It would look like this:
Create-Folder C:\folderpath foldername | Out-file -path C:\log.txt -Encoding ASCII -Append
I have this updated script: function Log-Script { param( [Parameter(ValueFromPipeline=$true,Mandatory=$false)] [string] $logFilePath ) if(!$logFilePath) { $_ | %{Out-Host -InputObject $_}; } else { $_ | %{Out-Host -InputObject $_; Out-File -InputObject $_ -FilePath $logFilePath -Encoding ASCII -Append} } } When I run the command below without the function code wrapped around it works fine. dir | %{Out-Host -InputObject $_; Out-File -InputObject $_ -FilePath $logFilePath -Encoding ASCII -Append} When I parameterize it and wrap it in the function, I get either no output (without the switch) or errors (with the switch): Log-Script : The input object cannot be bound to any parameters for the command either because the command does not take pip eline input or the input and its properties do not match any of the parameters that take pipeline input. At line:1 char:16 + dir |Log-Script <<<< + CategoryInfo : InvalidArgument: (windowspowershell:PSObject) [Log-Script], ParameterBindingException + FullyQualifiedErrorId : InputObjectNotBound,Log-Script
I have this updated script:
function Log-Script { param( [Parameter(ValueFromPipeline=$true,Mandatory=$false)] [string] $logFilePath ) if(!$logFilePath) { $_ | %{Out-Host -InputObject $_}; } else { $_ | %{Out-Host -InputObject $_; Out-File -InputObject $_ -FilePath $logFilePath -Encoding ASCII -Append} } }
When I run the command below without the function code wrapped around it works fine.
dir | %{Out-Host -InputObject $_; Out-File -InputObject $_ -FilePath $logFilePath -Encoding ASCII -Append}
Log-Script : The input object cannot be bound to any parameters for the command either because the command does not take pip eline input or the input and its properties do not match any of the parameters that take pipeline input. At line:1 char:16 + dir |Log-Script <<<< + CategoryInfo : InvalidArgument: (windowspowershell:PSObject) [Log-Script], ParameterBindingException + FullyQualifiedErrorId : InputObjectNotBound,Log-Script
Nevermind, I found a script that did what I needed:
http://poshcode.org/2813
Can you please post the solution to this? That link is dead.
This worked for me.
Function OutFile{ Param([Parameter(ValueFromPipeline=$True)] $message) $message | Out-File -FilePath $script:LogPath -Append}