cmd.exe vs & for executing batch file? - powershell

I want to execute a batch(*.bat) file from Powershell. Is there any reason not to use option 1 below?
& path-to-batch-file
cmd.exe /c path-to-batch-file

I can only think of corner cases...
Option 1 probably uses the default file handler for .bat files. If that handler is changed, corrupted, or different than expected, a problem could arise. Though, this seems pretty unlikely.
Option 2 gives you the power to pass additional parameters to cmd.exe, which might be valuable.
Option 2 might be slightly faster, but it's only a guess: in Option 1 powershell might need to lookup the shell handler for batch files from the registry first, then execute those instructions, whereas Option 2 needs only find the cmd.exe executable and run it with supplied parameters.

Related

How to run powershell quietly from registry key?

I've added an option to copy a proper UNC path to the context menu of all directories via PowerShell.
Edit:
I didn't mention that I'm actually using two different keys: One to copy the UNC of the current directory, and one to copy it from a different directory. I didn't think it would make a difference, but it does.
End Edit
Currently, the key value is as follows:
powershell.exe -WindowStyle Hidden -Command . <path I have to censor>\Save-To-Clipboard.ps1 \"%L%\"
Expected behaviour:
The PowerShell script is run quietly.
Actual behaviour:
A PowerShell Window pops up and closes itself.
The same thing happens with cmd.
I've tried using a VBS wrapper as well, but it needs the current path as an argument, which I can't figure out how to do. Simply putting it after the filename as you would in the command line results in the error:
This file does not have an app associated with it for performing this
action. Please install an app or, if one is already installed, create
an association in the Default Apps settings page.
Key value here:
<path I have to censor>\ClunkyWrapper.vbs \"%L%\"
Admittedly, this is my first time running a command from a registry key, and I can't seem to find any resources about this topic. (I might just not know what exactly to google for.) So I would be thankful for more general information on how to run commands from registry keys as well.
Okay, I found a way.
First of all, apparently whether you need to use %L% or %V% depends on the key. I can't tell you why, unfortunately.
That solves the error message of the VBA script, but it still wouldn't run like this.
So I then used wscript.exe, and it finally worked.
wscript.exe <secret path>\ClunkyWrapper.vbs "%V%"

How to Automate scripts with options in Powershell?

I'm not a native English speaker as such pardon some discrepancy in my question. I'm looking at a way to Automate option selection in Programs/Scripts running via PowerShell.
For example:
Start-Process -FilePath "velociraptor.exe" -ArgumentList "config generate -i"
In the above snipper PowerShell will run Velociraptor and initiate the configuration wizard from a Ps1 file. The Wizard has few options. After running it will generate some Yaml files.
As such what would be the way to have PowerShell Script automate the option selection process? I know what the option should be. I looked around but I don't know proper terms to find what I need. Nor am I sure this can be done with PowerShell.
The end goal is to have the Ps1 download Exe, run the config command and continue with choosing the selection based on predefined choices. So far I gotten download and lunching of the velociraptor.exe working. But not sure how to skip the window in screenshot and have PowerShell script do it instead.
I couldn't find a CLI reference for velociraptor at https://www.velocidex.com/, but, generally speaking, your best bet is to find a non-interactive way to provide the information of interest, via dedicated _parameters_ (possibly pointing to an input file).
Absent that, you can use the following technique to provide successive responses to an external program's interactive prompts, assuming that the program reads the responses from stdin (the standard input stream):
$responses = 'windows', 'foo', 'bar' # List all responses here
$responses | velociraptor.exe config generate -i

When calling Win32 ShellExecute within ANY language, how can I stop program execution until ShellExecute is done?

This question is similar to most about ShellExecute, with one exception: I want to use ShellExecute because it's simple to use and it can be called with a few lines from within many programming languages including the now sunsetted Visual FoxPro.
Most existing solutions rely on doing stuff with threads, looking for processes (not reliable if it's a common process name) or ShellExecuteEx which is much more complicated to use. I was not satisfied with other solutions out there.
The issue is that I have a file operation that takes a few seconds to complete: unzipping an archive. How do I know that I am done?
The solution is to create a semaphore from within the command being executed. This can be done by running a batch file, but if that is not desired, there is another way to handle it with a one-liner using cmd.exe:
ShellExecute(0, 'open', 'cmd.exe', '/c 7z.exe e Invoices.zip | find "Everything is Ok" > semaphore.txt', workingDirectory, 1)
This one-liner uses CMD to pipe the output of 7zip to Find, which looks for the line of text that indicates completion. Of course, this fails if there's an error.
If you don't have output you to wait for (for piping into FIND), you can use & echo thusly:
... & echo > semaphore.txt
This waits until ... is done, then echos a newline to semaphore.txt.
At the end, you'll have a semaphore.txt file inside your working directory. All you have to do is look for this file to appear, and you know that execution is done. Delete this file and continue with your program.

Powershell Script doesn't work when starting it by double-clicking

I got some strange behaviour when executing a powershell script.
When I run my script using the ISE it works just fine.
When I open Powershell.exe and run my script it works just fine.
When I open cmd, and start my script using powershell.exe -noexit
./myscript.ps1, myscript works just fine.
When I double-click myscript however, powershell opens for some milliseconds, I see that it shows some error (red font) and the powershell window closes. I'm unable to track down the error causing this problem since the powershell windows closes to fast.
I even tried one single big try-catch block around my hole script, catching any [Exception] and writing it down to a log file. However: the log file is not generated (catch is not called).
How can I track that issue? What could possibly be causing the trouble?
Please note that my execution-policy is set to unrestricted.
Before trying the suggestion invoke this to see your current settings (if you want restore them later):
cmd /c FType Microsoft.PowerShellScript.1
Then invoke this (note that you will change how your scripts are invoked "from explorer" by this):
cmd /c #"
FType Microsoft.PowerShellScript.1=$PSHOME\powershell.exe -NoExit . "'%1'" %*
"#
Then double-click the script, it should not exit, -NoExit does the trick. See your error messages and solve the problems.
But now all your scripts invoked "from explorer" keep their console opened. You may then
remove -NoExit from the above command and run it again or restore your
original settings.
Some details and one good way to invoke scripts in PS v2 is here.
Unfortunately it is broken in PS v3 - submitted issue.
by default, for security reason when you double clic on a .ps1 file the action is : Edit file, not Run file .
to execute your script : right-click on it and choose run with powershell
I also wasn’t able to run a script by double clicking it although running it manually worked without a problem. I have found out that the problem was in the path. When I ran a script from a path that contained spaces, such as:
C:\Users\john doe\Documents\Sample.ps1
The scipt failed to run. Moving the script to:
C:\Scripts\Sample.ps1
Which has no spaces, solved the problem.
This is most likely an issue with your local Execution Policy.
By default, Powershell is configured to NOT run scripts that are unsigned (even local ones). If you've not signed your scripts, then changing your default double-click 'action' in Windows will have no effect - Powershell will open, read the execution policy, check the script's signature, and finding none, will abort with an error.
In Powershell:
Help about_execution_policies
gives you all the gory details, as well as ways to allow unsigned scripts to run (within reason - you'd probably not want to run remote ones, only ones you've saved onto the system).
EDIT: I see at the tail end of your question that you've set Execution Policy to 'unrestricted' which SHOULD allow the script to run. However, this might be useful info for others running into execution policy issues.
If you would catch the error you will most likely see this
The file cannot be loaded. The file is not
digitally signed. The script will not execute on the system. Please
see "Get-Help about_signing" for more details.
Because you are able to run it from the shell you started yourself, and not with the right mouse button click "Run With PowerShell", I bet you have x64 system. Manually you are starting the one version of PowerShell where execution policy is configured, while with the right click the other version of the PowerShell is started.
Try to start both version x64 and x86 version and check for security policies in each
Get-ExecutionPolicy
I was in exactly the same situation as described in the question : my script worked everywhere except when double-clicking.* When I double-clicked a powershell windows would open but then it will close after a second or so. My execution-policy is also set to unrestricted.
I tried the selected answer concerning FType Microsoft.PowerShellScript.1 but it didn't change anything.
The only solution I found was a work around: create a bat file which start the powershell.
Create a file, copy this and modify the path : powershell.exe -File "C:\Users\user\script\myscript.ps1"
Save it as a .bat
Double-click the bat
I also used .ahk to start my powershell with a shorcut and it didn't work when pointing directly to the powershell. I had to point to the .bat

Can a Batch script know if it's called from PowerShell?

Is there any way a batch script can know if it's called from PowerShell (without extra parameters feeded)?
Need something like..
if (%THIS_BATCH_CALLED_FROM_POWERSHELL%)
... warn the user or drop back to powershell to execute the proper instructions...
Question related with this question - virtualenv-in-powershell.
You could use a tool like "tlist.exe /t" or this one to display the PIDs of the current process and all parent processes. You could check each one of those PIDs to see if any correspond to PowerShell.exe.
You could add a default warning in the script and pass it a flag that tells it not to show the warning. When you call it from power shell pass it that flag.
In my Powershell environment (a PS 2.0 CTP), I seem to have an environment variable PSMODULEPATH which is not set by the normal command line environment, but still exists when Powershell has a child CMD.exe shell.
I think you might be able to "reliably enough" check for the existence of PSMODULEPATH in your batch script.