如果我正确理解了此代码,则该代码应捕获活动窗口并保持其焦点。concentr.exe是进程名称。如何根据进程名称使窗口聚焦?
Add-Type @"
using System;
using System.Runtime.InteropServices;
public class UserWindows {
[DllImport("user32.dll")]
public static extern IntPtr GetForegroundWindow();
}
"@
try {
$ActiveHandle = [UserWindows]::GetForegroundWindow()
$Process = Get-Process | ? {$_.MainWindowHandle -eq $activeHandle}
$Process | Select ProcessName, @{Name="concentr.exe";Expression= {($_.MainWindowTitle)}}
} catch {
Write-Error "Failed to get active Window details. More Info: $_"
}
Run Code Online (Sandbox Code Playgroud)
我也尝试过
param([string] $proc="Citrix Connection Manager", [string]$adm)
cls
Add-Type @"
using System;
using System.Runtime.InteropServices;
public class WinAp {
[DllImport("user32.dll")]
[return: MarshalAs(UnmanagedType.Bool)]
public static extern bool SetForegroundWindow(IntPtr hWnd);
[DllImport("user32.dll")]
[return: …Run Code Online (Sandbox Code Playgroud) 我正在尝试编写一个脚本,将多个 Excel 文件转换为 PDF 文件。我在网上找到了一个有效的链接。
\n$path = Read-Host -Prompt \'Input Directory Path and Press Enter\'\n$xlFixedFormat = \xe2\x80\x9cMicrosoft.Office.Interop.Excel.xlFixedFormatType\xe2\x80\x9d -as [type]\n$excelFiles = Get-ChildItem -Path $path -include *.xls, *.xlsx -recurse\n$objExcel = New-Object -ComObject excel.application\n$objExcel.visible = $false\nforeach($wb in $excelFiles)\n{\n $filepath = Join-Path -Path $path -ChildPath ($wb.BaseName + \xe2\x80\x9c.pdf\xe2\x80\x9d)\n $workbook = $objExcel.workbooks.open($wb.fullname, 3)\n $workbook.Saved = $true\n \xe2\x80\x9csaving $filepath\xe2\x80\x9d\n $workbook.ExportAsFixedFormat($xlFixedFormat::xlTypePDF, $filepath)\n $objExcel.Workbooks.close()\n}\n$objExcel.Quit()\nRun Code Online (Sandbox Code Playgroud)\n如果我将其复制并粘贴到 PowerShell 窗口中,程序将按预期运行。但是,当我尝试创建运行该程序的快捷方式时,出现了几个错误(文件保存为 .ps1)。
\n这是我在设置快捷方式时所做的路径和参数:
\n$path = Read-Host -Prompt \'Input Directory Path and Press Enter\'\n$xlFixedFormat = \xe2\x80\x9cMicrosoft.Office.Interop.Excel.xlFixedFormatType\xe2\x80\x9d -as …Run Code Online (Sandbox Code Playgroud)