PowerShell Open URL in New Window - [system.Diagnostics.process]::Start("","") - powershell

So I'm using PowerShell (specifically version 5.1) in order to write a script for my work that essentially opens some websites depending on what the user selects from available options.
In order to open the website (for this example, let's just say I'm going to Google), I'm using [system.Diagnostics.Process]::Start("chrome","https://www.google.com"). This does open the website, but I'm attempting to open it into a new window entirely. In current state, I can only accomplish this by opening a new window manually and then running the script.
Any suggestions on any switches or anything that might work? I've seen a few on other sites talking about --new-window or -new = 1, but those were for other commands and don't seem to work on this one.
I'm also specifically using Chrome because the sites I am attempting to open require a sign-in, so I already have a Chrome Window open, and it defaults to opening all of my new tabs on the same window, which is what I'm attempting to avoid.
Any help would be greatly appreciated!

The Chrome CLI parameter that requests opening a given URL in a new window is -new-window
It's simpler to use PowerShell's Start-Process cmdlet rather than the underlying .NET API directly.
Therefore:
Start-Process chrome.exe '-new-window https://www.google.com'

Related

Automating Upload through Windows File Explorer in Microsoft Edge

I am trying to automate a web process using selenium.
The process clicks an "upload image" button and then sends the file name to a Windows File Explorer Window.
My solution as of now is to use selenium to click the "upload image" button and then to use a VBscript, via the sendKeys command, to send the file name to the active windows file explorer window.
As others have noted this causes some issues, foremost of which is that accidently focusing on another window sends the keys to it instead of file explorer.
I'm trying to make this process less "hacky".
I know MS Edge uses a Windows Utilities subprocess to open the file explorer window.
As of now, I can get the Id of this subprocess, but in a round about way, namely, by finding the most recently created subprocess managed by the browser. Obviously, this is a far cry from automation.
Using windows PowerShell, I've I have not managed to locate any information about this subprocess that could help with my automation task. For example, I could not get any unique identifying characteristics of the subprocess other than its process Id and I can't access its standard input. So far, all I can do is kill the sub process.
What I'm looking for is a rough outline (no code, just an architectural overview) of what it would take to do in PowerShell, what MS edge does after invoking the Windows Utility subprocess to upload a file to a website.
If someone has any suggestions on books I can read, I'd appreciate that as well.
Just looking for someone to point me in the right direction, really.

How can I use the "PowerShell Integrated Console" in VS Code?

I have the PowerShell extension installed under VSCode. When I open up a PowerShell script, VS Code automatically pops open a "PowerShell Integrated Console" window in my terminal. It's different from the normal PowerShell terminal:
It's better than the default one because keyboard shortcuts like Ctrl+Backspace to delete a word work. But when I try to open one of these fancy terminals myself, the option isn't there:
How can I get one of these improved terminals without opening a script file, and how can I get VSCode to use them automatically instead of the old PS terminal?
Note:
Normally, a PIC (PowerShell Integrated Console) is automatically created the first time you open a PowerShell source-code file for editing in a session.
The instruction below show to how to directly create a PIC, which may also be helpful if you need to restart it after a crash - though you're usually prompted to create a new one when that happens.
Instruction as of v2022.12.1 of the PowerShell extension for VSCode (Visual Studio Code):
Execute the PowerShell: Show Session Menu command from the command palette (Ctrl-Shift-P).
This will create a PIC and show a submenu, which you can simply dismiss with Esc.
Note: If the PowerShell extension was already loaded but the PIC has crashed, choose Restart Current Session from the submenu instead.
Additionally, you may assign a keyboard shortcut to the command, by clicking the cog icon on the right edge of the command palette entry.
Because the PIC doesn't have an external executable entry point you can not define it as a custom shell profile (at least as of VSCode 1.67.1).
GitHub issue #3918 discusses implementing simpler ways to activate / load the PIC on demand as a future enhancement:
Making the PIC show in the list of available shells (as shown in your question, analogous to the extension-contributed JavaScript Debug Terminal entry) is one of the suggestions, but the concern is that there can only be one PIC as of this writing, so such an entry would behave differently from other shells in the list, which create a new session every time.
However, this concern would go away if support for multiple PICs were to be implemented, which is being proposed in GitHub issue #2418.

How to automate UI keyboard input? pywin32 PostMessage vs Powershell SendKeys

Motivation: I'm using a software that doesn't have an API to interface with ... I have no alternative and have to open the software, send simple key sequence, then close ... Again and again, so I want to automate this process.
Goal: Send combinations of keyboard inputs to an inactive window.
Progress: I wrote a powershell script that open, send keys, wait, then end the process, but it only works on active window. A part of the powershell code is as follows.
$appProcess = Start-Process -FilePath $path -PassThru
$wshell = New-Object -ComObject wscript.shell;
$wshell.AppActivate($appProcess.Id)
$wshell.SendKeys('%(E)E')
Stop-Process $appProcess -Force
It works, but only on active window (windows comes to the top). What I want is to run automate the window in the background. I found an article that point me to using PostMessage in Win32 API. Since the majority of my code uses python, I decided to move from powershell to pywin32.
Issue: I cannot get the PostMessage to send key to the right handler. I saw in this article that I may need to find the exact window, but I still don't really understand how. In powershell, I can directly send keys via $wshell.AppActivate($appProcess.Id).
hwndMain = win32gui.FindWindow(None, winname)
hwndChild = win32gui.GetWindow(hwndMain, win32con.GW_CHILD)
temp = win32api.PostMessage(hwndChild, win32con.WM_KEYDOWN, 0x45, 0)
# temp came out as None
Question: Is there a way to do this in pywin32 / Win32 API?
Edit: (May 8, 2020) Yes, I have heard that using SendKeys are not reliable, but since there is no alternative offered to questions like this one on SO, how should anyone learn the "right way"? If you think there is alternative, everyone will appreciate to see a solution in action. Please suggest edits to my post to improve the quality of the question instead of shooting it down.
Time ago I developed SendMessage utility that allows to post messages to other processes or Windows via the WIN32 API. I discovered that I can't send a message to an inactive window, so I searched for a method to activate a Window. After several tests, I found a trick that allows me to "reactivate" a Window:
The development of this program presented a problem: the items in the
Notepad topmost menu can not be selected if the window was not active.
I did some tests and discovered that this point depends on the
relation that the cmd.exe window have (has?) with the other window and
the particular program in the other window. For example, in the case
of Calculator Windows accessory, its menu items can be selected when
the window is not active, and even when the window is minimized! I
tried to develop a method to activate other window from the cmd.exe
one using just System-Defined messages. I did several tests using
diverse combinations of WM_ACTIVATE, WM_CANCELMODE, WM_ENABLE,
WM_ACTIVATEAPP, WM_SETFOCUS and WM_KILLFOCUS messages, with no
success. Fortunately, the items in the system menu of any window can
be selected from another window, and after a SC_RESTORE the restored
window remains active; this behavior makes possible to activate
Notepad and other windows from the cmd.exe one via a Minimize/Restore
procedure.
I think that using my SendMessage.exe utility you may test if you may send keys to your inactive window process or to activate it, so you may then translate such a method to your phyton code. For complete details on this matter, see this link.

Web UI Automation login and then submit form with Powershell/Curl equivalent

This is for a Windows environment: Windows 7-10 / Server 2008/2012.
I want to create a script which will automate login at a URL, navigate to a another page, and then submit a form on that page.
Open Internet Explorer, if not already opened
Login to www.example.com/login (takes me to www.example.com/home)
Once logged in, go to www.example.com/submit-form-data
Enter values for fields and submit the form via button click
At first, I thought maybe I could use something like iMacro, a greasemonkey alternative, or even bookmarklets. But that'd all still require some user intervention. If there is a macro program which can replay all the steps, perhaps I'd be open to that. Otherwise, I think I may need to employ some kind of PowerShell or Curl-like program for Windows to handle UI automation.
At least with PowerShell, I don't think submitting a form should be too bad. If that's all I had to do, I wouldn't need any help. The problem is how do I log into a site, then navigate to a specific URL, on top of submitting a form, all in the same script?
I don't have to use PowerShell. I'd be open to alternatives to Curl on Windows, whatever those might be.
Thank you for your help in advance.
If your goal is to drive Internet Explorer, the most painless and robust way to accomplish this is probably to use Selenium WebDriver.
Many folks successfully use it on Windows via the Selenium C# bindings (although my favorite way to use Selenium on Windows is via the Selenium Python bindings).
If you're keen on using PowerShell, it should be possible to use Selenium from PS, although there are no official PS bindings from what I can tell.

How to open the default browser from Eclipse user space on any platform?

I want to select a term a term in the editor and pass it to an external URL using the default browser.
I found a few helpful hints on SO, especially this recipe for invoking IE from an external tool. But I would like to open the default browser, not necessarily IE, and not necessarily in Windows.
I answered the question below with a Windows-specific recipe, but given that there are several recipes for launching the browser from the Java API, I figured there is also an user-visible variable for the browser, and I was just unable to find it. If you are able to find that, please answer the question. Or if you know that it is impossible to do so, please let me know. Thanks!
To open the default browser on Windows, use the start built-in command to cmd.exe with a URL. Below I have an example of the dialog you get when you go to Run, External Tools, External Tools Configurations... I compressed the left pane because it contains proprietary information from my company, but the part that matters is in the right pane.
Cygwin has a similar mechanism. You would not need the /c and start arguments. Instead you would use ${system_path:cygstart.exe} for Location and only the url in Arguments. From my Ubuntu command-line, I can also run xdg-open in place of cygstart.exe to open the default browser. But I couldn't find a way that works the same in all platforms, although it seems to exist at the Eclipse API level.
On Mac you can use External tools and set the location to "/usr/bin/open" and in the arguments specify the path to the file you want to open:
Since the file is an HTML page, the default browser should open with it.