Hi all,
Hi I’m new PowerShell but have enjoyed learning on this code.
I have put together a script that refreshes a internet explorer 11 (IE 11) every 15 seconds if it currently isn’t in use/in the foreground.
The problem I have is comparing the foreground processName (read into variable $check) and the string I have “iexplorer” (which is the processName for IE 11).
I currently have the code using Out-String but seems to not converting ProcessName to a string (variable $check).
add-type -AssemblyName System.Windows.Forms
###Variables to Adjust####
$timeoutSeconds = 15 #adjust to how many seconds it takes the login to expire...
$checkIncrement = 3 #adjust - how many seconds each time to check
$appName = "iexplore"
####End####
$ie = New-Object -ComObject "InternetExplorer.Application"
$ie.Navigate("https://www.powershell.org/")
$ie.Visible = $true
#Write-Output $appName
start-sleep -Milliseconds 300
$code = @'
[DllImport("user32.dll")]
public static extern IntPtr GetForegroundWindow();
'@
Add-Type $code -Name Utils -Namespace Win32
do
{
$hwnd = [Win32.Utils]::GetForegroundWindow()
#Get Active Window
$check = Get-Process | Where-Object { $_.mainWindowHandle -eq $hwnd } | Select-Object processName | Out-String
$timer = 0
###Check which window is "active" (which is foreground window)
while ($check -eq $appName)
{
start-sleep -Seconds $checkIncrement
#increment timer
$timer +=1
#update active window.
$check = Get-Process |
Where-Object { $_.mainWindowHandle -eq $hwnd } |
Select-Object processName #, MainWindowTItle, MainWindowHandle | Out-String
#check timeout
Write-Output $timer
if ($timer > $timeoutSeconds)
{
$ie.Refresh()
}
}
sleep -Seconds $checkIncrement
}
While ($i -ne 0)
Thanks in advanced for your help!