PowerShell, Object = Start Process? - windows

Hi I would like to start the Internet Explorer without extensions and control it. (Navigate to other pages, click buttons etc.)
When I use the command: "Start iexplore.exe -ArgumentList -extoff" I have the IExplore without extensions, but no object. I need the object to navigate to different pages and click buttons.
"$ie = Start iexplore.exe -ArgumentList -extoff" is not possible with the Command "Start X"
The following code create a Com Object and all I want is possible without the "Extensions = off"
$ie = New-Object -ComObject InternetExplorer.Application
$ie.Navigate("http://www.stackoverflow.com")
$ie.Navigate("www.Navigate to a other Page.com")
$ie.Document.getElementById("ButtonID")|foreach{
$_.Click()
}

This is a real hack but you could tweak the COM startup commandline for IE to pass in the argument -extoff. The registry entry to start IE on my machine (with IE9) is:
HKEY_CLASSES_ROOT\CLSID{0002DF01-0000-0000-C000-000000000046}\LocalServer32
Note that you might need to override the regkey permissions to edit the value.

Related

windows - create shortcut to printer with powershell

There are various questions and answers on SO explaining how to create a shortcut to the printer queue of a printer using powershell. They all use a shortcut destination like this: C:\Windows\System32\rundll32.exe printui.dll,PrintUIEntry /n Printername.
However, the shortcuts created like this do not have the right-click options like Scan which are visible when right-clicking the printer on the printer & devices page in the control panel. Right-clicking the printer on the Devices and Printers page reveals a Create Shortcut option, which creates a shortcut which has the same right-click options as the original item. (Dragging the printer to the desired destination folder works too)
The properties pages of the shortcuts created a) with powershell b) with the Create shortcut option also look quite different:
Thus, my question is: How do I create a shortcut that is equivalent to the shortcut created when using the right-click option Create Shortcut on a printer on the devices and printers page with powershell?
The correct shortcut does not point to rundll32.exe, it points to the printer in the shell namespace. This target is an item id list, not a filesystem path.
I don't know the native way to do this in Powershell. With P/invoke it would be SHParseDisplayName + IShellLink::SetIDList.
You would probably want to go the reverse way first; on a correct link, get its id list and call SHGetNameFromIDList(...,SIGDN_DESKTOPABSOLUTEPARSING,...). The returned string would look something like ::{GUIDHERE}\::{ANOTHERGUID}\MyPrinterName.
Thanks to Anders for bringing me onto the right track.
You can achieve it like this:
Get a list of all devices and printers:
$shell = New-Object -ComObject Shell.Application
$devices_and_printers = $shell.namespace("::{26EE0668-A00A-44D7-9371-BEB064C98683}\2\::{A8A91A66-3A7D-4424-8D24-04E180695C7A}")
$devices_and_printers.items() | select name,path
Create shortcut:
$WshShell = New-Object -comObject WScript.Shell
$Shortcut = $WshShell.CreateShortcut("$env:userprofile\Desktop\My Printer.lnk")
$Shortcut.TargetPath = ($devices_and_printers.items() | where { $_.name -eq "My Printer Name" }).Path
$Shortcut.Save()

Viewing hidden Powershell/IE windows

I have a Powershell script that runs in the background and part of the script will use IE that is hidden. Sometimes this script gets hung up and I'd like to view the IE and Powershell windows to see where its stuck. I can't find a way to do this.
Sometimes Windows will give me a message if a script is running that a process is running and asks if I would like to view it and then I am able to view the windows then but I can't figure out away to do that without getting that message first.
I need to figure out where this script is getting stuck at. If I run it manually it works fine and works fine most times but every so often it gets stuck in the same place.
It's a Windows 10 machine that it's running on.
Open a powershell window and type in these commands to connect to your running internet explorer:
$windows = (New-Object -Com "Shell.Application").Windows()
$ie = $windows | Where-Object {$_.Name -eq "Internet Explorer"}
$ie.Visible = $true
That should connect you to the running IE instance and make the window visible again. If that doesn't work, I would check out what objects are in the $windows variable to see if it is even detecting your IE. If it doesn't detect it, you might need to make sure it is running with Administrator rights.
After finding that the powershell script is starting the IE object in the remote session, I would instead implement a scheduled task and trigger that task remotely so that it will run in session 1 instead of session 0.
More info on this issue with various other applications:
http://psappdeploytoolkit.com/forums/topic/session-0-ui/
https://github.com/PowerShell/Win32-OpenSSH/issues/998
https://serverfault.com/questions/690852/use-powershell-to-start-a-gui-program-on-a-remote-machine

Open some Windows dialogs directly

From my software I'd like to be able to access several Windows dialogs direcly. Basically shortcuts to some dialogs that I use from time to time and that are otherwise difficult to access.
One example would be the "Merge or delete network locations" dialog (screenshot).
Using Process Explorer I found out the following information about that dialog:
C:\Windows\System32\netprof.dll
C:\Windows\system32\DllHost.exe /Processid:{44C39C96-0167-478F-B68D-783294A2545D}
Unfortunately I can't figure out what to do with it.
For other dialogs/locations there are ways like this:
Trash:
%windir%\explorer.exe /n,::{645FF040-5081-101B-9F08-00AA002F954E}
Keyboard:
%systemroot%\system32\control.exe /name Microsoft.RegionalAndLanguageOptions /page /p:"keyboard"
But I can't figure out a general way to do this.
The programming language doesn't matter in this case. You're welcome to post a solution in any language or by the command prompt. Please use the mentioned dialog as an example, but I hope to find out a general solution. Thanks.
Not every dialog can be programmatically accessed as you suggest. Some dialogs are custom to the application that launches them, so you won't be able to easily access them.
I wasn't able to find a reference to the "Merge or delete network locations" dialog that you were primarily interested in.
For the ones that can be accessed, there are basically two types.
Dialogs that can be invoked from the regular Windows command line. As mentioned by kenny in the comments, this serverfault question captures a lot of the known commands.
Another approach would be to launch the dialog through powershell. Powershell can access and launch the .NET framework dialogs. This TechNet Scripting Guys article provides an example of launching the open file dialog through powershell.
The article is well worth reading, but this is the snippet that does the magic.
Function Get-FileName($initialDirectory)
{
[System.Reflection.Assembly]::LoadWithPartialName("System.windows.forms") | Out-Null
$OpenFileDialog = New-Object System.Windows.Forms.OpenFileDialog
$OpenFileDialog.initialDirectory = $initialDirectory
$OpenFileDialog.filter = "All files (*.*)| *.*"
$OpenFileDialog.ShowDialog() | Out-Null
$OpenFileDialog.filename
} #end function Get-FileName
# *** Entry Point to Script ***
Get-FileName -initialDirectory "c:\fso"
And just in case you wanted something besides the open file dialog, you would change the following line to the dialog you need. $OpenFileDialog = New-Object System.Windows.Forms.OpenFileDialog

Invoke 'Open' Dialog from Windows Desktop

Is there some way I can programmatically (in VBS) OR by using CMD/RUN open the 'Open' dialog that contains the places bar and a browser but without opening say notepad or MSpaint?
http://i.technet.microsoft.com/dynimg/IC354177.jpg
I'd like to use this on the desktop itself, it would be really cool if there was a DLL I can just use instead of having a VBS file but if not i'm sure its possible in VBS.
I'm busy searching where the actual open dialog box comes from, it should come from some DLL file somewhere.
I might even consider stopping the windows shell from opening all together and just using this open window as the shell on some computers.
Regards, Rocklore
What version of Windows are you on?
"UserAccounts.CommonDialog" was the way to do this in XP. But it no longer exists in Windows 7. You may be able to use some of the flags available for the BrowseForFolder() method to make it look like a file open dialog. See this page for an example.
XP Edit:
Here's an XP example using UserAccounts.CommonDialog.
With CreateObject("UserAccounts.CommonDialog")
.InitialDir = CreateObject("WScript.Shell").SpecialFolders("Desktop")
.Filter = "All Files|*.*"
' Show the dialog. If [Open] is clicked, save the name of the selected file...
If .ShowOpen Then strFile = .FileName
End With

How can I create a new instance of explorer.exe by Microsoft Visual C++?

When I use CreateProcess API, the result is successful, but there is no new instance of explorer.exe, and the old instance just open a folder.
So, How can I create a new instance of explorer.exe by Microsoft Visual C++ ?
Check Explorer.exe Command-Line Options for Windows XP out.
Quote:
The options that you can use with Explorer.exe are /n, /e, /root (plus an object), and /select (plus an object).
Option Function
/n Opens a new single-pane window for the default
selection. This is usually the root of the drive that
Windows is installed on. If the window is already
open, a duplicate opens.
/e Opens Windows Explorer in its default view.
/root, Opens a window view of the specified object.
/select, Opens a window view with the specified folder, file,
or program selected.
Examples
Example 1: Explorer /select,C:\TestDir\TestProg.exe
Opens a window view with TestProg selected.
Example 2: Explorer /e,/root,C:\TestDir\TestProg.exe
Opens Explorer with drive C expanded and TestProg selected.
Example 3: Explorer /root,\TestSvr\TestShare
Opens a window view of the specified share.
Example 4: Explorer /root,\TestSvr\TestShare,select,TestProg.exe
Opens a window view of the specified share with TestProg selected.
Process.Start method

Resources