Execute-processasuser script not running and full psadt script not continuing

Execute-ProcessAsUser -Path "$PSHOME\powershell.exe" -Parameters "-executionpolicy bypass -Command & { & `"$dirFiles\Printerremove.ps1`" `$LastExitCode}" -wait

My code creates the scheduled task and shows it was successfully executed. However, the script doesnt seem to actually run as the printers are not actually removed for the logged in user unless they are ad admin.

Then the full script just ends with this logging and the script doesnt continue to execute my next line of code under “installation”. Any idea what Im doing wrong? Am I missing someting to get the script to actually continue?

[Pre-Installation] :: Executing [C:\Windows\System32\schtasks.exe /delete /tn PSAppDeployToolkit-ExecuteAsUser-001 /f]...	Execute-Process	6/21/2024 1:55:10 PM	24256 (0x5EC0)

[Pre-Installation] :: Execution completed successfully with exit code [0].	Execute-Process	6/21/2024 1:55:11 PM	24256 (0x5EC0)

[Pre-Installation] :: Deleting file in path [C:\Windows\TEMP\PSAppDeployToolkit\PSAppDeployToolkit-ExecuteAsUser-001.xml]...	Remove-File	6/21/2024 1:55:11 PM	24256 (0x5EC0)

[Pre-Installation] :: Deleting folder [C:\Windows\TEMP\PSAppDeployToolkit\ExecuteAsUser] recursively...	Remove-Folder	6/21/2024 1:55:11 PM	24256 (0x5EC0)

As a follow up, the powershell scipt that is supposed to be execute with the scheduled task is just a simple get-printer task to find the printer that are installed under the users context and simply remove those printers so that the driver can be removed under the system context by the next part of the script. I noticed the scheduled task script seems to actually execute if the logged in user is a local admin, if its a standard user, it doesnt excute. This is being deployed via Intune in the system context.

If I execute deploy-application.exe just locally on the computer, even if the user is a standard user account, the user context printers do get removed.

Im not sure what I could be doing wrong.

Hi, you are referring to a script in $dirFiles. That location can’t be read by a standard user so that is probably why it’s not run. The examples for the function Execute-ProcessAsUser are all using a specific folder. You should probably copy the script to a folder that the current user have Read and Execute rights in and run it from there.

Note that there are several optional parameters to this function that you could use but unfortunately the documentation of these are not that good.

1 Like

Hi, I thought the script in the $dirFiles folder got copied to a temp folder by the main script and thrn used to create the scheduled task. So I thought it would use a folder that is readable by a standard user… Is that not accurate?

I think you are correct, at least it copies the RunHidden.vbs file:

Copy-Item -LiteralPath $appDeployRunHiddenVbsFile -Destination $executeAsUserTempPath -Force -ErrorAction ‘Stop’

It’s a complex function to troubleshoot, do you have any more information in the log file? According to the log info you provided the function only executes the Finally statement correctly so it probably breaks early.

1 Like