Reputation: 11
Update: settings the scheduled task to "run whether user is logged on or not" was the issue. when you do this it doesn't allow running a program that has a gui in an interactive manner ie it won't launch the GUI. Changing the setting to "run only when user is logged on" fixed the problem. In my case I have a user logged on but disconnected and this task needs to run to reset an application overnight to clear out issues. So this solved the problem.
I am trying to run this script as a scheduled task in a Windows 2012/2016 environment where the user running this is an administrator and the scheduled task is set to run as an admin. When I run the script in Powershell ISE or interactively the script kills the application and then relaunches it as expected but when I run the scheduled task it kills the application but does not launch it.
the transcript also looks nearly identical to when I run it in the ISE.
The action is "powershell -file scriptpath.ps1"
I'm sure I'm missing something simple here but I'm not sure what it is.
From the task scheduler.
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
**********************
From the 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
**********************
The script
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'."
}
Running it in the ISE, interactively. tried action powershell.exe instead of powershell.
Upvotes: 1
Views: 405
Reputation: 438208
Your intent was to launch a GUI application from a scheduled task:
This fundamentally only works if the task runs on the desktop (window station, session) of a currently logged on user.
That is, the scheduled task must be configured with the Run only when user is logged on
option selected in the Task Scheduler GUI (taskschd.msc
)
Run whether user is logged on or not
is selected, the task invariably runs invisibly, namely in the session in which all Windows services run (this hidden session is the one named services
and has ID 0
in qwinsta.exe
's output).The implications of selecting Run only when user is logged on
are:
The task runs visibly, unless the process itself chooses to launch hidden (or launches in an unobtrusive way, such as by adding only a notification-are icon); if the process creates a (non-hidden, non-minimized) window on startup, which invariably happens with console applications, that window will pop up.
You must configure the task to run as a real user (as opposed to a system account) or as a user group; in the latter case, the task will run if any member of that group happens to be logged on.
As the option name implies the task will not run at the scheduled time if the specified user / no user from that group happens to be logged on then.
If you choose the Users
group, then the task will run if any user happens to be logged on, but if your task requires elevation, i.e. running as administrator, it will only work for those users that are also in the Administrators
group, provided that you have also selected the Run with highest privileges
checkbox.
Per your feedback, in your case the administrator in whose context the task runs is logged on (but disconnected) overnight, so reconfiguring your task with the Run only when user is logged on
option solved your problem.
Upvotes: 0