我已经找到了这两个脚本来设置PowerShell窗口状态,并且从其他用户的经验来看,这些脚本正在工作,碰巧我无法运行它
脚本1是
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'当我运行它时,我得到
Set-WindowState.ps1:36 char:58
+ $Win32ShowWindowAsync = Add-Type –memberDefinition @†如果我运行.\Set-WindowState.ps1
也试过了
Set-WindowStyle -Name Set-WindowStyle -Value 6但在这里我发现了一个错误
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是
<#
.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我得到
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时”
我现在明白了
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中,“是常规的”
发布于 2022-09-02 11:35:38
https://stackoverflow.com/questions/73581790
复制相似问题