计划任务执行PowerShell脚本无法启动GUI应用的排查与解决
计划任务执行PowerShell脚本无法启动GUI应用的问题与解决
在Windows Server 2012/2016环境中,使用管理员账号配置计划任务执行PowerShell脚本,脚本功能为:终止用户名匹配*-QB的QBWebConnector.exe进程,并重启该应用。
在PowerShell ISE或交互式运行脚本时,进程终止与应用重启均正常;但通过计划任务执行时,仅能终止进程,无法启动应用,且执行日志与ISE中几乎一致。计划任务的动作设置为powershell -file scriptpath.ps1。
问题根源与解决:
原计划任务设置为「不管用户是否登录都要运行」,该模式下系统不允许以交互方式启动带GUI的程序。将计划任务设置改为「仅当用户登录时运行」后问题解决,此场景下用户处于登录但断开状态,脚本可正常夜间运行以重置应用。
任务计划程序执行日志
Windows PowerShell transcript start Start time: 20230629124853 Username: RunAs User: Configuration Name: Machine: (Microsoft Windows NT 10.0.17763.0) Host Application: powershell.exe reset-qbwebconnector.ps1 Process ID: 20496 PSVersion: 5.1.17763.2090 PSEdition: Desktop PSCompatibleVersions: 1.0, 2.0, 3.0, 4.0, 5.0, 5.1.17763.2090 BuildVersion: 10.0.17763.2090 CLRVersion: 4.0.30319.42000 WSManStackVersion: 3.0 PSRemotingProtocolVersion: 2.3 SerializationVersion: 1.1.0.1 ********************** Transcript started, output file is ...0629124853.txt Launching file: C:\Program Files (x86)\Common Files\Intuit\QuickBooks\QBWebConnector\QBWebConnector.exe PS>$global:? True ********************** Windows PowerShell transcript end End time: 20230629124853 **********************
PowerShell ISE执行日志
************ Windows PowerShell transcript start Start time: 20230629130614 Username: RunAs User: Configuration Name: Machine: (Microsoft Windows NT 10.0.17763.0) Host Application: C:\Windows\system32\WindowsPowerShell\v1.0\PowerShell_ISE.exe Process ID: 14748 PSVersion: 5.1.17763.2090 PSEdition: Desktop PSCompatibleVersions: 1.0, 2.0, 3.0, 4.0, 5.0, 5.1.17763.2090 BuildVersion: 10.0.17763.2090 CLRVersion: 4.0.30319.42000 WSManStackVersion: 3.0 PSRemotingProtocolVersion: 2.3 SerializationVersion: 1.1.0.1 ********************** Launching file: C:\Program Files (x86)\Common Files\Intuit\QuickBooks\QBWebConnector\QBWebConnector.exe ********************** Windows PowerShell transcript end End time: 20230629130615 **********************
执行脚本
Start-Transcript -OutputDirectory d:\scripts #Find the qbwebconnector process for any users names *-QB and kill it. $processwebconnector = "QBWebConnector.exe" $processes = Get-WmiObject -Query "SELECT * FROM Win32_Process WHERE Name = '$processwebconnector'" foreach ($process in $processes) { $owner = $process.GetOwner() $username = $owner.User if ($username -like "*-QB") { Stop-Process -Id $process.ProcessId -Force } } #find the QBwebconnector program in the likely installation path and run it. $targetFileweb = "QBWebConnector.exe" $basePathweb = "C:\Program Files (x86)\Common Files\Intuit" # Function to recursively search for the target file function Find-FileRecursivelyweb($pathweb, $fileNameweb) { Get-ChildItem -Path $pathweb -Recurse -Filter $fileNameweb -ErrorAction SilentlyContinue | ForEach-Object { $_.FullName } } # Search for the target file recursively $foundFilesweb = Get-ChildItem -Path $basePathWeb -Recurse -Filter $targetFileWeb -File -ErrorAction SilentlyContinue | Select-Object -ExpandProperty FullName # Check if any files were found and run it if ($foundFilesweb) { foreach ($fileweb in $foundFilesweb) { Write-Host "Launching file: $fileweb" Start-Process -FilePath $fileweb } } else { Write-Host "File '$targetFileweb' not found in '$basePathweb'." }
内容的提问来源于stack exchange,提问作者Adam Abel
相关产品推荐
相关产品推荐

