首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >Set-WindowState状态powershell脚本

Set-WindowState状态powershell脚本
EN

Stack Overflow用户
提问于 2022-09-02 11:21:36
回答 1查看 161关注 0票数 0

我已经找到了这两个脚本来设置PowerShell窗口状态,并且从其他用户的经验来看,这些脚本正在工作,碰巧我无法运行它

脚本1是

代码语言:javascript
复制
function Set-WindowState {
param(
    [Parameter()]
    [ValidateSet('FORCEMINIMIZE', 'HIDE', 'MAXIMIZE', 'MINIMIZE', 'RESTORE', 
                 'SHOW', 'SHOWDEFAULT', 'SHOWMAXIMIZED', 'SHOWMINIMIZED', 
                 'SHOWMINNOACTIVE', 'SHOWNA', 'SHOWNOACTIVATE', 'SHOWNORMAL')]
    [Alias('Style')]
    [String] $State = 'SHOW',
    
    [Parameter(ValueFromPipelineByPropertyname='True')]
    [System.IntPtr] $MainWindowHandle = (Get-Process –id $pid).MainWindowHandle,

    [Parameter()]
    [switch] $PassThru

)
BEGIN
{

$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
}
    
$Win32ShowWindowAsync = Add-Type –memberDefinition @” 
[DllImport("user32.dll")] 
public static extern bool ShowWindowAsync(IntPtr hWnd, int nCmdShow); 
“@ -name “Win32ShowWindowAsync” -namespace Win32Functions –passThru

}
PROCESS
{
    $Win32ShowWindowAsync::ShowWindowAsync($MainWindowHandle, $WindowStates[$State]) | Out-Null
    Write-Verbose ("Set Window State on '{0}' to '{1}' " -f $MainWindowHandle, $State)

    if ($PassThru)
    {
        Write-Output $MainWindowHandle
    }

}
END
{
}

}

Set-Alias -Name 'Set-WindowStyle' -Value 'Set-WindowState'

当我运行它时,我得到

代码语言:javascript
复制
Set-WindowState.ps1:36 char:58
+ $Win32ShowWindowAsync = Add-Type –memberDefinition @† 

如果我运行.\Set-WindowState.ps1

也试过了

代码语言:javascript
复制
Set-WindowStyle -Name Set-WindowStyle -Value 6

但在这里我发现了一个错误

代码语言:javascript
复制
Set-WindowStyle : The term 'Set-WindowStyle' is not recognized as the name of a cmdlet, function, script file, or
operable program. Check the spelling of the name, or if a path was included, verify that the path is correct and try
again.
At line:1 char:1
+ Set-WindowStyle -Name Set-WindowStyle -Value 6
+ ~~~~~~~~~~~~~~~
    + CategoryInfo          : ObjectNotFound: (Set-WindowStyle:String) [], CommandNotFoundException
    + FullyQualifiedErrorId : CommandNotFoundException

脚本2是

代码语言:javascript
复制
<#
.Synopsis
   Set the Window State of the Powershell Console Window
.DESCRIPTION
   Enables PowerShell scripts to set the visibility and various states of the PowerShell Console Window
.EXAMPLE
   Set-ConsoleWindowState -WindowState Minimized
.NOTES
   Author: Richard Bunker
   Version History:-
   1.0 - 08/06/2016 - Richard Bunker - Initial Version
.FUNCTIONALITY
   Enables PowerShell scripts to hide and set the various window states of the PowerShell console
#>

$DebugPreference="SilentlyContinue"

$script:showWindowAsync=Add-Type -MemberDefinition @"
[DllImport("user32.dll")] public static extern bool ShowWindowAsync(IntPtr hWnd, int nCmdShow);
"@ -Name "Win32ShowWindowAsync" -Namespace Win32Functions -PassThru

Add-Type @"
    using System;
    using System.Runtime.InteropServices;
    public class SetWindowToFront {
        [DllImport("user32.dll")]
        [return: MarshalAs(UnmanagedType.Bool)]
        public static extern bool SetForegroundWindow(IntPtr hWnd);
    }
"@

function Set-ConsoleWindowState {

    [CmdletBinding()]
    param(
        [Parameter(Mandatory=$true, Position=0)]
        [ValidateSet("Normal", "Maximized","Minimized","Default","Hidden","SetWindowToFront")]
        [String]
        $WindowState="Normal"
    )

    switch ($WindowState)
    {
        'Normal' {$null = $script:showWindowAsync::ShowWindowAsync((Get-Process -id $pid).MainWindowHandle, 1)}
        'Maximized' {$null = $script:showWindowAsync::ShowWindowAsync((Get-Process -id $pid).MainWindowHandle, 3)}
        'Minimized' {$null = $script:showWindowAsync::ShowWindowAsync((Get-Process -id $pid).MainWindowHandle, 2)}
        'Default' {$script:showWindowAsync::ShowWindowAsync((Get-Process -id $pid).MainWindowHandle, 10)}
        'Hidden' {$null = $script:showWindowAsync::ShowWindowAsync((Get-Process -id $pid).MainWindowHandle, 0)}
        'SetWindowToFront' {[void][SetWindowToFront]::SetForegroundWindow((Get-Process -id $pid).MainWindowHandle)}
    }

}

# Export Module Functions:
Export-ModuleMember -Function Set-ConsoleWindowState

运行.\SetConsoleWindowState.psm1 -WindowState Minimized我得到

代码语言:javascript
复制
Set-ConsoleWindowState : The term 'Set-ConsoleWindowState' is not recognized as the name of a cmdlet, function, script
file, or operable program. Check the spelling of the name, or if a path was included, verify that the path is correct
and try again.
At line:1 char:1
+ Set-ConsoleWindowState -WindowState Minimized
+ ~~~~~~~~~~~~~~~~~~~~~~
    + CategoryInfo          : ObjectNotFound: (Set-ConsoleWindowState:String) [], CommandNotFoundException
    + FullyQualifiedErrorId : CommandNotFoundException

您能提供一些关于如何运行这些脚本的指导吗?

谢谢

编辑:

在脚本1中,我替换了“和运行.\Set-WindowState.ps1时”

我现在明白了

代码语言:javascript
复制
At C:\Desktop\Folder\Set-WindowState.ps1:36 char:54
+ $Win32ShowWindowAsync = Add-Type –memberDefinition @"
+                                                      ~~
The string is missing the terminator: "@.
At C:\Desktop\Folder\Set-WindowState.ps1:59 char:59
+ Set-Alias -Name 'Set-WindowStyle' -Value 'Set-WindowState'
+                                                           ~
Missing closing ')' in expression.
At C:\Desktop\Folder\Set-WindowState.ps1:59 char:59
+ Set-Alias -Name 'Set-WindowStyle' -Value 'Set-WindowState'
+                                                           ~
Missing ')' in function parameter list.
At C:\Desktop\Folder\Set-WindowState.ps1:1 char:26
+ function Set-WindowState {
+                          ~
Missing closing '}' in statement block or type definition.
    + CategoryInfo          : ParserError: (:) [], ParseException
    + FullyQualifiedErrorId : TerminatorExpectedAtEndOfString

在脚本2中,“是常规的”

EN

回答 1

Stack Overflow用户

发布于 2022-09-02 11:35:38

对于第一个错误,将所有像这样的印刷引号替换为"。这是必要的,因为由Add-Type -TypeDefinition调用的Add-Type -TypeDefinition编译器使用的是更严格的报价规则而不是PowerShell。

关于第二个错误,".psm1“文件是一个不能直接调用的模块。首先使用Import-Module,然后调用模块中包含的函数:

代码语言:javascript
复制
Import-Module .\SetConsoleWindowState.psm1
Set-ConsoleWindowState -WindowState Minimized
票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/73581790

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档