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
Add-type -Assemblyname System.something.cuteftpbefore 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