Hello Everyone I'm new to any kind of scripting but I'm slowly learning. Here is my issue, I'm trying to echo a line in cmd that I'm assuming is picking it up as another command. I want to
echo $cmdOutput = ($env:COMPUTERNAME -replace "TEMP", "RUSS" | Out-String).Trim(). However, this is the error that it is giving me when trying echo it.
C:\Temp>echo $cmdOutput = ($env:COMPUTERNAME -replace "TEMP", "RUSS" | Out-String).Trim()
'Out-String).Trim' is not recognized as an internal or external command,
operable program or batch file.
I just want it to echo it whole code back because eventually I'm going to have this appended to a .txt file however I need a little help to get over this obstacle. Any and all help would be greatly appreciated
The vertical bar has a special meaning in cmd (pipe character).
You can prevent echo from interpreting this character as command-character by escaping it with a caret.
you have a problem with
(Out-String).Trim()
try this command in powershell
echo $cmdOutput = ($env:COMPUTERNAME -replace "TEMP", "RUSS" | Out- String).TrimStart()
Related
I have text file having content like
8.4.0.154
newline
I have written below command in a batch file to read the first line means 8.4.0.154
set /p ClientSideUnitTestDestinationLocation=<%scriptLocation%\assemblyVersion.txt
Echo %ClientSideUnitTestDestinationLocation%
here it is just printing 8 prefixes with some special symbol as shown in below image
could anyone help me out here to figure it out that why am not able to read complete number in batch file and print it out.
Thanks in advance
I was creating this text file using PowerShell script using below command:
param ([string] $dllPath = $null,[string] $textFile = $null)
$version = [System.Reflection.Assembly]::LoadFrom($dllPath).GetName().Version.ToString()
$version > $textFile
The text file was not getting created with ANSI encoding hence, unable to read using a batch file.
Now, I changed the above code as below and it is working.
param ([string] $dllPath = $null,[string] $textFile = $null)
$version = [System.Reflection.Assembly]::LoadFrom($dllPath).GetName().Version.ToString()
$version | Out-File $textFile -Encoding Ascii
I am able to read the text file content using below command
set /p ClientSideUnitTestDestinationLocation=<%scriptLocation%\assemblyVersion.txt
Echo %ClientSideUnitTestDestinationLocation%
I'm trying to combine all my commands into a .BAT file. This line use to find and replace text in a file, works just fine in CMD, but when i put this in to a .BAT file, it does not. I am using windows 7
powershell -Command "(gc src\template.html) -replace 'xxxxx', '%1' | Out-File src\%1.html"
Error
After extensive googling, I guess the % needs to be an escape, however, when I do that, the filename becomes %1.html and not the variables of %1.hmtl. How do I get the variables in?
I'm using this site for a some time without ever finding the need to ask a question myself as most of the time I find the answer fairly easily
But my next question is kinda hard to search for with keywords, so here I am asking.
I am trying to take an output of an array, and redirect it to a batch file.
For example:
PS C:\Users\Administrator> $Exmpl = "echo one","echo two"
PS C:\Users\Administrator> $Exmpl
echo one
echo two
PS C:\Users\Administrator> $Exmpl > ExmplFile.bat
So far so good. The problem begins when I want to execute this new script
(In this example I'm using the same shell but it works the same in a CMD shell).
PS C:\Users\Administrator> .\ExmplFile.bat
C:\Users\Administrator>■e
'■e' is not recognized as an internal or external command, operable program or batch file.
After a little exploring, I found:
It acts the same even if I create the file with no extentions and later on add the .bat extension.
If I open the file, and change the entire content to something else like "cd .." - the same error occurs. Like the whole file is damaged from its' creation.
The "e" in the string in the error ■e refers to the first letter in the file. For example after I've changed the content to the command "cd .." - the string in the execution error was ■c. Like it detects an unknown character before the first letter, and after the first letter it detects some sort of a line break.
Can you guys please share your knowledge as I assume it's not a hard question for those of you who know how the redirection to a file in powershell works?
Thanks in advance.
Sounds like an encoding problem. Output redirection is probably using multibyte characters, e.g., UTF-16 or what .Net calls "Unicode". Alternately, it's UTF-8 with a byte order mark.
Try:
Set-Content -Path 'ExmplFile.bat' -Value $Exmpl -Encoding Ascii
Or:
$Exmpl | Out-File -FilePath 'ExmplFile.bat' -Encoding ascii
I'm having a hard time to write a simple batch file as powershell script.
Consider this folder structure. Note the directory with the cool [1] in it...
exiftool.exe
Is a command utility to (for example) extract pictures from embedded MP3 tags.
I uploaded its help if you need more info.
oldscript.cmd
exiftool -picture -b input.mp3 > output.jpg
This line is the one to write in powershell. I found the syntax in a forum post from the author
-picture stands for the tag to extract and -b stands for binary mode
input.mp3 is my test mp3 which can contain special characters in its path like [ and ]
> output.jpg defines the name and saves the resulting image in the same folder
newscript.ps1
My best current non-working code is:
$ownpath = Split-Path $MyInvocation.MyCommand.Path
$exe = $ownpath + '\exiftool.exe'
$input = $ownpath + '\input.mp3'
$outimg = $ownpath + '\output.jpg'
& $exe -picture -binary $input| Set-Content -literalPath $outimg -encoding UTF8
I found Set-Content which is able to handle special characters in pathes through "-literalpath". But I'm still not able to convert the batch to a Powershell script because
Set-Content (and Out-File method too) seems work different compared to old batch piping (">"). The resulting image is not viewable regardless which encoding I use. The help file from above says that exiftool is using UTF8 encoding.
Of course I tried other available encodings, but all of them failed to produce a viewable image. I'm stuck at this point. So my initial question still stands partly "How do I convert this batch file to powershell".
So why is it working when using the old batch command?
For example: create a folder "D:folder" and place this MP3 file with a cover image in it.
Download exiftool.exe from above and place it there too.
The old batch command will work and give you a viewable image
D:\folder\exiftool -picture -binary D:\folder\input.mp3 > D:\folder\output.jpg
The new Powershell V2 script with the same syntax will fail. Why?
& D:\folder\exiftool.exe -picture -binary D:\folder\input.mp3 > D:\folder\output.jpg
You can try this, though I've not tested it 'cause I've not an mp3 with embedded images:
$file = & "D:\folder\exiftool.exe" -picture -binary "D:\folder\input.mp3"
[io.file]::WriteAllBytes('D:\folder\input[1].jpg',$file)
Edit:
using this line from a powershell console return a readable image:
cmd.exe /c "D:\folder\exiftool.exe -picture -binary `"D:\folder\input.mp3`" > image.jpg"
You can use special characters in path and in file name as:
$exe = "c:\ps\exiftool.exe"
$mp3 = "c:\ps\a[1]\input.mp3"
$jpg = " c:\ps\a[1]\image[1].jpg"
cmd.exe /c "$exe -picture -binary $mp3 > $jpg"
with spaces inside path:
$exe = "c:\ps\exiftool.exe"
$mp3 = "`"c:\ps\a [1]\input.mp3`""
$jpg = "`"c:\ps\a [1]\image [1].jpg`""
cmd.exe /c "$exe -picture -binary $mp3 > $jpg"
Try this:
& $exe -picture -b $input | Out-File -LiteralPath $output
There is no need to complicate things by using Start-Process. Because you compute the path to the exe and put that result in a string, you only need use the call operator & to invoke the command named by the string that follows it.
Here is a work around. It seems you can't avoid good old cmd.exe completely.
Thanks should go # C.B.
$ownpath = Split-Path $MyInvocation.MyCommand.Path
$exe = $ownpath + '\exiftool.exe'
$input = $ownpath + '\input.mp3'
$output = $ownpath + '\output.jpg'
cmd.exe /c " `"$exe`" -picture -binary `"$input`" > `"$output`" "
Note:
This way all pathes can contain special characters like [ and ] or spaces
That extra space in " `"$exe is important. It won't work without it.
The normal Powershell way with set-content, Out-File (">" is an alias) and [io.file]::WriteAllBytes all don't work with the exiftool.exe utility. For me its a miracle.
I have a shell script that I want to execute this line:
qtvars.bat vsstart "qt.sln" /BUILD "Debug|Win32"
This works fine (though I had to modify qtvars.bat, but that's beside the point). The problem is that I want the command to execute to be in a variable:
EDIT: This doesn't work either, if I type it into bash. Previously I was typing it into cmd.exe, which hardly made for a fair comparison.
command="qtvars.bat"
args="vsstart"
$command $args "qt.sln" /BUILD "Debug|Win32"
Now it chokes on the pipe! I get this message:
'Win32' is not recognized as an internal or external command,
operable program or batch file.
I've tried a bunch of forms of escaping the quotes and/or pipe, all to no avail. Interestingly, it works when it's an executable rather than a batch file, e.g.:
command="devenv.exe"
args=""
$command $args "qt.sln" /BUILD "Debug|Win32"
Thanks for any ideas.
I know you "escape" the pipe character in a batch file with the ^ character, so...
echo ^| Some text here ^|
Would display...
| Some text here |
I don't know whether that would help you in this instance? Maybe try prepending each pipe character with a ^ and see what happens? :-)
This is a classic case of double-escaping, where both bash and CMD.EXE need to be instructed to ignore the special | (pipe) character.
Try the following:
$command $args "qt.sln" /BUILD '"Debug|Win32"'
This will be the equivalent of you typing, at a CMD.EXE prompt:
qtvars.bat vsstart qt.sln /BUILD "Debug|Win32"
Using the above, you are essentially forcing the passing of the double-quotes on to CMD.EXE (instead of bash eating them away.) The outermost single quotes instruct bash not to interpret or touch in any way what's inside them; the inner double-quotes instruct CMD.EXE to ignore any special characters (the pipe in this case) within.
Alternatively, you can also try:
$command $args "qt.sln" /BUILD 'Debug\|Win32'
This should be the equivalent of you typing, at a CMD.EXE prompt:
qtvars.bat vsstart qt.sln /BUILD Debug\|Win32
Note the use of single quotes (!), which ensure that bash will not interpret the \ (and, instead, will pass it as-is to CMD.EXE.)
Here's another solution (workaround?) I've found:
first, ensure an environment variable defines the pipe character, for example:
set PIPE="|"
later, run the command specifying the above defined environment variable name:
"c:\(...)\devenv.com" foo.sln /build Debug%PIPE%Win32
That does the job even if there are multiple wrappers between the caller and the callee. I'm now using it with a very long chain of wrappers:
Python/Linux -> VirtualBox guest's executeProcess -> Cmd/Windows -> devenv.com
(cross posted to: How to pass a quoted pipe character to cmd.exe?)
Escaping a piping character in the Windows scripting language is done with a caret (^). I just had to do this the other day. I know this is old, but I thought I would post what I found in case others ran across this, like I did.
I'd consider going the easy route, and passing a placeholder-token instead - "$P", and then replace it within the CMD/Batch file; e.g. using the 'UnxUtils' SEd command to do the replacement:
For /F "usebackq delims=" %%r in (`Echo %Cmd% ^| sed -e "s/$P/|/g"`) do #Set Cmd2=%%r
REM Now run the command, with the proper pipe symbol in place
%Cmd2%
So having passed the command arg/CMD script args - "git status $P wc -l".
Interesting! What does escaping the | do?
Do these work?
echo "Debug|Win32"
echo "qt.sln" /BUILD 'Debug|Win32'