invoking an executable on powershell and then type commands - batch-file

I am trying to call a executable file as follows
.\test_use.exe
then I press the return key to get a interface like
"Enter a value between 1 and 5"
I type 4 and get some details. Is there a way to automate it via powershell. I tried passing
.\test_use.exe 4
But it does not work. I also tried
$input = "4"
Invoke-command ".\test_use.exe" -InputObject $input
I get the following error
Invoke-command: Parameter cannot be resolved using the specified named characters
Does anyone have any pointers ??
Will it be easier to do this in batch file and store the output in a text file?

In batch you might be able to do the following:
(echo 4) | test_use.exe
See this question - how to pass input to .exe in batch file?

Related

Read text file content using batch file command

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%

CMD runs in terminal but not in .BAT

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?

Powershell redirection of an array to a file

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

Run C program from shell script [duplicate]

I have a script in unix that looks like this:
#!/bin/bash
gcc -osign sign.c
./sign < /usr/share/dict/words | sort | squash > out
Whenever I try to run this script it gives me an error saying that squash is not a valid command. squash is a shell script stored in the same directory as this script and looks like this:
#!/bin/bash
awk -f squash.awk
I have execute permissions set correctly but for some reason it doesn't run. Is there something else I have to do to make it able to run like shown? I am rather new to scripting so any help would be greatly appreciated!
As mentioned in #Biffen's comment, unless . is in your $PATH variable, you need to specify ./squash for the same reason you need to specify ./sign.
When parsing a bare word on the command line, bash checks all the directories listed in $PATH to see if said word is an executable file living inside any of them. Unless . is in $PATH, bash won't find squash.
To avoid this problem, you can tell bash not to go looking for squash by giving bash the complete path to it, namely ./squash.

Exact command for starting a batch file by using powershell

I know this question has been asked before and I found a thread on here which almost gives me the solution I need.
Here is the link: How to run batch file using powershell
But this only works when I write out the full path. For example:
c:\Users\Administrator\Desktop\dcp_bearbeitet\start.bat -p c:\Users\Administrator\Desktop\dcp_bearbeitet\start.prop
What I want to reach is a solution which accepts a path with parameters, like this one here:
c:\Users\Administrator\Desktop\dcp_bearbeitet\$title\start.bat -p c:\Users\Administrator\Desktop\dcp_bearbeitet\$title\start.prop
Whereas $title contains the name of my file which I am using in this case. I know that I can create another parameter for the -p command and I know that this works, but unfortunately when I try the same method for the first command I always get an error message.
I hope you guys know a way to solve this problem.
I think Invoke-Expression could help here.
Just construct your path like you want it to be, for example:
$title = "file"
$path = "C:\Users\Administrator\Desktop\dcp_bearbeitet\$title\start.bat -p c:\Users\Administrator\Desktop\dcp_bearbeitet\$title\start.prop"
and then invoke it:
Invoke-Expression $path
Regards Paul

Resources