I've been working on a script for to learn a few things (and to save
some gnashing of teeth when Firebox blows up).

I used this little function to bring a Firefox window to the front:
http://community.idera.com/powershell/powertips/b/tips/posts/bringing-window-in-the-foreground

I had it working for a couple of hours, but somehow I broke it, and I
can't figure out how I did that. Anyone have any ideas on what I
glitched?

Here's the script as it stands:

----------begin script----------
function Show-Process($Process, [Switch]$Maximize)
{
  $sig = '
    [DllImport("user32.dll")] public static extern bool
ShowWindowAsync(IntPtr hWnd, int nCmdShow);
    [DllImport("user32.dll")] public static extern int
SetForegroundWindow(IntPtr hwnd);
  '

  if ($Maximize) { $Mode = 3 } else { $Mode = 4 }
  $type = Add-Type -MemberDefinition $sig -Name WindowAPI -PassThru
  $hwnd = $process.MainWindowHandle
  $null = $type::ShowWindowAsync($hwnd, $Mode)
  $null = $type::SetForegroundWindow($hwnd)
}

# We're going to bring the lowest PID process with this name to the front
$Firefox = get-process -Name firefox | sort Id

# Let's see what we've harvested
$Firefox

# Bring the process to the front - give it focus
Show-Process -Process $Firefox[0].id
---------end script---------

Here's the output, which contains a set of errors I can't decipher, as
today my search-fu is weak.

---------begin output----------
PS C:\batchfiles> function Show-Process($Process, [Switch]$Maximize)
>> {
>>   $sig = '
>>     [DllImport("user32.dll")] public static extern bool 
>> ShowWindowAsync(IntPtr hWnd, int nCmdShow);
>>     [DllImport("user32.dll")] public static extern int 
>> SetForegroundWindow(IntPtr hwnd);
>>   '
>>
>>   if ($Maximize) { $Mode = 3 } else { $Mode = 4 }
>>   $type = Add-Type -MemberDefinition $sig -Name WindowAPI -PassThru
>>   $hwnd = $process.MainWindowHandle
>>   $null = $type::ShowWindowAsync($hwnd, $Mode)
>>   $null = $type::SetForegroundWindow($hwnd)
>> }
>>
PS C:\batchfiles> # We're going to bring this process to the front
PS C:\batchfiles> $Firefox = get-process -Name firefox | sort Id
PS C:\batchfiles>
PS C:\batchfiles> # Let's see what we've harvested
PS C:\batchfiles> $Firefox

Handles  NPM(K)    PM(K)      WS(K) VM(M)   CPU(s)     Id  SI ProcessName
-------  ------    -----      ----- -----   ------     --  -- -----------
    208      26    40604      58772   438     4.17   1480   1 firefox
    502      84   334348     333860   993    88.53   4236   1 firefox
    735     116   320392     376668  1036    41.20   5008   1 firefox


PS C:\batchfiles>
PS C:\batchfiles> # Bring the process to the front - give it focus
PS C:\batchfiles> Show-Process -Process $Firefox[0].id
Cannot convert argument "hWnd", with value: "", for "ShowWindowAsync"
to type "System.IntPtr": "Cannot convert null to
type "System.IntPtr"."
At line:10 char:3
+   $null = $type::ShowWindowAsync($hwnd, $Mode)
+   ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
    + CategoryInfo          : NotSpecified: (:) [], MethodException
    + FullyQualifiedErrorId : MethodArgumentConversionInvalidCastArgument

Cannot convert argument "hwnd", with value: "", for
"SetForegroundWindow" to type "System.IntPtr": "Cannot convert
null to type "System.IntPtr"."
At line:11 char:3
+   $null = $type::SetForegroundWindow($hwnd)
+   ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
    + CategoryInfo          : NotSpecified: (:) [], MethodException
    + FullyQualifiedErrorId : MethodArgumentConversionInvalidCastArgument
---------end output----------



Reply via email to