0

I think am close to the solution, or in the general ball park, but the way am calling the powershell might be part of the issue am calling the ps1 from a batch file, to

I want to have the user click a button and then have powershell get the url of the active window (EXPLORER.exe) - i did try to but the start/sleep command to check in the ISE but could get it to work :( could you help be out ?

Thank You

Set-ExecutionPolicy RemoteSigned
Param(
)
Add-Type @"
using System;
using System.Runtime.InteropServices;
public class UserWindows {
[DllImport("user32.dll")]
public static extern IntPtr GetForegroundWindow();
}
"@
try {
$ActiveHandle = [UserWindows]::GetForegroundWindow()
$Process | Select LocationURL| select -ExpandProperty LocationURL -First 1 | 
Tee-Object -Variable "dirvar"
} catch {
Write-Error "Failed to get active Window details. More Info: $_"
}
$dirvar1 = "$dirvar" -replace 'file:///', '' 
echo "$dirvar1"
Start-Process "Z:\30_Sysadmin\ADB_LOADER_DIR\ADBDIR.bat" "$dirvar1"

1 Answer 1

0

You are going about it in a difficult way. There are helper functions. This lists all shell windows (Internet Explorer and Explorer windows). The object is an Internet Explorer object. See https://msdn.microsoft.com/en-us/library/aa752084(v=vs.85).aspx.

This is VBScript but COM calls are identical across all languages.

Set objShell = CreateObject("Shell.Application")
Set AllWindows = objShell.Windows
For Each window in AllWindows
    window.Navigate "c:\"
    msgbox window.locationURL
Next
3
  • i don't want to get all the windows but the active one eg. not the first one just the first one opened, thats where am having an issue
    – Knotwood V
    Jan 10, 2018 at 17:25
  • You go through all windows to find the one you want. This is basic programming skills.
    – ACatInLove
    Jan 10, 2018 at 18:16
  • No but that is a core technique of processing lists. You can't get the first one as Windows doesn't know which is the first one. One common technique is to do a series of linear steps, next is to process lists, then to walk trees.
    – ACatInLove
    Jan 15, 2018 at 10:06

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.