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:
$_ | 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.
Powered by IDERA