Getting PowerShell to post all information to out file - powershell

PowerShell is writing information to file when I delete a registry entry successfully however I also need it to post errors to the file instead of the PS session.
Remove-Item -Path $path -Force -Verbose 4>&1 | Out-File "c:\registryresults.txt"
How would I be best able to achieve this?

4>&1 is the 'Verbose' stream being redirected to the 'output' stream.
you probably want to use *>&1 or just *> "c:\registryresults.txt" on its own without the Out-File

Related

Why doesn't -whatif parameter get passed to pipeline

Utilizing the common parameter -Whatif within a Powershell script e.g.:
Remove-Item -Path "$now\$basename" -Recurse -Whatif -Force |
Write-Entry -Line $(get-linenumber) -level 'Info' |
Create-Log -title "$title.log" -display
The log file is blank even though I can clearly see the text onscreen. I would like to pass the -Whatif string to a text file. I know this has something to do with redirection but that's as far as I have gotten.
The output of the -Whatif processing bypasses the standard streams for output and goes to the console directly. This TechNet question about accessing the -WhatIf output from C# explains.

Is it possible to "-passthru" to Log file only and not the console when using "Start-Transcript -Path "$LogOutput"

I have a Powershell script where I want the console to output my custom messages only, but I would like to capture what the command is doing in a log only. Is this possible?
Just to make it clearer with an example below. I want to copy all the Sub Directories and Files as they are in their own directory to a new location:
Start-Transcript -Path "$LogOutput" -IncludeInvocationHeader -Append
Get-ChildItem -Path "$Source" | Copy-Item -Destination "$Destination" -Exclude "File*.xml" -Force -Recurse -PassThru
Stop-Transcript
I then use $? to get the success of the result on the console for my custom messages.
The above shows details I find helpful but it outputs the -PassThru on the console which I do not want to display there. If I don't specify it, it outputs nothing in the Transcript log either.
If I try appending to the copy command ...-PassThru | Out-File -FilePath "$LogOutput" -Append (The same Log used for Transcript) it fails as the log file is locked by the Transcript. Is there a way to make this possible, or will I have to append ...-PassThru | Out-File -FilePath "$LogOutput" -Append to all my commands individually?
Many thanks in advance.

How can I capture what is happening when a that are being removed during a

Good afternoon SO!
I am still new to powershell and I'm looking for a way to record what happens after
I invoke Command as for example on Line 51.
Can I print the device logging into that directory?
On Line 52 after DEL *.* happens is it possible to print what is being deleted to file?
i.e.
51 Invoke-Command -device $ipaddress -command 'cd \firmware'| Out-File $LogFileName -Append;
52 Invoke-Command -device $ipaddress -command 'DEL *.*'| Out-File $LogFileName -Append;
Stop Transcript
}`
I've tried using Start-Transcript -path $LogFileName -append // Stop Transcript but this doesnt print what is happen when every command is invoked.
To answer your 2nd question:
On Line 52 after DEL . happens is it possible to print what is being
deleted to file?
Remove-Item (aka DEL) has a switch -Verbose that will output the list of files deleted. The rub is that it outputs the list to the "verbose" stream. Powershell can generate output on one of many streams in addition to the traditional output stream and error stream. One of those is the verbose stream. Unfortunately, only the output stream passed down the pipeline, the other streams just get output to the console. Fortunately, you can re-direct these other streams to the out stream by using the redirection operator >&. On the left side of the operator, you include the # of the stream you want to redirect from, and on the right side, what you want to redirect to. For your example, you want to redirect from the verbose stream (4) to the output stream (1):
4>&1
So the command would look like this:
Invoke-command -ScriptBlock {del *.* -Verbose 4>&1} | Out-File $LogFileName -Append

Powershell Move-Item logging

I would like some assistance getting a powershell script to work correctly. A piece of it is pasted below. Basically what I'm trying to do is to move all files in various subdirectories to another location. I can get the files to move and the folders to create fine, I'm just having a problem with getting the results to a text file. I've tried using out-file, but the txt file is empty. I've also tried start-transcript, but notepad doesn't seem to see line breaks and all the text bleeds together. Is there a better way to go about this? I'm using this basic command for testing.
Start-Transcript c:\log.txt
Move-Item c:\temp\*.* C:\Temp\2014 -Verbose -Force
Stop-Transcript
You may just need to collapse the output file descriptors. According to the about_Redirection page, it should work if you do the following:
Move-Item c:\temp\*.* C:\Temp\2014 -Verbose -Force *>&1 |
Out-File -FilePath $MyLogPath
Or if you want to see the output at the same time
Move-Item c:\temp\*.* C:\Temp\2014 -Verbose -Force *>&1 |
Tee-File -FilePath $MyLogPath

How to redirect the output of a PowerShell to a file during its execution

I have a PowerShell script for which I would like to redirect the output to a file. The problem is that I cannot change the way this script is called. So I cannot do:
.\MyScript.ps1 > output.txt
How do I redirect the output of a PowerShell script during its execution?
Maybe Start-Transcript would work for you. First stop it if it's already running, then start it, and stop it when done.
$ErrorActionPreference="SilentlyContinue"
Stop-Transcript | out-null
$ErrorActionPreference = "Continue"
Start-Transcript -path C:\output.txt -append
# Do some stuff
Stop-Transcript
You can also have this running while working on stuff and have it saving your command line sessions for later reference.
If you want to completely suppress the error when attempting to stop a transcript that is not transcribing, you could do this:
$ErrorActionPreference="SilentlyContinue"
Stop-Transcript | out-null
$ErrorActionPreference = "Continue" # or "Stop"
Microsoft has announced on Powershell's Connections web site (2012-02-15 at 4:40 PM) that in version 3.0 they have extended the redirection as a solution to this problem.
In PowerShell 3.0, we've extended output redirection to include the following streams:
Pipeline (1)
Error (2)
Warning (3)
Verbose (4)
Debug (5)
All (*)
We still use the same operators
> Redirect to a file and replace contents
>> Redirect to a file and append to existing content
>&1 Merge with pipeline output
See the "about_Redirection" help article for details and examples.
help about_Redirection
Use:
Write "Stuff to write" | Out-File Outputfile.txt -Append
I take it you can modify MyScript.ps1. Then try to change it like so:
$(
Here is your current script
) *>&1 > output.txt
I just tried this with PowerShell 3. You can use all the redirect options as in Nathan Hartley's answer.
powershell ".\MyScript.ps1" > test.log
If you want a straight redirect of all output to a file, try using *>>:
# You'll receive standard output for the first command, and an error from the second command.
mkdir c:\temp -force *>> c:\my.log ;
mkdir c:\temp *>> c:\my.log ;
Since this is a straight redirect to file, it won't output to the console (often helpful). If you desire the console output, combined all output with *&>1, and then pipe with Tee-Object:
mkdir c:\temp -force *>&1 | Tee-Object -Append -FilePath c:\my.log ;
mkdir c:\temp *>&1 | Tee-Object -Append -FilePath c:\my.log ;
# Shorter aliased version
mkdir c:\temp *>&1 | tee -Append c:\my.log ;
I believe these techniques are supported in PowerShell 3.0 or later; I'm testing on PowerShell 5.0.
One possible solution, if your situation allows it:
Rename MyScript.ps1 to TheRealMyScript.ps1
Create a new MyScript.ps1 that looks like:
.\TheRealMyScript.ps1 > output.txt
You might want to take a look at the cmdlet Tee-Object. You can pipe output to Tee and it will write to the pipeline and also to a file
If you want to do it from the command line and not built into the script itself, use:
.\myscript.ps1 | Out-File c:\output.csv
To embed this in your script, you can do it like this:
Write-Output $server.name | Out-File '(Your Path)\Servers.txt' -Append
That should do the trick.