DCOM error when using Scheduled tasks

Hi,

I’m not sure if someone here can help me with this…
I’ve created a powershell script using VMware PowerCLI to create an Excel file with ESXi servers information.
It runs perfeclty when I run the script, but when I try to schedule a task it gives me the error below:

New-Object : Exception Retrieving the COM class factory for component with CLSID
CLSID {00024500-0000-0000-C000-000000000046} failed due to the following error: 80080005
HRESULT: 0x80080005 (CO_E_SERVER_EXEC_FAILURE)).

Additional information:
I used to have this scheduled task on another machine using Office 2010. The only thing I have to do there was to create the two “Desktop” folders under system32/config/systemprofile and syswow64/config/systemprofile.
Now I have a machine with Office 2013 (Click-to-run).

The funny things is that it worked yesterday when I installed Lync… After updating Lync to Skype for business it stopped.
Tried adding Network Service to Microsoft Excel Application security tab under DCOM configuration.
No joy… Any light? =D

Let’s concentrate on the code that is having issues. Can you provide the code that is creating the scheduled task? Also tell us if you only run the scheduled task code on the servers, can you reproduce the error?

Scheduled Tasks default to running under System, and it’s likely that the Office COM object doesn’t like running that user context. It’s probably looking for a user profile and SYSTEM doesn’t have one. And the difference from your old machine is probably that Office 2010 created a default user profile when it installed or something.

That’s my best guess. You seem to have a lot happening on that machine. And frankly, the idea of having Office installed on a server gives me shivers.

As a suggestion - please don’t think I’m crazy - have you considered using an actual database to store this information, rather than a spreadsheet? SQL Server Express is free, is designed to run as a service component, and is actually a lot easier to mess with than Excel. If you want reports from it, SQL Server Reporting Services is included with the big distribution of Express, and can generate some pretty mighty reports. We have a free ebook that covers some of the basics (Resources menu here on the site).

But this business of using Excel as a database and reporting tool is something we admins need to talk away from.

Rob Simmers and Don Jones,

First, thanks for your time once again!

I’m not running this on a server =p. I’m trying to run it on my workstation with “Windows 7 Professional” and “Office 2013 Home and business (Click-to-run)”.
The idea of having an Excel file is not to use it as an important database. We only need some fast and updated information about our servers. That’s why the scheduled task only runs once in the day. Nothing really big deal, but we need to fill this file automatically.
About the profile, that’s the reason I’ve created those desktop folders on Windows\System32\config\systemprofile and Windows\SysWOW64\config\systemprofile.
Read about it on this link (last post):
http://stackoverflow.com/questions/22670344/powershell-script-cannot-access-a-file-when-run-as-a-scheduled-task
And yes, I’ve tested and it works on a “Windows 7 Professional” with “Office 2010 (not click-to-run version)”. We only have to create those two folders and it works.

Let me try to show you what I’m doing:

1. Have PowerShell 4.0 and VMware PowerCLI 6 installed
2. Created the script below [portuguese-brazilian]


$pwd = Get-Content C:\VMware\powerclicred | ConvertTo-SecureString
$cred = New-Object System.Management.Automation.PSCredential “user$”, $pwd
Connect-VIServer -Server server1, server2, server3, server4, server5 -Credential $cred

function HostInfo {
#$WorkBook01.Sheets.Item($ESXiServer.Name).Activate()
$TotalGB = $Null
$Datastores = Get-VMHost $ESXiServer.Name | Get-Datastore | Where {($.Name -notlike “VMD*”) -and ($.Name -notlike “BKP*”)}
foreach ($Datastore in $Datastores) {
$TotalGB += $Datastore.CapacityGB
}
($WorkBook01.Sheets.Item($ESXiServer.Name)).Cells.Item(7,2) = $ESXiServer.NumCpu
($WorkBook01.Sheets.Item($ESXiServer.Name)).Cells.Item(7,3) = ($ESXiServer | select @{N=“RAM”;E={[math]::Round($_.MemoryTotalGB,0)}}).RAM
($WorkBook01.Sheets.Item($ESXiServer.Name)).Cells.Item(7,4) = [math]::Round($TotalGB,0)
($WorkBook01.Sheets.Item($ESXiServer.Name)).Cells.Item(7,6) = "VMware “+($ESXiServer).Version+” build "+($ESXiServer).Build
($WorkBook01.Sheets.Item($ESXiServer.Name)).Cells.Item(7,7) = if ($ESXiServer.PowerState -eq 1){“Ligada”}else{“Desligada”}
}

function Recursos {
$ServidoresAtivos = Get-VM -Server $ESXiServer.Name | Where {$.PowerState -eq “PoweredOn”}
$CPUUtilizados = $Null
$RAMUtilizada = $Null
$TotalHD = $Null
foreach ($CPU in $ServidoresAtivos.NumCpu){
$CPUUtilizados += $CPU
}
foreach ($RAM in $ServidoresAtivos.MemoryGB){
$RAMUtilizada += $RAM
}
$ServidorRAM = ($ESXiServer | select @{N=“RAM”;E={[math]::Round($
.MemoryTotalGB,0)}}).RAM
$TotalCPU = $ESXiServer.NumCpu - $CPUUtilizados
$Datastores = Get-VMHost $ESXiServer.Name | Get-Datastore | Where {($.Name -notlike “VMD*”) -and ($.Name -notlike “BKP*”)}
foreach ($Datastore in $Datastores) {
$TotalFreeGB += $Datastore.FreeSpaceGB
}
($WorkBook01.Sheets.Item($ESXiServer.Name)).Cells.Item(8,2) = $TotalCPU
($WorkBook01.Sheets.Item($ESXiServer.Name)).Cells.Item(8,3) = [math]::Round(($ServidorRAM - $RAMUtilizada),0)
($WorkBook01.Sheets.Item($ESXiServer.Name)).Cells.Item(8,4) = [math]::Round($TotalFreeGB,0)
}

function ServerInfo {
foreach ($Servidor in $Servidores) {
#$WorkBook01.Sheets.Item($ESXiServer).Activate()
$TotalHD = $Null
$HardDisks = (Get-HardDisk $Servidor).CapacityGB
foreach ($HardDisk in $HardDisks) {
$TotalHD += $HardDisk
}
($WorkBook01.Sheets.Item($ESXiServer)).Cells.Item($Linha,1) = $Servidor.Name
($WorkBook01.Sheets.Item($ESXiServer)).Cells.Item($Linha,2) = $Servidor.NumCpu
($WorkBook01.Sheets.Item($ESXiServer)).Cells.Item($Linha,3) = [math]::Round($Servidor.MemoryGB)
($WorkBook01.Sheets.Item($ESXiServer)).Cells.Item($Linha,4) = [math]::Round($TotalHD,0)
($WorkBook01.Sheets.Item($ESXiServer)).Cells.Item($Linha,5) = (Get-Datastore -VM $Servidor.name).name
($WorkBook01.Sheets.Item($ESXiServer)).Cells.Item($Linha,6) = $Servidor.Guest.OSFullName
($WorkBook01.Sheets.Item($ESXiServer)).Cells.Item($Linha,7) = if ($Servidor.PowerState -eq 1){“Ligada”}else{“Desligada”}
($WorkBook01.Sheets.Item($ESXiServer)).Cells.Item($Linha,8) = $Servidor.Version.ToString()
($WorkBook01.Sheets.Item($ESXiServer)).Cells.Item($Linha,9) = ($Servidor.Guest.ExtensionData).ToolsStatus.ToString()
($WorkBook01.Sheets.Item($ESXiServer)).Cells.Item($Linha,10) = if (Get-VM $Servidor | Get-Snapshot){“Sim”}else{“Não”}
#($WorkBook01.Sheets.Item($ESXiServer)).Columns.Item(“A:I”).EntireColumn.AutoFit() | out-null
($WorkBook01.Sheets.Item($ESXiServer)).Columns.Item(“A:I”).HorizontalAlignment = -4108
$Linha = $Linha + 1
}
}

$ESXiServers = Get-VMHost | Sort Name

$Excel01 = New-Object -ComObject Excel.Application
$Excel01.Visible = $False
$Workbook01 = $Excel01.Workbooks.Open(“C:\VMware\Arquivos\ConfigVirtual.xlsx”)

for ($i = 1; $i -le ($ESXiServers.Count); $i++)
{
$WorkSheet = $WorkBook01.Sheets.Item($i)
#$WorkSheet.Activate()
While ($Worksheet.Cells.Item(9,1).Value() -NotLike $Null){
[void]$Worksheet.Cells.Item(9,1).EntireRow.Delete()
}
}

foreach ($ESXiServer in $ESXiServers) {
HostInfo
Recursos
}

foreach ($ESXiServer in $ESXiServers.Name) {
$Linha = 9
$Servidores = (Get-VM -Server $ESXiServer | Where {$_.Name -notlike “Unknown*”})
ServerInfo
}

$WorkBook01.Save()
#$WorkBook01.Close($True)
$Excel01.Quit()
[System.Runtime.Interopservices.Marshal]::ReleaseComObject($Excel01) | Out-Null
[System.Runtime.Interopservices.Marshal]::ReleaseComObject($Workbook01) | Out-Null
[System.Runtime.Interopservices.Marshal]::ReleaseComObject($Worksheet) | Out-Null
$Excel01 = $Null

3. This scheduled task
Run everyday at 5:00 a.m. this command: C:\VMware\AgConfigVirtual.bat

4. bat content
C:\Windows\System32\WindowsPowerShell\v1.0\powershell.exe -PSConsoleFile “C:\Program Files (x86)\VMware\Infrastructure\vSphere PowerCLI\vimaxx.psc1” -command “ampersand{C:\VMware\AgConfigVirtual.ps1}” > C:\VMware\Arquivos\ErrCmd.txt
PS: The PSconsole file is another one that the default because I had to remove the PSModules info from it to work.
PS2: Wrote ampersand because the special character was being erased of the final text.

5. Content of ErrCmd.txt file


Name Port User


server1 443 DOMAIN\user$
server2 443 DOMAIN\user$
server3 443 DOMAIN\user$
server4 443 DOMAIN\user$
server5 443 DOMAIN\user$

New-Object : Falha na recupera‡Æo de f brica de classes COM do componente com
CLSID {00024500-0000-0000-C000-000000000046} devido ao seguinte erro: 80080005
Falha na execu‡Æo do servidor (Exce‡Æo de HRESULT: 0x80080005
(CO_E_SERVER_EXEC_FAILURE)).
No C:\VMware\AgConfigVirtual.ps1:67 caractere:12

  • $Excel01 = New-Object -ComObject Excel.Application
  •        ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
    
    • CategoryInfo : ResourceUnavailable: (:slight_smile: [New-Object], COMExcept
      ion
    • FullyQualifiedErrorId : NoCOMClassIdentified,Microsoft.PowerShell.Comman
      ds.NewObjectCommand

A propriedade ‘Visible’ nÆo foi encontrada neste objeto. Verifique se a
propriedade existe e pode ser definida.
No C:\VMware\AgConfigVirtual.ps1:68 caractere:1

  • $Excel01.Visible = $False
  •   + CategoryInfo          : InvalidOperation: (:) [], RuntimeException
      + FullyQualifiedErrorId : PropertyNotFound
    

… etc etc …

I’m thinking on something about the click-to-run version of Office 2013 than is different that MSI installed version… The tricky thing is why it worked yesterday…
Every research I make it shows DCOM security settings (already done) and ASPNET on IIS (which I don’t have on my machine).
Sorry about the english. Trying to learn (english and powershell) =p

Well, all I guess I can do is share what I’d do.

Excel not working. Excel not a database. Could install SQL Server Express and have this done in 10 minutes, or continue fighting with crappy Excel COM object from the early 2000s.

So I’d just install SQL Express and use that. Or output an object that PowerShell could run through Export-CSV, which could be opened by Excel, if that’s the end goal. But Excel is always weird, and that’s why I almost never waste time trying to make it work.

Don Jones,

My problem now is that my vacations starts this week. And to change the whole script is not an option for me now… =/
But since it works running manually, I’ll let someone with this task =D
When I come back I’ll try your option!
Thanks again!