Aac*_*ini 3 windows powershell cmd batch-file
我有这个 PowerShell 代码,是从这个问题的答案中得到的;它显示了运行 PS 代码的 cmd.exe 窗口的位置/尺寸:
$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
Run Code Online (Sandbox Code Playgroud)
当我从命令行在 .ps1 脚本中运行此代码时,它可以正常工作:
C:\Users\Antonio\Documents\test> powershell Set-ExecutionPolicy -ExecutionPolicy
Unrestricted -Scope Process; .\test.ps1
26 -7 943 738
Run Code Online (Sandbox Code Playgroud)
我想将此代码插入到 .BATch 文件中,以便没有单独的 .ps1 文件,因此我必须在长行中编写相同的代码作为powershell命令的参数。但是,为了保持可读性,我想在 .bat 文件中使用单独的行,并用 Batch 继续符终止每一行^;这是我的第一次尝试:
@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
%End PowerShell%
Run Code Online (Sandbox Code Playgroud)
当我运行这个批处理文件时,报告了几个错误:
C:\Users\Antonio\Documents\test> test.bat
Add-Type : c:\Users\Antonio\AppData\Local\Temp\yhd4ckqv.0.cs(8) : El nombre
'user32' no existe en el contexto actual
c:\Users\Antonio\AppData\Local\Temp\yhd4ckqv.0.cs(7) : {
c:\Users\Antonio\AppData\Local\Temp\yhd4ckqv.0.cs(8) : >>>
[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; }
c:\Users\Antonio\AppData\Local\Temp\yhd4ckqv.0.cs(9) :
En línea: 1 Carácter: 36
+ $WindowFunction,$RectangleStruct = Add-Type -MemberDefinition '
[DllImport(user3 ...
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
~~~
+ CategoryInfo : InvalidData: (c:\Users\Antoni...contexto actual:
CompilerError) [Add-Type], Exception
+ FullyQualifiedErrorId : SOURCE_CODE_ERROR,Microsoft.PowerShell.Commands.
AddTypeCommand
Add-Type : No se puede agregar el tipo. Hubo errores de compilación.
En línea: 1 Carácter: 36
+ $WindowFunction,$RectangleStruct = Add-Type -MemberDefinition '
[DllImport(user3 ...
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
~~~
and a long et cetera....
Run Code Online (Sandbox Code Playgroud)
我试图将撇号传递给下面的行,通过引号更改撇号,反之亦然,消除了每行开头的额外空格和其他一些修改,但我找不到编写此代码的正确方法。我之前用同样的方式写了几个比这个大得多的 PS 代码段,完全没有问题。虽然我是一名经验丰富的程序员,但我对 PowerShell 还是新手,它的多种特性一直让我感到困惑......
在批处理文件中编写此 PS 代码的正确方法是什么?如果还包括对问题原因的简单解释,我将不胜感激...
双引号文字必须转义为 \"
@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
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
1194 次 |
| 最近记录: |