unable to store output in a variable also not able to export result to any file using powershell - powershell

I have written following commands:
$env:Path += "C:\Program Files (x86)\Java\jre6\bin"
& "C:\Program Files (x86)\Java\jre6\bin\java" -version
I am trying to store output of second line in a variable but unable to do so. Also tried to export output in some file, that also did not work. Please assist.

java.exe might be writing the version output to stderr.
You can merge the error stream (2) into the standard output stream (1) with a stream redirection operator (>&) like so:
& "C:\program files(x86)\java\jre6\bin\java" -version 2>&1
PowerShell will try to be helpful and wrap the error output in an ErrorRecord. Since we just need the plaintext output, grab the Message value from the Exception wrapped by it:
$jversion = (& "C:\program files(x86)\java\jre6\bin\java" -version 2>&1).Exception.Message
$jversion will now contain one or more strings as output by java.exe
See the about_Redirection help file for more information

Related

What does "supply values for the following parameters mean in command line?"

So inside of my terminal, I created a text file inside a directory (cat > fnames.txt). My initial goal was to write some data into said file. After creating fnames.txt, the following information showed up after trying to append data to the file using (cat >> fnames.txt):
cmdlet Get-Content at command pipeline position 1
Supply values for the following parameters:
Path[0]:
Image of terminal
Does anyone know the reason of this and what it means?
The command cat is in Powershell an alias to Get-Content. The cmdlet reads information from somewhere. The error message means that Get-Content does not have an idea from where you want it to get data, so it asks you.

How to read a text file to a variable in batch and pass it as a parameter to a powershell script

I have a powershell script that generates a report, and I have connected it to an io.filesystemwatcher. I am trying to improve the error handling capability. I already have the report generation function (which only takes in a filepath) within a try-catch loop that basically kills word, excel and powerpoint and tries again if it fails. This seems to work well but I want to embed in that another try-catch loop that will restart the computer and generate the report after reboot if it fails a second consecutive time.
I decided to try and modify the registry after reading this article: https://cmatskas.com/configure-a-runonce-task-on-windows/
my plan would be, within the second try-catch loop I will create a textfile called RecoveredPath.txt with the file path being its only contents, and then add something like:
Set-ItemProperty "HKLMU:\Software\Microsoft\Windows\CurrentVersion\RunOnce" -Name '!RecoverReport' -Value "C:\...EmergencyRecovery.bat"
Before rebooting. Within the batch file I have:
set /p RecoveredDir=<RecoveredPath.txt
powershell.exe -File C:\...Report.ps1 %RecoveredDir%
When I try to run the batch script, it doesn't yield any errors but doesn't seem to do anything. I tried adding in an echo statement and it is storing the value of the text file as a variable but doesn't seem to be passing it to powershell correctly. I also tried adding -Path %RecoveredDir% but that yielded an error (the param in report.ps1 is named $Path).
What am I doing incorrectly?
One potential problem is that not enclosing %RecoveredDir% in "..." would break with paths containing spaces and other special chars.
However, the bigger problem is that using mere file name RecoveredPath.txt means that the file is looked for in whatever the current directory happens to be.
In a comment your state that both the batch file and input file RecoveredPath.txt are located in your desktop folder.
However, it is not the batch file's location that matters, it's the process' current directory - and that is most likely not your desktop when your batch file auto-runs on startup.
Given that the batch file and the input file are in the same folder and that you can refer to a batch file's full folder path with %~dp0 (which includes a trailing \), modify your batch file to look as follows:
set /p RecoveredDir=<"%~dp0RecoveredPath.txt"
powershell.exe -File C:\...Report.ps1 "%RecoveredDir%"

How to redirect the terminal output of a PowerShell script to a file

I have a script which when it runs, prints out lines to the terminal (with errors). I would like to redirect this output into files.
I read that I should add this line in my PS1 script: ".\myscript.ps1 *> &1 > outfile.log" but it doesn't work because of ampersand character is not allowed.
You don't need space symbol before ampersand.
.\myscript.ps1 *>&1 will redirect all output streams to the stream number 1
.\myscript.ps1 *>outfile.log will redirect all output streams to the file
https://learn.microsoft.com/en-us/powershell/module/microsoft.powershell.core/about/about_redirection?view=powershell-6
If you just want to put your lines into another file from a terminal I would try this:
./myscript.ps1 2>outfile.log
"./" activates the script "2>" takes the stderr stream into (in this case) the outfile.log But I am not sure what you are supposed to do. Maybe if you could post your code or your task here that would be great.
if you want both streams in one outlog, I would do it like that "2>&1"

Start process, wait for it, and throw away all output

I'd like to do this in PowerShell:
myprogram.exe arg1 arg2 >nul 2>nul
How does it work? There are like a million options to start processes in PowerShell, it's impossible to know the right one you need at a time.
nul is a special device (see this answer on SuperUser for instance) that's available in CMD, but not in PowerShell. In PowerShell you can use $null instead. Starting with PowerShell v3 you can use *> to redirect all output streams, but since you want to redirect output from an external program there should only be output on STDOUT and STDERR (Success and Error output stream in PowerShell terms), so >$null 2>$null should be fine.
A notable difference between CMD and PowerShell is that PowerShell doesn't include the current working directory in the PATH (the list of directories that is searched when you call a program/script without a path). If you want to run myprogram.exe from the current directory you need to prepend it with the path to the current directory (./).
You may also want to use the call operator (&). Although it's not required in this particular case I consider using it good practice. If you specify the command as a string (for instance because the path or filename contains spaces, or you want to use a variable instead of a literal) you MUST use the call operator, otherwise the statement would throw an error.
Something like this
& ./myprogram.exe arg1 arg2 >$null 2>$null
or like this
& ./myprogram.exe arg1 arg2 *>$null
should work.

How can I redirect the output of a command (running in a batch loop) to a file?

I have a windows batch file, which iterates over files in a folder and runs a command on each file. Specifically I am running xmllint to validate some files:
for %%i in (c:\temp\*.xml) do (
C:\XMLLINT\xmllint -noout -schema "C:\schemas\schema.xsd" "%%~dpnxi" >> c:\output.txt
)
It currently shows the output on screen. I want to show the output of all these commands placed in an output file. How can I achieve this? By using the append operator (>>) nothing is accomplished except for a blank file being created.
Is it because of xmllint?
If you're trying to redirect error output from the program, it might be writing to stderr. You can try to redirect it with:
for %%i in (c:\temp\*.xml) do (
C:\XMLLINT\xmllint -noout -schema "C:\schemas\schema.xsd" "%%~dpnxi" >> c:\output.txt 2>&1
)
Basically the 2>&1 at the end means redirect anything from stderr (which is 2) to stdout (which is 1). Since stdout is redirected to a file, you should now see the stderr stream in the file. Hope this works for you!
I've never used it, but if its documentation is here, have you tried just removing your "-noout" option, or adding an: "-output c:\output.txt"?