iNNOKENTIY21
Silver Member | Редактировать | Профиль | Сообщение | Цитировать | Сообщить модератору
Код: #Кодировка файла: UTF-8 with BOM # Запускает браузер установленный по умолчанию, если уже запущен, активирует окно на передний план. <# https://docs.microsoft.com/ru-ru/windows/win32/shell/default-programs #> <# $UserChoicePath = "Software\Microsoft\Windows\Shell\Associations\UrlAssociations\http\UserChoice" $UserChoiceProgId = [Microsoft.Win32.Registry]::CurrentUser.OpenSubKey($UserChoicePath).GetValue("ProgId") $ProgIdPath = "$UserChoiceProgId\shell\open\command" $SplitOptions = [System.StringSplitOptions]::RemoveEmptyEntries $Browser = [Microsoft.Win32.Registry]::ClassesRoot.OpenSubKey($ProgIdPath).GetValue($null).Split('"', $SplitOptions)[0] $Browser #> enum CmdShow { HIDE SHOWNORMAL SHOWMINIMIZED MAXIMIZE SHOWMAXIMIZED SHOWNOACTIVATE SHOW MINIMIZE SHOWMINNOACTIVE SHOWNA RESTORE SHOWDEFAULT FORCEMINIMIZE } function Set-WindowShow { param ( # Идентификатор окна [Parameter(ValueFromPipeline, ValueFromPipelineByPropertyName)] [ValidateScript( { $_ -ne 0 } )] [Alias('MainWindowHandle')] [System.IntPtr] $hWnd, # Состояние окна [Parameter(Mandatory)] [CmdShow] $CmdShow ) if (!('WindowShow' -as [type])) { Add-Type -TypeDefinition @' using System; using System.Runtime.InteropServices; public class WindowShow { [DllImport("user32.dll")] [return: MarshalAs(UnmanagedType.Bool)] public static extern bool ShowWindowAsync(IntPtr hWnd, int nCmdShow); } '@ } [WindowShow]::ShowWindowAsync($hWnd, $CmdShow) } function Set-WindowForeground { param ( # Идентификатор окна [Parameter(ValueFromPipeline, ValueFromPipelineByPropertyName)] [ValidateScript( { $_ -ne 0 } )] [Alias('MainWindowHandle')] [System.IntPtr] $hWnd ) if (!('SetWindowForeground' -as [type])) { Add-Type -TypeDefinition @' using System; using System.Runtime.InteropServices; public class SetWindowForeground { [DllImport("user32.dll")] [return: MarshalAs(UnmanagedType.Bool)] public static extern bool SetForegroundWindow(IntPtr hWnd); } '@ } [SetWindowForeground]::SetForegroundWindow($hWnd) } [string] $Browser = "iexplore.exe" try { [string] $UserChoiceKeyPath = "Software\Microsoft\Windows\Shell\Associations\UrlAssociations\http\UserChoice" [Microsoft.Win32.RegistryKey] $UserChoiceKey = [Microsoft.Win32.Registry]::CurrentUser.OpenSubKey($UserChoiceKeyPath) [string] $UserChoiceProgId = $UserChoiceKey.GetValue("ProgId") [string] $ProgIdKeyPath = "$UserChoiceProgId\shell\open\command" [System.StringSplitOptions] $SplitOptions = [System.StringSplitOptions]::RemoveEmptyEntries [Microsoft.Win32.RegistryKey] $ProgIdKey = [Microsoft.Win32.Registry]::ClassesRoot.OpenSubKey($ProgIdKeyPath) [string] $ProgIdCommand = $ProgIdKey.GetValue($null).Split('"', $SplitOptions)[0] if ($ProgIdCommand) { $Browser = $ProgIdCommand } } finally { $UserChoiceKey.Close() $ProgIdKey.Close() } [string] $BrowserName = [System.IO.Path]::GetFileNameWithoutExtension($Browser) [System.Diagnostics.Process[]] $BrowserProcess = Get-Process -Name $BrowserName if ($BrowserProcess.Count -gt 0) { [System.IntPtr] $hWnd = ($BrowserProcess | Where-Object MainWindowHandle -ne 0).MainWindowHandle # эта фигня нужна, что бы Set-WindowForeground нормально отрабатывал, иначе через раз, два… Set-WindowShow -hWnd $hWnd -CmdShow MINIMIZE Set-WindowShow -hWnd $hWnd -CmdShow SHOWNA Set-WindowForeground -hWnd $hWnd } else { & $Browser } |
|