Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/powershell/12.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
在屏幕位置打开powershell窗口_Powershell - Fatal编程技术网

在屏幕位置打开powershell窗口

在屏幕位置打开powershell窗口,powershell,Powershell,我正在尝试打开一个新的powershell窗口,我可以做得很好。但是我不能在屏幕上设置它的位置 我正在使用以下命令: 但我得到了以下错误: 方法调用失败,因为[Window]不包含名为“MoveWindow”的方法 以下是powershell脚本: Function Set-Window { <# .SYNOPSIS Sets the window size (height,width) and coordinates (x,y) of a pro

我正在尝试打开一个新的powershell窗口,我可以做得很好。但是我不能在屏幕上设置它的位置

我正在使用以下命令:

但我得到了以下错误: 方法调用失败,因为[Window]不包含名为“MoveWindow”的方法

以下是powershell脚本:

Function Set-Window {
<#
    .SYNOPSIS
        Sets the window size (height,width) and coordinates (x,y) of
        a process window.

    .DESCRIPTION
        Sets the window size (height,width) and coordinates (x,y) of
        a process window.

    .PARAMETER ProcessName
        Name of the process to determine the window characteristics

    .PARAMETER X
        Set the position of the window in pixels from the top.

    .PARAMETER Y
        Set the position of the window in pixels from the left.

    .PARAMETER Width
        Set the width of the window.

    .PARAMETER Height
        Set the height of the window.

    .PARAMETER Passthru
        Display the output object of the window.

    .NOTES
        Name: Set-Window
        Author: Boe Prox
        Version History
            1.0//Boe Prox - 11/24/2015
                - Initial build
            1.1//JosefZ (https://superuser.com/users/376602/josefz) - 19.05.2018
                - treats more process instances of supplied process name properly

    .OUTPUT
        System.Automation.WindowInfo

    .EXAMPLE
        Get-Process powershell | Set-Window -X 2040 -Y 142 -Passthru

        ProcessName Size     TopLeft  BottomRight
        ----------- ----     -------  -----------
        powershell  1262,642 2040,142 3302,784   

        Description
        -----------
        Set the coordinates on the window for the process PowerShell.exe

#>
[OutputType('System.Automation.WindowInfo')]
[cmdletbinding()]
Param (
    [parameter(ValueFromPipelineByPropertyName=$True)]
    $ProcessName,
    [int]$X,
    [int]$Y,
    [int]$Width,
    [int]$Height,
    [switch]$Passthru
)
Begin {
    Try{
        [void][Window]
    } Catch {
    Add-Type @"
          using System;
          using System.Runtime.InteropServices;
          public class Window {
            [DllImport("user32.dll")]
            [return: MarshalAs(UnmanagedType.Bool)]
            public static extern bool GetWindowRect(IntPtr hWnd, out RECT lpRect);

            [DllImport("user32.dll")]
            [return: MarshalAs(UnmanagedType.Bool)]
            public extern static bool MoveWindow(IntPtr handle, int x, int y, int width, int height, bool redraw);
          }
          public struct RECT
          {
            public int Left;        // x position of upper-left corner
            public int Top;         // y position of upper-left corner
            public int Right;       // x position of lower-right corner
            public int Bottom;      // y position of lower-right corner
          }
"@
    }
}
Process {
    $Rectangle = New-Object RECT
    $Handles = (Get-Process -Name $ProcessName).MainWindowHandle   ### 1.1//JosefZ
    foreach ( $Handle in $Handles ) {                              ### 1.1//JosefZ
        if ( $Handle -eq [System.IntPtr]::Zero ) { Continue }      ### 1.1//JosefZ
        $Return = [Window]::GetWindowRect($Handle,[ref]$Rectangle)
        If (-NOT $PSBoundParameters.ContainsKey('Width')) {            
            $Width = $Rectangle.Right - $Rectangle.Left            
        }
        If (-NOT $PSBoundParameters.ContainsKey('Height')) {
            $Height = $Rectangle.Bottom - $Rectangle.Top
        }
        If ($Return) {
            $Return = [Window]::MoveWindow($Handle, $x, $y, $Width, $Height,$True)
        }
        If ($PSBoundParameters.ContainsKey('Passthru')) {
            $Rectangle = New-Object RECT
            $Return = [Window]::GetWindowRect($Handle,[ref]$Rectangle)
            If ($Return) {
                $Height = $Rectangle.Bottom - $Rectangle.Top
                $Width = $Rectangle.Right - $Rectangle.Left
                $Size = New-Object System.Management.Automation.Host.Size -ArgumentList $Width, $Height
                $TopLeft = New-Object System.Management.Automation.Host.Coordinates -ArgumentList $Rectangle.Left, $Rectangle.Top
                $BottomRight = New-Object System.Management.Automation.Host.Coordinates -ArgumentList $Rectangle.Right, $Rectangle.Bottom
                If ($Rectangle.Top -lt 0 -AND $Rectangle.LEft -lt 0) {
                    Write-Warning "Window is minimized! Coordinates will not be accurate."
                }
                $Object = [pscustomobject]@{
                    ProcessName = $ProcessName
                    Size = $Size
                    TopLeft = $TopLeft
                    BottomRight = $BottomRight
                }
                $Object.PSTypeNames.insert(0,'System.Automation.WindowInfo')
                $Object            
            }
        }
    }
}
函数集窗口{
[OutputType('System.Automation.WindowInfo')]
[cmdletbinding()]
Param(
[参数(ValueFromPipelineByPropertyName=$True)]
$ProcessName,
[int]$X,
[int]$Y,
[int]$Width,
[int]$Height,
[交换机]$Passthru
)
开始{
尝试{
[作废][窗口]
}抓住{
添加类型@”
使用制度;
使用System.Runtime.InteropServices;
公共类窗口{
[DllImport(“user32.dll”)]
[返回:Marshallas(UnmanagedType.Bool)]
公共静态外部bool GetWindowRect(IntPtr hWnd,out RECT lpRect);
[DllImport(“user32.dll”)]
[返回:Marshallas(UnmanagedType.Bool)]
公共外部静态布尔移动窗口(IntPtr句柄、intx、inty、intwidth、intheight、布尔重绘);
}
公共结构矩形
{
public int Left;//左上角的x位置
public int Top;//左上角的y位置
public int Right;//右下角的x位置
public int Bottom;//右下角y位置
}
"@
}
}
过程{
$Rectangle=新对象RECT
$Handles=(获取进程-名称$ProcessName).MainWindowHandle####1.1//JosefZ
foreach($Handles中的Handle){1.1//JosefZ
if($Handle-eq[System.IntPtr]::Zero){Continue}###1.1//JosefZ
$Return=[Window]::GetWindowRect($Handle,[ref]$Rectangle)
If(-NOT$PSBoundParameters.ContainsKey('Width')){
$Width=$Rectangle.Right-$Rectangle.Left
}
If(-NOT$PSBoundParameters.ContainsKey('Height')){
$Height=$Rectangle.Bottom-$Rectangle.Top
}
如果($返回){
$Return=[Window]::MoveWindow($Handle、$x、$y、$Width、$Height、$True)
}
If($PSBoundParameters.ContainsKey('Passthru')){
$Rectangle=新对象RECT
$Return=[Window]::GetWindowRect($Handle,[ref]$Rectangle)
如果($返回){
$Height=$Rectangle.Bottom-$Rectangle.Top
$Width=$Rectangle.Right-$Rectangle.Left
$Size=新对象System.Management.Automation.Host.Size-参数列表$Width,$Height
$TopLeft=新对象System.Management.Automation.Host.Coordinates-ArgumentList$Rectangle.Left、$Rectangle.Top
$BottomRight=新对象System.Management.Automation.Host.Coordinates-参数列表$Rectangle.Right、$Rectangle.Bottom
如果($Rectangle.Top-lt 0-和$Rectangle.LEft-lt 0){
写入警告“窗口已最小化!坐标将不准确。”
}
$Object=[pscustomobject]@{
ProcessName=$ProcessName
大小=$Size
TopLeft=$TopLeft
BottomRight=$BottomRight
}
$Object.PSTypeNames.insert(0,'System.Automation.WindowInfo')
$Object
}
}
}
}
}


有人能帮忙吗?

你能用vbscript吗

看看这个答案


将cmd.exe替换为powershell.exe,并使用
cscript.exe script.vbs调用脚本。请编辑您的问题并在此处发布脚本。谢谢。一定要发布一个,而不是你的整个脚本。我们不会去其他地方(即使是另一个网站)调查你的问题。请在这里提供一个链接,并问一个特定的问题-不是。有趣的是,您链接的代码与您得到的特定错误有关。这篇文章甚至一字不差。所以你现在写的问题是,有人能在这里为我在其他地方问这个问题的答案吗?这显然不适合这个网站。肯·怀特。。。我通常会同意,但是我已经将脚本更改为声明的User32.dll->User32.dll,但是我得到了错误。所以我想我会寻求帮助,因为我想不出来