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

5
  • 2
    I think all you gotta do is add the assembly. Like: Add-type -Assemblyname System.something.cuteftp before you create an object of that class. I don't have the correct assembly location coz I dont have it installed on my machine. Hence, I gave an arbitrary one. you should be able to find the right one from your machine.
    – Sid
    Jan 28, 2019 at 18:11
  • It was a good idea, but after trying various combinations of parameters it still didn't work. What's galling is that it works perfectly in the PowerShell ISE tool, so you think it will work in the target environment. It also works perfectly when you do it in VBS, which was where I ported it from. I was running a combination of PowerShell and VBS to do the task, and wanted to have a pure PowerShell version. The PowerShell documentation even says that the VBS CreateObject function translates directly to New-Object. Obviously, it doesn't. I guess I'll have to go back to the PS/VBS mashup.
    – npowroz
    Jan 29, 2019 at 4:18
  • 1
    Powershell ISE v3 and above automatically loads the assembly when a class from the assembly is called. I think you will probably be able to recreate the issue from a Powershell console as it does not load assemblies unless specified.
    – Sid
    Jan 29, 2019 at 5:39
  • Any particular reason you're using CuteFTP? There's several ways to use FTP in Powershell without that dependency. Jan 29, 2019 at 9:05
  • I use CuteFTP as my main FTP client and it came with a sample VBScript to do most of what I needed. In my first implementation, I simply extended the script for the FTP transfer, and called it from PowerShell. I used PowerShell to deal with some file preparation before the transfer. I thought it would be cleaner to do it all in PowerShell, so I converted the VBScript. That's when my trouble started. I'm not a PowerShell guru, but I'll do some research to see if its own FTP capabilities could solve my problem.
    – npowroz
    Jan 29, 2019 at 18:50

1 Answer 1

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.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.