我创建了一个计划任务,使用任务计划程序打开 MS Edge,并希望将其最小化打开。我尝试了参数:/background、/min,但似乎不起作用。Edge 仍然以最大化打开。请指导我将其最小化打开。
非常感谢您的帮助。
答案1
Edge 似乎忽略了-WindowStyle
启动期间传递给它的任何 eg(第二个实例有效,但第一个实例无效)。
因此,您可以做的是启动窗口,然后在启动后将其最小化:
start microsoft-edge:
Sleep 1
(Get-Process -Name msedge).MainWindowHandle | foreach { Set-WindowStyle MINIMIZE $_ }
将以上内容放入.ps1
文件中并从任务计划程序运行该文件。
如果你没有这个Set-WindowStyle
函数,你可以将下面的函数粘贴到脚本的顶部(源代码取自这里)
function Set-WindowStyle {
<#
.SYNOPSIS
To control the behavior of a window
.DESCRIPTION
To control the behavior of a window
.PARAMETER Style
Describe parameter -Style.
.PARAMETER MainWindowHandle
Describe parameter -MainWindowHandle.
.EXAMPLE
(Get-Process -Name notepad).MainWindowHandle | foreach { Set-WindowStyle MAXIMIZE $_ }
#>
[CmdletBinding(ConfirmImpact='Low')]
[Diagnostics.CodeAnalysis.SuppressMessageAttribute('PSUseShouldProcessForStateChangingFunctions','')]
param(
[ValidateSet('FORCEMINIMIZE', 'HIDE', 'MAXIMIZE', 'MINIMIZE', 'RESTORE',
'SHOW', 'SHOWDEFAULT', 'SHOWMAXIMIZED', 'SHOWMINIMIZED',
'SHOWMINNOACTIVE', 'SHOWNA', 'SHOWNOACTIVATE', 'SHOWNORMAL')]
[string] $Style = 'SHOW',
$MainWindowHandle = (Get-Process -Id $pid).MainWindowHandle
)
begin {
Write-Verbose -Message "Starting [$($MyInvocation.Mycommand)]"
$WindowStates = @{
FORCEMINIMIZE = 11; HIDE = 0
MAXIMIZE = 3; MINIMIZE = 6
RESTORE = 9; SHOW = 5
SHOWDEFAULT = 10; SHOWMAXIMIZED = 3
SHOWMINIMIZED = 2; SHOWMINNOACTIVE = 7
SHOWNA = 8; SHOWNOACTIVATE = 4
SHOWNORMAL = 1
}
}
process {
Write-Verbose -Message ('Set Window Style {1} on handle {0}' -f $MainWindowHandle, $($WindowStates[$style]))
$Win32ShowWindowAsync = Add-Type -memberDefinition @'
[DllImport("user32.dll")]
public static extern bool ShowWindowAsync(IntPtr hWnd, int nCmdShow);
'@ -name 'Win32ShowWindowAsync' -namespace Win32Functions -passThru
$Win32ShowWindowAsync::ShowWindowAsync($MainWindowHandle, $WindowStates[$Style]) | Out-Null
}
end {
Write-Verbose -Message "Ending [$($MyInvocation.Mycommand)]"
}
}