Convert a small PS script into a long line in a .BATch file

前端 未结 3 1275
逝去的感伤
逝去的感伤 2020-11-27 23:06

I have this PowerShell code that I got from the answer to this question; it show the location/dimensions of the cmd.exe window where the PS code run:

$Window         


        
3条回答
  •  北海茫月
    2020-11-27 23:47

    Double quote literals must be escaped as \"

    @echo off
    
    PowerShell^
      $WindowFunction,$RectangleStruct = Add-Type -MemberDefinition '^
      [DllImport(\"user32.dll\", SetLastError = true)]^
      [return: MarshalAs(UnmanagedType.Bool)]^
      public static extern bool GetWindowRect(IntPtr hWnd, ref RECT lpRect);^
      [StructLayout(LayoutKind.Sequential)]^
      public struct RECT^
      {^
         public int Left;^
         public int Top;^
         public int Right;^
         public int Bottom;^
      }^
      ' -Name \"type$([guid]::NewGuid() -replace '-')\" -PassThru;^
      $MyWindowHandle = (Get-Process -Id (^
        Get-WmiObject Win32_Process -Filter \"ProcessId=$PID\"^
      ).ParentProcessId).MainWindowHandle;^
      $WindowRect = New-Object -TypeName $RectangleStruct.FullName;^
      $null = $WindowFunction::GetWindowRect($MyWindowHandle,[ref]$WindowRect);^
      Write-Host $WindowRect.Left $WindowRect.Top $WindowRect.Right $WindowRect.Bottom
    

提交回复
热议问题