Can call headless from cmd but not from powershell - powershell

In dos when I paste this command it works:
"C:\Program Files (x86)\Google\Chrome\Application\chrome.exe" https://google.com --screenshot=c:\test\google.png --headless --hide-scrollbars --window-size=1920,1080 --disable-gpu &
When I do the same in Powershell it doesn't. I guess my syntax not right ?

You have to use Start-Process in Powershell and parse the Arguments:
Start-Process -FilePath "C:\Program Files (x86)\Google\Chrome\Application\chrome.exe" -ArgumentList "https://google.com","--screenshot=c:\test\google.png","--headless","--hide-scrollbars","--window-size=1920,1080","--disable-gpu"

Related

How to escape space in PowerShell?

I run this command in PowerShell
Start-Process -FilePath 'C:\Program Files\Typora\Typora.exe' -ArgumentList 'C:\Users\Administrator\Desktop\Hello world.md'
But Typora says
C:\User\Administrator\world.md does not exist
It looks like PowerShell executes
Start-Process -FilePath 'C:\Program Files\Typora\Typora.exe'
-ArgumentList 'C:\Users\Administrator\Desktop\Hello' 'world.md'
I want to escape the space but the single-quoted not working...
PowerShell version: 5.1.19041.610
PowerShell may not quote strings properly when calling external executables. See
Curl in PowerShell with custom cookie file
PowerShell or CMD errors on space in file path
So to fix this you need to pass the literal " to the exe file by escaping it properly
Start-Process -FilePath 'C:\Program Files\Typora\Typora.exe' `
-ArgumentList '"C:\Users\Administrator\Desktop\Hello world.md"'
Try adding ` to excape spaces in powershell:
Start-Process -FilePath 'C:\Program Files\Typora\Typora.exe' -ArgumentList 'C:\Users\Administrator\Desktop\Hello`world.md'

Use the start-process command to get the return value of the running program

Using the .bat file code below, I can get the value returned after running the program.
"C:\Program Files\www\www.exe" /start "/My app/abc" && echo %ERRORLEVEL% > c:\ResultCode.txt"
How to use the powershell code to achieve the same functionality
I use the code below and I didn't get the result.
Start-Process -FilePath "C:\Program Files\www\www.exe" -ArgumentList "/start `"/My Processes/abc`"" -RedirectStandardError "c:\ResultCode.txt"

Start-Process cmdlet with -ArgumentList throw "Invalid Argument" error

I want to register a dll using gacutil.exe.
Start-Process -Wait -FilePath "C:\Program Files (x86)\Microsoft SDKs\Windows\v8.1A\bin\NETFX 4.5.1 Tools\gacutil.exe" -ArgumentList '/u', "USB\CRM\Common"
It throw the error:
You don't need Start-Process if you just want to run a command at the PowerShell command line. PowerShell can run commands typed at its prompt. (It's a shell; one of the purposes of a shell is to run commands you enter.) Since the command contains spaces, enclose it in " and execute it with the & (call or invocation) operator.
& "C:\Program Files (x86)\Microsoft SDKs\Windows\v8.1A\bin\NETFX 4.5.1 Tools\gacutil.exe" /u USB\CRM\Common

PowerShell execute external command in same window

I am trying to execute an external command using powershell, without having the second program to popup, I need to execute this program within the same PowerShell window and output both the log and the errors.
I started with this:
$outcome = Start-Process -Wait -FilePath "cmd.exe" -ArgumentList "dir" -NoNewWindow 2>&1
$outcome
But it doesn't work as expected. I still see the new window popping up with DOS and no redirect at all about the output, the errors and so on.
Am I doing something wrong?
Does this work for you?
$outcome = Invoke-Expression "cmd.exe /c dir"
$outcome
My suggestion is to use the call operator
& cmd.exe /c dir
You can just run it. You're missing "/c".
$outcome = cmd /c dir
With the added complication of start-process, you'd have to save the output to a file.
Start-Process -Wait -FilePath "cmd.exe" -ArgumentList "/c","dir" -NoNewWindow 2>&1 -RedirectStandardOutput cmd.log

Running a PS1 script using a batch file (.bat)

Currently the following is my path for launching the VMware vSphere PowerCLI command prompt. I wish to run my sample.ps1 script automatically using a batch file. How can I incoporate sample.ps1 into this path and create a batch file?
C:\WINDOWS\system32\WindowsPowerShell\v1.0\powershell.exe -psc "C:\Program Files\VMware\Infrastructure\vSphere PowerCLI\vim.psc1" -noe -c ". \"C:\Program Files\VMware\Infrastructure\vSphere PowerCLI\Scripts\Initialize-PowerCLIEnvironment.ps1\""
If you are working with PowerShell 2.0, you can use the -file parameter of PowerShell.exe
C:\WINDOWS\system32\WindowsPowerShell\v1.0\powershell.exe -psc "C:\Program Files\VMware\Infrastructure\vSphere PowerCLI\vim.psc1" -noe -file "C:\Program Files\VMware\Infrastructure\vSphere PowerCLI\Scripts\Initialize-PowerCLIEnvironment.ps1"
If you are working with PowerShell 1.0, you can use -command parameter this way
C:\WINDOWS\system32\WindowsPowerShell\v1.0\powershell.exe -psc "C:\Program Files\VMware\Infrastructure\vSphere PowerCLI\vim.psc1" -noe -command "& 'C:\Program Files\VMware\Infrastructure\vSphere PowerCLI\Scripts\Initialize-PowerCLIEnvironment.ps1'"
echo off
Title,Report Script &color 9e
for /f "usebackq delims=$" %%a in (`cd`) do (
set SCRIPTDIR=%%a
)
(Set ScriptFile=%SCRIPTDIR%\Report.ps1)
C:\Windows\System32\WindowsPowerShell\v1.0\powershell.exe -psc "C:\Program Files\VMware\Infrastructure\vSphere PowerCLI\vim.psc1" -c ". \"C:\Program Files\VMware\Infrastructure\vSphere PowerCLI\Scripts\Initialize-PowerCLIEnvironment.ps1\";%ScriptFile%"
You can use this to launch arbitrary .ps1 scripts via .bat files by calling the bat file like your ps1. Then extract the name of file in batch and call powershell with it.
For a ready to use solution, use the following Gist: https://gist.github.com/JonasGroeger/10417237
I saw this code in another page, I test it in a W2012 R2 and it runs.
I hope it work:
C:\>powershell "C:\>1\file.ps1"