The issue occurs because Kaseya does not close the Powershell process that was run at the time the script is launched. The solution is to add code that closes the previous Powershell processes to the script.
Assuming that you are using Powershell 3.0, the following code needs to be added to the beginning of your script:
#get current process id (new session launched by Kaseya)
$powershellid = ([System.Diagnostics.Process]::GetCurrentProcess()).Id
#get all powershell processes running on the AppAssure core
$powershellprocesses = (Get-Process -name "*Powershell*").Id
#stop all powershell processes exccept the newly launched one (found above)
foreach($powershellprocess in $powershellprocesses){if($powershellprocess -ne $powershellid){stop-process -Id $powershellprocess -Force}}
This would stop all Powershell processes running on the core (except the one launched by Kaseya to run your script) and your script would run as expected.
Another solution which would be more elegant as it affects only the Powershell process launched by Kaseya to run the script but, which may or may not work properly (depending of circumstances) would be adding at the end of your script the following code:
#get current process id (new session launched by Kaseya)
$powershellid = ([System.Diagnostics.Process]::GetCurrentProcess()).Id
#stop the powershell process that was used to run your script
stop-process -Id $powershellid -Force
This approach, although less invasive, does not guarantee that the script would be executed (as other Poweshell processes potentially crippling it may be already running on the core) but, if the script is properly executed, there will be no lingering Powershell process remaining.