Powershell New-Object fails when run by Windows Task scheduler

0

I'm pretty sure that this must be some kind of weird permissions problem, but I haven't been able to find anything that works.

Here's the problem:

I have a PowerShell script that performs an FTP transfer to a remote site. It uses the CuteFTP transfer object. The script is called by a one-line batch file. If I run the batch file from within a Windows command prompt, it runs perfectly. However, if the batch file is called from the Windows Task Scheduler then the PowerShell script fails when it attempts to create the CuteFTP object. In both cases, the batch file is being run using the Administrator account on the local server.

The relevant parts of things are:

Batch file:

powershell.exe -File "D:\FTPToHost.ps1"

The PowerShell script:

$oSite = New-Object -ComObject CuteFTPPro.TEConnection
$oSite.Protocol = 'FTP'
$oSite.Host = "99.999.9.999"
$oSite.Login = 'UserID'
$oSite.Password = 'MyPassword'

The Windows Task Scheduler runs this by the command:

D:\FTPToHost.cmd

The execution of the batch file works within the Task Scheduler, however the PowerShell script fails with the error:

New-Object : Retrieving the COM class factory for component with CLSID
{112EA537-7AB9-4E22-8BFB-7FD5FCB19849} failed due to the following error:
80080005 Server execution failed (Exception from HRESULT: 0x80080005
(CO_E_SERVER_EXEC_FAILURE)).
At D:\FTPToHost.ps1:6 char:10
+ $oSite = New-Object -ComObject CuteFTPPro.TEConnection
+          ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
    + CategoryInfo          : ResourceUnavailable: (:) [New-Object], COMException
    + FullyQualifiedErrorId : NoCOMClassIdentified,Microsoft.PowerShell.Commands.NewObjectCommand

To the best of what I can see, the problem looks like a failure to have the correct system environment established when PowerShell is being run by the Task Scheduler, so the ComObject is not being found or instantiated correctly. As I mentioned, if I simply open a command prompt and run the .CMD file from the command-line, everything works as expected.

System environment is Windows Server 2016. Any ideas would be appreciated.

Norm

powershell
batch-file
task
taskscheduler
comobject
asked on Stack Overflow Jan 28, 2019 by npowroz • edited Jan 28, 2019 by Ansgar Wiechers

1 Answer

0

I am new to PowerShell and I was running into a similar scenario with a different application, similar conditions as to scheduling the task, and same error message. In my case, the instantiation of the application

$oSite = New-Object -ComObject Application.Application

actually launched it but the PowerShell object was not bound to it (I still do not know why).

What I ended up doing was:

  1. Getting the process for the application (adding a check before this would greatly help).
  2. Binding a PowerShell object to the process.
  3. Setting the properties or calling the methods using the recently bound object.

The code I ended up using looked like this:

$Proc = Get-Process process-name -ErrorAction SilentlyContinue
if (-Not($Proc -eq $Null)) {
    $NewApplicationObject = [System.Runtime.InteropServices.Marshal]::GetActiveObject("Application.Application")
    $NewApplicationObject.Property = value
    $NewApplicationObject.Quit()
    $oSite = $null
}

I guess the original object could have been bound back using the GetActiveObject cmdlet (or whatever it is), but I did not try it.

I hope this works for you or anyone reading this.

answered on Stack Overflow Nov 5, 2020 by LarryV • edited Jan 22, 2021 by LarryV

User contributions licensed under CC BY-SA 3.0