如何解决通过任务计划程序运行时,Windows Powershell GetForegroundWindow() 返回多个进程
我正在尝试使用任务调度程序定期运行 powershell 脚本。我想返回当前正在使用的进程的名称。我正在使用找到的代码 here 这样做。当我从 powershell 运行脚本时,这有效。返回的进程是预期的“powershell”。但是,当我使用任务调度程序运行它时,将返回所有当前正在运行的进程,而不仅仅是前台窗口进程。该脚本将进程详细信息保存在文件“process_file.out”中。我使用的脚本如下:
#set working directory to path of script
$scriptPath = split-path -parent $MyInvocation.MyCommand.Definition
Set-Location -Path $scriptPath -Passthru
Add-Type @"
using System;
using System.Runtime.InteropServices;
public class Tricks {
[DllImport("user32.dll")]
public static extern IntPtr GetForegroundWindow();
}
"@
$a = [tricks]::GetForegroundWindow()
$b=get-process | ? { $_.mainwindowhandle -eq $a } | Get-Unique
$c=$b.ProcessName
$c | Out-File -FilePath process_file.out
使用任务调度程序运行时输出文件“process_file.out”的第一部分示例如下:
acrotray
aesm_service
AGMService
AGSService
ApplicationFrameHost
armsvc
audiodg
backgroundTaskHost
chrome
cmd
...etc
任何建议将不胜感激。
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。