I have installed cygwin and I am about to execute a script written in powershell. I do it like this:
powershell "& ""C:\my\script.ps1"""
This works as expected, I have to do this that way because in that script I am executing another external command and so on ...
I would like to add some environment variable to that script, so I would like to write something like
powershell "$FOO="bar"; & ""C:\my\script.ps1"""
so I can then access $FOO variable in the script and do something with it. The idea is that if that variable is not defined, I use some default value. I know that this could be also achieved with some environment variables or I could put these variables to the profile (profile.ps1) but I want to get rid of that file so I need none and I can override the default value with the variables as I showed.
but is says that:
=bar : The term '=bar' is not recognized as the name of a cmdlet, function,
script file, or operable program. Check the spelling of the name, or if a path
was included, verify that the path is correct and try again.
At line:1 char:1
So I was thinking that something like this could work:
powershell { $web = "google.com" ; & ping.exe $web }
But it works only in powershell console and not in cygwin, it cygwin it says that
-bash: syntax error near unexpected token `&'
So it seems like that & is treaten as bash character. I tried to escape it in thousand ways, e.g.
bash -c "'powershell { \$web = \"google.com\" ; & ping.exe \$web }'"
But this is the output
bash: powershell { $web = "google.com" ; & ping.exe $web }: command not found
Thank you for a hint.
UPDATE
I am able to do this:
powershell "Invoke-Command -ScriptBlock {\$env:FOO = \"google.com\" ; & ""C:\my\script.ps1"" }"
But when I am trying to access that FOO variable throught $env:FOO it is empty, it seems like I am unable to do so because that script is running in another scope or what ...
this command will pass an environment variable from cygwin ($FOO="bar") to powershell, then run another powershell command (dir env:) which lists the environment variables (proving that it was set):
powershell "\$env:FOO=\"bar\";dir env:"
replace the dir env: part with your script call and this should work for you.
edit: here's the command (quoted slightly differently) including the call to an external script:
powershell '$env:FOO="bar";& "C:\script name.ps1"'
Related
I want to make a file having Windows Powershell commands. Then I want to open it with windows powershell directly and without pressing any key I want windows powershell start running those commands directly same as command prompy I can make .cmd or .bat file.
For example:
These are two commands or Powershell, I want to save this file. Then I want directly execute this file by powershell. I have tried to save it as ps1 and ps2 extension as well but not working. Many methods online are not working. Any solution?
PowerShell script files, across all versions, use the .ps1 filename extension.
From within PowerShell, you can invoke them directly, e.g., .\script.ps1
Note that, unlike in cmd.exe, you must use .\ (or a full path) in order to execute a file located in the current directory - just script.ps1 won't work - see this answer for background information.
From cmd.exe, you must use PowerShell's CLI (powershell.exe in Windows PowerShell / pwsh in PowerShell [Core] v6+) in order to execute a script file:
powershell.exe -File script.ps1
pwsh -File script.ps1 (-File may be omitted)
Note that with -File the .\-prefix is not required.
However, if you use -Command (-c) instead (which is the default with powershell.exe, whereas pwsh now defaults to -File), you do need the .\, because the -Command argument(s) are interpreted as a piece of PowerShell code, i.e. as if you had submitted it inside a PowerShell session.
You've discovered this in your own answer, where you pass a PowerShell command directly to the (implied) -Command parameter.
Note, however, that it's better to double-quote such commands, so as to prevent cmd.exe from interpreting certain characters itself, which breaks the call.
For instance, the following call would break, if you didn't enclose the -Command (-c) argument in "...":
# From cmd.exe; "..." required.
C:\>powershell.exe -c "Write-Output 'a & b'"
a & b
Another important consideration is that you need to escape embedded " chars. as \" for the CLI (even though PowerShell-internally you would use `" or ""):
# From cmd.exe; note the inner " escaped as \"
C:\>powershell.exe -c "Write-Output \"hi there\""
hi there
I have found the solution. I use command powershell.exe and can directly execute powershell commands within cmd.
powershell.exe $MyVariable=Get-Content .\Path.txt
is working fine for me
I have a powershell 2 script that I'm trying to develop. The purpose of this script is to wrap around a batch script and intelligently choose what version of said batch script to run. Somewhere along the lines in my code I have some logic that goes like this:
& $myCommand $args
$myCommand is the fully qualified filename of the batch file I want to run. $args is the args passed into this script. This works except it opens up a command window when running $myCommand. How do I prevent this so that the output is within the same powershell shell?
What's odd is that if I execute the command directly, it shows up the way I want it. So something like:
C:\myCommand.bat $args
Given that I need to choose which command I want to run at runtime, how do I make it so the output is in the same shell when I use the '&' to execute the command in the variable? Thanks!
Use Start-Process with the -NoNewWindow parameter instead of &:
Start-Process -filepath C:\myCommand.bat -argumentList #("arg1","arg2") -NoNewWindow
I have a command I execute in the bash which requires an environment variable. If I call it like this, everything works fine:
export MYVAR=value & my_first_command
But now I want to pipe the result to a second command, which requires the same environment variable. I tried this one:
export MYVAR=value & my_first_command | my_second_command
In that case, MYVAR seems not to be set for my_second_command. What's the correct syntax to make MYVAR available to my_second_command too?
The & is incorrect and should be ; instead.
Using & is syntactically valid, and runs a command in the background; but running export as a background task makes no sense at all.
I have to run some shell scripts in Windows using Cygwin. I am able to achieve that using %BASH% --login -i "/cygdrive/d/script.sh", where %BASH% is an environment variable in Windows set to C:\cygwin\bin\bash.exe.
The above script executes without any problem. But when there are Cygwin environment variables inside the .sh files then it doesn't work.
For example, I have a created a Cygwin environment variable $EXE_PATH = /cygdrive/d/somepath/dir. Inside my script the line is
$EXE_PATH/some.exe -f /cygdrive/d/password.txt
There is error in this line every time I run the script.
Error is something to do with the some.exe/password.txt not being found.
If I run the same command from the Cygwin terminal, it works fine. Is there something wrong with /cygdrive?
Here is the script
#!/bin/sh
#TESTLEELA=/cygdrive/d
echo simple shell script
echo $EXE_PATH
$EXE_PATH/runube.exe -f $TESTLEELA/password.txt DVRP910 *ALL R0006P XJDE0001 QBATCH B H S //NASRVNY1
Change the line
$EXE_PATH = /cygdrive/d/somepath/dir
to
EXE_PATH=/cygdrive/d/somepath/dir (in cygwin shell script)
i have following situation:
a powershell script with this variables:
$RUBY = "C:\installing\ruby\bin\ruby.exe"
$ARGS = "C:\files\sript.rb $Arg1 $Arg2 $Arg3"
in a batch file i can write
%RUBY% %ARGS%
and it works.
but how can i do this in powershell?
It is important that the ruby installing path is variable.
I tried it with Start-Process but only ruby.exe was started, without script.
Any hints?
Thanks.
First, $args is an automatic variable managed by PowerShell, so I would avoid trying to declare your own variable with the same name.
To invoke a command stored in a variable from PowerShell, you can use the call operator &:
& $ruby 'script.rb'
If you need to build up the list of arguments, I would recommend creating an array, instead of mashing them all together into a single string:
$rubyArgs = #('C:\files\script.rb', $Arg1, $Arg2, $Arg3)
& $ruby $rubyArgs
If you need to pass more complicated arguments, you may find this answer useful.