Executing Powershell from qmake hangs indefinitely - windows

I am trying to execute a Powershell command from within qmake. A simplified .pro file looks like this:
$$system(powershell -NonInteractive -Command \"[long]((date).touniversaltime()-[datetime]'1970-01-01').totalmilliseconds\")
However, running qmake with this file or opening it in QtCreator will create a powershell.exe process which hangs indefinitely. Running Powershell from the command line works without problems, the command prints the expected timestamp and exits immediately.
What could be causing this hang and how to resolve it?
Tested on Windows 7 x64 with Powershell version 2.0 and qmake version 3.1 / Qt 5.9.8.

Don't use the quote.
The possible command is:
$$system(powershell -Command Get-Date -Format "yyyy.MM.dd.HH.mm")

Related

What would the bat version of this powershell command be and what command would be used as the install line in sccm?

Below is the command I use in powershell to launch the app, I have deliberately used * wildcard as the app folder name changes slightly for each device. I want to replicate the wildcard in the bat file.
Start-Process -FilePath "C:\ProgramData\theapp*\theapp.exe"
therefore, for the bat file version could I use:
start "C:\ProgramData\theapp*" theapp.exe
Also, if this could be used : what install line would be best to launch this in sccm?
Massive thank you!

install two wireshark through command in line silent mode on window

I am looking for the way where i can install two Wireshark version using window command line.
I am trying below PowerShell command, it is working fine but deleting old one. Is there any way to keep both version and install automatically from PowerShell without user intervention.
Start-Process Wireshark-win64-2.6.6.exe -Wait -ArgumentList #('/NCRC', '/S', '/desktopicon=no' ,'/quicklaunchicon=no', '/D=C:\Program Files\Wireshark2');
Thanks
Shrwan

git-bash.exe: how to run single command?

I am using the portable version of Git for Windows, but when I use git-bash.exe to run a single command, it looks like it does not work. Can you help me find out where is the problem?
I have a PowerShell script to call below command. But looks like it does not work. I am not able to let the new cmd window to pause to see the error log:
cmd /c "d:\git\git-bash.exe dos2unix d:\test\my-script.sh"
But If double click and run bash-exe.sh, then in the git-bash.exe window type
dos2unix d:\test\my-script.sh
then it works.
There are a few different ways to call EXEs from PowerShell. One of the best ways I've found that allows you the most control is the Start-Process cmdlet. Instead of calling a cmd.exe window try this:
Start-Process -FilePath 'd:\git\git-bash.exe' -ArgumentList 'dos2unix d:\test\my-script.sh'
Better yet convert that shell script into native PowerShell! :)
Anyway, I got a walkaround. In the portable git, under \usr\bin, there is a exe called dos2unix.exe, so that I can directly call dos2unix.exe from my powershell scripts, not from git-bash.exe then call dos2unix command.
cmd /c "d:\git\usr\bin\dos2unix.exe d:\test\my-script.sh"
With this walkaround, the problem resolved. But I still don't know why powershell calls dos2unix to parse my file does not work (I mean the file is not converted after call that command).
Another solution is use the base.exe, e.g:
"%ProgramFiles%\Git\bin\bash.exe" start.sh

Powershell Command post-build VS2010

I have a command in my post-build event command line in Visual Studio 2010
Powershell -command .'$(SolutionDir)Powershell\MoveFiles.ps1'
And when the event runs, I get an error that the command "exited with code 1."
However when I run the same command on the command line (see below), with an actual directory instead of the VS2010 macro, it works perfectly.
Powershell -command .'C:\TFS\MyProject\Main\Source\Powershell\MoveFiles.ps1'
So it seems that the problem lies with how VS2010 is executing the command.
What could be causing this problem?
[UPDATE]
I have also tried changing the post-build event to:
C:\Windows\SysWOW64\WindowsPowerShell\v1.0\Powershell -command .'C:\TFS\MyProject\Main\Source\Powershell\MoveFiles.ps1' and I get the same result as described above. It works when run from the command line but not from VS 2010.
Two things:
Use the -file parameter instead of the -command parameter.
Use double quotes.
powershell.exe -file "$(SolutionDir)Powershell\MoveFiles.ps1"
If you are running on a 64-bit OS, you will need to specify the full path to the 64-bit version of powershell since Visual Studio is a 32-bit app.
There is an answer in this question that has a workaround for the problem.

How to run a PowerShell script

How do I run a PowerShell script?
I have a script named myscript.ps1
I have all the necessary frameworks installed
I set that execution policy thing
I have followed the instructions on this MSDN help page
and am trying to run it like so:
powershell.exe 'C:\my_path\yada_yada\run_import_script.ps1' (with or without --noexit)
which returns exactly nothing, except that the file name is output.
No error, no message, nothing. Oh, when I add -noexit, the same thing happens, but I remain within PowerShell and have to exit manually.
The .ps1 file is supposed to run a program and return the error level dependent on that program's output. But I'm quite sure I'm not even getting there yet.
What am I doing wrong?
Prerequisites:
You need to be able to run PowerShell as an administrator
You need to set your PowerShell execution policy to a permissive value or be able to bypass it
Steps:
Launch Windows PowerShell as an Administrator, and wait for the PS> prompt to appear
Navigate within PowerShell to the directory where the script lives:
PS> cd C:\my_path\yada_yada\ (enter)
Execute the script:
PS> .\run_import_script.ps1 (enter)
Or: you can run the PowerShell script from the Command Prompt (cmd.exe) like this:
powershell -noexit "& ""C:\my_path\yada_yada\run_import_script.ps1""" (enter)
according to Invoking a PowerShell script from cmd.exe (or Start | Run) by Kirk Munro.
Or you could even run your PowerShell script asynchronously from your C# application.
If you are on PowerShell 2.0, use PowerShell.exe's -File parameter to invoke a script from another environment, like cmd.exe. For example:
Powershell.exe -File C:\my_path\yada_yada\run_import_script.ps1
If you want to run a script without modifying the default script execution policy, you can use the bypass switch when launching Windows PowerShell.
powershell [-noexit] -executionpolicy bypass -File <Filename>
Type:
powershell -executionpolicy bypass -File .\Test.ps1
NOTE: Here Test.ps1 is the PowerShell script.
I've had the same problem, and I tried and tried... Finally I used:
powershell.exe -noexit "& 'c:\Data\ScheduledScripts\ShutdownVM.ps1'"
And put this line in a batch-file, and this works.
If you only have PowerShell 1.0, this seems to do the trick well enough:
powershell -command - < c:\mypath\myscript.ps1
It pipes the script file to the PowerShell command line.
Pretty easy. Right click the .ps1 file in Windows and in the shell menu click on Run with PowerShell.
Open PowerShell in administrator mode
Run: set-executionpolicy unrestricted
Open a regular PowerShell window and run your script.
I found this solution following the link that was given as part of the error message: About Execution Policies
Make sure to run set-ExecutionPolicy default once you're done, or you will be exposed to security risks.
Using cmd (BAT) file:
#echo off
color 1F
echo.
C:\Windows\system32\WindowsPowerShell\v1.0\powershell.exe -ExecutionPolicy Bypass -File "PrepareEnvironment.ps1"
:EOF
echo Waiting seconds
timeout /t 10 /nobreak > NUL
If you need run as administrator:
Make a shortcut pointed to the command prompt (I named it
Administrative Command Prompt)
Open the shortcut's properties and go to the Compatibility tab
Under the Privilege Level section, make sure the checkbox next to "Run this program as an administrator" is checked
An easy way is to use PowerShell ISE, open script, run and invoke your script, function...
In case you want to run a PowerShell script with Windows Task Scheduler, please follow the steps below:
Create a task
Set Program/Script to Powershell.exe
Set Arguments to -File "C:\xxx.ps1"
It's from another answer, How do I execute a PowerShell script automatically using Windows task scheduler?.
If your script is named with the .ps1 extension and you're in a PowerShell window, you just run ./myscript.ps1 (assuming the file is in your working directory).
This is true for me anyway on Windows 10 with PowerShell version 5.1 anyway, and I don't think I've done anything to make it possible.
Give the path of the script, that is, path setting by cmd:
$> . c:\program file\prog.ps1
Run the entry point function of PowerShell:
For example, $> add or entry_func or main
You can run from cmd like this:
type "script_path" | powershell.exe -c -
Use the -File parameter in front of the filename. The quotes make PowerShell think it is a string of commands.
I've just found the method what Microsoft do when we right click on a ps1 script and click on "Run with PowerShell" :
"C:\Windows\System32\WindowsPowerShell\v1.0\powershell.exe" "-Command" "if((Get-ExecutionPolicy ) -ne 'AllSigned') { Set-ExecutionPolicy -Scope Process Bypass }; & 'C:\Users\USERNAME\Desktop\MYSCRIPT.ps1'"
With the appropriate execution policy, you should just be able to call the file directly and Windows will associate it with PowerShell
C:\my_path\yada_yada\run_import_script.ps1
That does not do so well with arguments. The real answer to your question is that you are missing the & to say "execute this"
powershell.exe '& C:\my_path\yada_yada\run_import_script.ps1'

Resources