Run Powershell script as Scheduled task, that uses Excel COM object

What am I missing here..

 

I have  Powershell script that uses the Quest AD cmdlets to get computer information from AD and populate an Excel spreadsheet with the data.

The script works fine, so I created a batch file and started the script from there (which works fine as well). It populates the excel spreadsheet, saves and closes the file.

If I run the script as a scheduled task, I can see from the logging that it supposedly gets the computers from AD, and runs through them. But a file is never saved, I have tried to run the scheduled task with admin credentials.

What am I forgetting?

November 11th, 2010 12:21pm

Is Excel installed for the admin account you are using?
Free Windows Admin Tool Kit Click here and download it now
November 11th, 2010 1:17pm

Yes.. I have tried running it under my own domain admin credentials that I have logged on to the server with, and I can start Excel just fine.

 

November 11th, 2010 1:28pm

Are there any diagnostic logging options for Excel you could turn on?

Free Windows Admin Tool Kit Click here and download it now
November 11th, 2010 1:42pm

Hi,

I suggest we use Process Monitor to trace the process of the scheduled task:

Process Monitor
http://technet.microsoft.com/en-us/sysinternals/bb896645.aspx   

Download and run it. Click File menu, check Capture Events, try to run the task, when the error occurs, uncheck Capture Events again. Exported events to Logfile.PML and upload the file to Windows Live SkyDrive (http://www.skydrive.live.com/). If you would like other community member to analyze the report, you can paste the link here, if not, you can send the link to tfwst@microsoft.com (with this thread title or link in the email. Please don’t share documents with this address).

Thanks.

November 12th, 2010 11:47am

Can you post the script?

Are you using relative paths?  You'll need to make sure you are using full paths, e.g., c:\output.xls - or make sure the starting directory is set.

Free Windows Admin Tool Kit Click here and download it now
November 12th, 2010 2:32pm

I am using full paths, I have tried with both local paths and UNC paths.

I will try to see what I can get out of Process monitor. My bet is that Excel is prompting for something, since I can see that it does not respond to the quit application either.. So when the scheduled task has run, it leaves an Excel process running.

November 12th, 2010 8:38pm

Here is the script.

$Descriptions = @{"ST" = "Scheduled Tasks";
"LO" = "Logon After Reboot";
"SS" = "Services";
"CO" = "COM+ Objects";
"AP" = "Applications Pools"}



#$serverlist = Get-QADComputer -LdapFilter '(!(userAccountControl:1.2.840.113556.1.4.803:=2))' | where {$_.Osname -like "*server*"}
$serverlist = Get-QADComputer -IncludedProperties pwdLastSet -SizeLimit 0  | where {$_.Osname -like "*server*"} | where {$_.name -like "apof*"}

$erroractionpreference = "SilentlyContinue"
$a = New-Object -comobject Excel.Application
$a.visible = $false
$a.DisplayAlerts = $false
$a.AskToUpdateLinks = $false
$a.AlertBeforeOverwriting = $false
$a.FeatureInstall = "msoFeatureInstallNone"

$b = $a.Workbooks.Add()
$c = $b.Worksheets.Item(1)

$c.Cells.Item(1,1) = "Machine Name"
$c.Cells.Item(1,2) = "OS Name"
$c.Cells.Item(1,3) = "IP Address"
$c.Cells.Item(1,4) = "Ping Status"
$c.Cells.Item(1,5) = "Password last set"
$c.Cells.Item(1,6) = "Enabled/Disabled"
$c.Cells.Item(1,7) = "Physical/Virtual"
$c.Cells.Item(1,8) = "Description         "
$c.Cells.Item(1,9) = "Test/Prod  "
$c.Cells.Item(1,10) = "Patch  "
$d = $c.UsedRange
$d.Interior.ColorIndex = 19
$d.Font.ColorIndex = 11
$d.Font.Bold = $True

$intRow = 2


$colComputers = $serverlist
foreach ($strComputer in $colComputers)
{
$c.Cells.Item($intRow, 1) = $strComputer.Name




$ping = new-object System.Net.NetworkInformation.Ping
$Reply = $ping.send($strComputer.Name)

if ($Reply.status eq Success) 
#if (Test-Connection $strComputer.DnsName -Count 1 -Quiet ) 
{
 $machineType = Get-WmiObject -ComputerName $strComputer.Name -Class Win32_BIOS

 If ($strComputer.AccountIsDisabled) {$enab = "Disabled"} else {$enab = "Enabled"}
 if ($strComputer.pwdLastSet -le (Get-Date).AddDays(-90)) {$age = "Older than 90 Days" ; $fgColor = 3} else {$age = "Less than 90 days"; $fgColor = 0}

 if ($machineType.Serialnumber -like "*vmware*") {$type = "VMware"} 
 Elseif ($machineType.Version -like "*VRTUAL*") {$type = "Hyper-V"} 
 Elseif (!($machineType.Version)) {$type = "N/A"} 
 else {$type = "Physical"}
 $Usetype = "Prod"
 $Patch = "Late Patch"

[ARRAY]$DescriptionAR = ($strComputer).Description -split(",") | %{
	Switch ($_) 
		{
			"T" {$Usetype = "Test"}
			"S" {$Usetype = "Staging"}
			"EP" {$Patch = "Early Patch"}
			default {"$($Descriptions.get_item($_))"}

		}

}
[String]$Description = $DescriptionAR -join("`r`n")

$c.Cells.Item($intRow, 2) = $strComputer.OSName
$c.Cells.Item($intRow, 3) = $Reply.Address.ToString()
$c.Cells.Item($intRow, 4) = "Online"
$c.Cells.item($intRow, 5).Interior.ColorIndex = $fgColor
$c.Cells.Item($intRow, 5) = $age
$c.Cells.Item($intRow, 6) = $enab
$c.Cells.Item($intRow, 7) = $type
$c.Cells.Item($intRow, 8) = $Description
$c.Cells.Item($intRow, 9) = $Usetype
$c.Cells.Item($intRow, 10) = $Patch

$Reply = ""
$Description = ""
$intRow = $intRow + 1

}
else 
{
 #$machineType = Get-WmiObject -ComputerName $strComputer.Name -Class Win32_BIOS
$type = "N/A"
 If ($strComputer.AccountIsDisabled) {$enab = "Disabled"} else {$enab = "Enabled"}
 if ($strComputer.pwdLastSet -le (Get-Date).AddDays(-90)) {$age = "Older than 90 Days" ; $fgColor = 3} else {$age = "Less than 90 days"; $fgColor = 0}

 $Usetype = "Prod"
 $Patch = "Late Patch"
 
[ARRAY]$DescriptionAR = ($strComputer).Description -split(",") | %{
	Switch ($_) 
		{
			"T" {$Usetype = "Test"}
			"S" {$Usetype = "Staging"}
			"EP" {$Patch = "Early Patch"}
			default {"$($Descriptions.get_item($_))"}

		}

}
[String]$Description = $DescriptionAR -join("`r`n")



$c.Cells.Item($intRow, 2) = $strComputer.OSName
$c.Cells.Item($intRow, 3) = $Reply.Address.ToString()
$c.Cells.Item($intRow, 4) = "Offline"
$c.Cells.item($intRow, 5).Interior.ColorIndex = $fgColor
$c.Cells.Item($intRow, 5) = $age
$c.Cells.Item($intRow, 6) = $enab
$c.Cells.Item($intRow, 7) = $type
$c.Cells.Item($intRow, 8) = $Description
$c.Cells.Item($intRow, 9) = $Usetype
$c.Cells.Item($intRow, 10) = $Patch

$Reply = ""
$Description = ""
$intRow = $intRow + 1  
}
$d.EntireColumn.AutoFit()
$c.Rows.Autofit()

}

$b.SaveAs("C:\Tools\ComputerList\ServerList.xlsx")

$a.Quit()

Free Windows Admin Tool Kit Click here and download it now
November 12th, 2010 9:00pm

First off, to potentially eliminate this as either a Task Scheduler problem or credentials, can you try to write some of this off simply to a text file, without trying to invoke Excel?

Not sure it will make any difference, but what OS and x86 or 64-bit?

November 14th, 2010 3:41pm

To solve the Excel not closing after the script finishes, add this to the begining of your script:

function Release-Ref ($ref) {
[System.Runtime.InteropServices.Marshal]::ReleaseComObject($ref)
[System.GC]::Collect()
[System.GC]::WaitForPendingFinalizers()
}

And this at the end:

Release-Ref $d
Release-Ref $c

Release-Ref $b
$a.Quit()
Release-Ref $a

You also might want to try making Excel visable and watching your script work with a subset of your servers, so that you can see what's happening.

 

OldDog

Free Windows Admin Tool Kit Click here and download it now
November 14th, 2010 5:22pm

I have been playing with for some time.. and  it seems as if there are just things you cannot do with the Excel COM object, unless you are running in interactive mode.

When using ProcessMon I can see that Excel does not query any paths or anything, even if I  add a default savepath, which it does when run interactively..

 

Olddog I can output data to a text file no problem, so it has to do with Excel..

So I guess I have to run the scheduled task as a logged in user.

November 16th, 2010 7:45pm

Hi,

Could the scheduled task run when configured to run as a logged in user? Do you need any other assistance? If there is anything we can do for you, please let us know.

Thanks.

Free Windows Admin Tool Kit Click here and download it now
November 19th, 2010 6:08am

I am just running this as a logged in user and it works... :)
December 5th, 2010 8:27pm

I am just running this as a logged in user and it works... :)
Free Windows Admin Tool Kit Click here and download it now
December 5th, 2010 8:27pm


Glad to hear the task is working fine. If you have more questions in the future, you’re welcomed to this forum.

Thanks.

December 7th, 2010 2:32am

Hello, I believe the issue is with a bug in the excel object that does not aloow it to be run in a task in windows task scheduler unless the user has checked that the user has to be logged on to run. If you have it to run without user logged on the excel part of the script will not run correctly. I have been testing and breaking my head with this. here is my code
$filenameB = "C:\Users\administrator\Documents\My Dropbox\2010-12-31_1510_SYSB_Last_Month_GroupName_Logins_GroupCalls.csv"
$filenameC = "C:\Users\administrator\Documents\My Dropbox\2010-12-31_1510_SYSC_Last_Month_GroupName_Logins_GroupCalls.csv"
$filenameE = "C:\Users\administrator\Documents\My Dropbox\2010-12-31_1510_SYSE_Last_Month_GroupName_Logins_GroupCalls.csv"



$dataB = Import-Csv $filenameB
$dataC = Import-Csv $filenameC
$dataE = Import-Csv $filenameE
#Creat a new excel Object
$Excel = New-Object -ComObject Excel.Application
#make it invisible
$Excel.visible = $False
#No alerts 
$Excel.DisplayAlerts = $False
#NOw we add a workbook
$file1 = "C:\Users\administrator\Documents\My Dropbox\System_BCE_2011_TalkGroups_Logins_GroupCalls - Copy.xlsx"
$Excel = $Excel.Workbooks.open($file1)
$SheetB = $Excel.Worksheets.Item(1)
$SheetC = $Excel.Worksheets.Item(2)
$SheetE = $Excel.Worksheets.Item(3)
$date = (Get-Date).AddMonths(-1)
$month = Get-Date($date) -f "MM-yyyy"
Switch ($month)
 {
  "12-2010" { $intRow = 5
          foreach ($line in $dataB)
          {
            #put the data into these cells and repeat down
            #write-host $line.UniqueID $line.GroupName $line.agency $line.Division $line.logins $line.group_calls
            $SheetB.Cells.Item($intRow,2) = $line.UniqueID
            $SheetB.Cells.Item($intRow,5) = $line.logins
            $SheetB.Cells.Item($intRow,6) = $line.group_calls
					  $intRow++
          }
					$intRow = 5
					foreach ($line in $dataC){
						$SheetC.Cells.Item($intRow,2) = $line.UniqueID
            $SheetC.Cells.Item($intRow,5) = $line.logins
            $SheetC.Cells.Item($intRow,6) = $line.group_calls
						$intRow++
											}
											$intRow = 5
					foreach ($line in $dataE) {
						$SheetE.Cells.Item($intRow,2) = $line.UniqueID
            $SheetE.Cells.Item($intRow,5) = $line.logins
            $SheetE.Cells.Item($intRow,6) = $line.group_calls
						$intRow++
											}
        }
        
       
 "01-2011" { $intRow = 5
          foreach ($line in $dataB)
          {
            #put the data into these cells and repeat down
            #write-host $line.UniqueID $line.GroupName $line.agency $line.Division $line.logins $line.group_calls
            $SheetB.Cells.Item($intRow,2) = $line.UniqueID
            $SheetB.Cells.Item($intRow,7) = $line.logins
            $SheetB.Cells.Item($intRow,8) = $line.group_calls
					$intRow++
					}
					$intRow = 5
						foreach ($line in $dataC)
          {	$SheetC.Cells.Item($intRow,2) = $line.UniqueID
            $SheetC.Cells.Item($intRow,7) = $line.logins
            $SheetC.Cells.Item($intRow,8) = $line.group_calls
						$intRow++
					}
					$intRow = 5
						foreach ($line in $dataE)
					{
						$SheetE.Cells.Item($intRow,2) = $line.UniqueID
            $SheetE.Cells.Item($intRow,7) = $line.logins
            $SheetE.Cells.Item($intRow,8) = $line.group_calls
            $intRow++
          } 
        }
  "02-2011" { $intRow = 5
          foreach ($line in $dataB)
          {
            #put the data into these cells and repeat down
            #write-host $line.UniqueID $line.GroupName $line.agency $line.Division $line.logins $line.group_calls
            $SheetB.Cells.Item($intRow,2) = $line.UniqueID
            $SheetB.Cells.Item($intRow,9) = $line.logins
            $SheetB.Cells.Item($intRow,10) = $line.group_calls
						$intRow++
          }
					$intRow = 5
					foreach ($line in $dataC)
          {
					$SheetC.Cells.Item($intRow,2) = $line.UniqueID
            $SheetC.Cells.Item($intRow,9) = $line.logins
            $SheetC.Cells.Item($intRow,10) = $line.group_calls
						$intRow++
					}
					$intRow = 5
					foreach ($line in $dataE)
          {
					$SheetE.Cells.Item($intRow,2) = $line.UniqueID
            $SheetE.Cells.Item($intRow,9) = $line.logins
            $SheetE.Cells.Item($intRow,10) = $line.group_calls
						$intRow++
					}
        }
  "03-2011" { $intRow = 5
          foreach ($line in $dataB)
          {
            #put the data into these cells and repeat down
            #write-host $line.UniqueID $line.GroupName $line.agency $line.Division $line.logins $line.group_calls
            $SheetB.Cells.Item($intRow,2) = $line.UniqueID
            $SheetB.Cells.Item($intRow,11) = $line.logins
            $SheetB.Cells.Item($intRow,12) = $line.group_calls
						$intRow++
          } 
					$intRow = 5
					foreach ($line in $dataC)
          {
						$SheetC.Cells.Item($intRow,2) = $line.UniqueID
            $SheetC.Cells.Item($intRow,11) = $line.logins
            $SheetC.Cells.Item($intRow,12) = $line.group_calls
						$intRow++
          } 
					$intRow = 5
					foreach ($line in $dataE)
          {
						$SheetE.Cells.Item($intRow,2) = $line.UniqueID
            $SheetE.Cells.Item($intRow,11) = $line.logins
            $SheetE.Cells.Item($intRow,12) = $line.group_calls
						$intRow++
          } 
        }
  "04-2011" { $intRow = 5
          foreach ($line in $dataB)
          {
            #put the data into these cells and repeat down
            #write-host $line.UniqueID $line.GroupName $line.agency $line.Division $line.logins $line.group_calls
            $SheetB.Cells.Item($intRow,2) = $line.UniqueID
            $SheetB.Cells.Item($intRow,13) = $line.logins
            $SheetB.Cells.Item($intRow,14) = $line.group_calls
            $intRow++
          } 
					$intRow = 5
					foreach ($line in $dataC)
          {
            #put the data into these cells and repeat down
            #write-host $line.UniqueID $line.GroupName $line.agency $line.Division $line.logins $line.group_calls
            $SheetC.Cells.Item($intRow,2) = $line.UniqueID
            $SheetC.Cells.Item($intRow,13) = $line.logins
            $SheetC.Cells.Item($intRow,14) = $line.group_calls
            $intRow++
          } 
					$intRow = 5
					foreach ($line in $dataE)
          {
            
            $SheetE.Cells.Item($intRow,2) = $line.UniqueID
            $SheetE.Cells.Item($intRow,13) = $line.logins
            $SheetE.Cells.Item($intRow,14) = $line.group_calls
            $intRow++
          } 
        }
  "05-2011" { $intRow = 5
          foreach ($line in $dataB)
          {
            #put the data into these cells and repeat down
            #write-host $line.UniqueID $line.GroupName $line.agency $line.Division $line.logins $line.group_calls
            $SheetB.Cells.Item($intRow,2) = $line.UniqueID
            $SheetB.Cells.Item($intRow,15) = $line.logins
            $SheetB.Cells.Item($intRow,16) = $line.group_calls
            $intRow++
          } 
					$intRow = 5
					 foreach ($line in $dataC)
          {
            #put the data into these cells and repeat down
            #write-host $line.UniqueID $line.GroupName $line.agency $line.Division $line.logins $line.group_calls
            $SheetC.Cells.Item($intRow,2) = $line.UniqueID
            $SheetC.Cells.Item($intRow,15) = $line.logins
            $SheetC.Cells.Item($intRow,16) = $line.group_calls
            $intRow++
          } 
					$intRow = 5
					 foreach ($line in $dataE)
          {
            #put the data into these cells and repeat down
            #write-host $line.UniqueID $line.GroupName $line.agency $line.Division $line.logins $line.group_calls
            $SheetE.Cells.Item($intRow,2) = $line.UniqueID
            $SheetE.Cells.Item($intRow,15) = $line.logins
            $SheetE.Cells.Item($intRow,16) = $line.group_calls
            $intRow++
          } 
        }
  "06-2011" { $intRow = 5
          foreach ($line in $dataB)
          {
            #put the data into these cells and repeat down
            #write-host $line.UniqueID $line.GroupName $line.agency $line.Division $line.logins $line.group_calls
            $SheetB.Cells.Item($intRow,2) = $line.UniqueID
            $SheetB.Cells.Item($intRow,17) = $line.logins
            $SheetB.Cells.Item($intRow,18) = $line.group_calls
            $intRow++
          } 
					$intRow = 5
          foreach ($line in $dataC)
          {
            #put the data into these cells and repeat down
            #write-host $line.UniqueID $line.GroupName $line.agency $line.Division $line.logins $line.group_calls
            $SheetC.Cells.Item($intRow,2) = $line.UniqueID
            $SheetC.Cells.Item($intRow,17) = $line.logins
            $SheetC.Cells.Item($intRow,18) = $line.group_calls
            $intRow++
          } 
					$intRow = 5
          foreach ($line in $dataE)
          {
            #put the data into these cells and repeat down
            #write-host $line.UniqueID $line.GroupName $line.agency $line.Division $line.logins $line.group_calls
            $SheetE.Cells.Item($intRow,2) = $line.UniqueID
            $SheetE.Cells.Item($intRow,17) = $line.logins
            $SheetE.Cells.Item($intRow,18) = $line.group_calls
            $intRow++
          } 
        }
  "07-2011" { $intRow = 5
          foreach ($line in $dataB)
          {
            #put the data into these cells and repeat down
            #write-host $line.UniqueID $line.GroupName $line.agency $line.Division $line.logins $line.group_calls
            $SheetB.Cells.Item($intRow,2) = $line.UniqueID
            $SheetB.Cells.Item($intRow,19) = $line.logins
            $SheetB.Cells.Item($intRow,20) = $line.group_calls
            $intRow++
          } 
					$intRow = 5
          foreach ($line in $dataC)
          {
            #put the data into these cells and repeat down
            #write-host $line.UniqueID $line.GroupName $line.agency $line.Division $line.logins $line.group_calls
            $SheetC.Cells.Item($intRow,2) = $line.UniqueID
            $SheetC.Cells.Item($intRow,19) = $line.logins
            $SheetC.Cells.Item($intRow,20) = $line.group_calls
            $intRow++
          } 
					$intRow = 5
          foreach ($line in $dataE)
          {
            #put the data into these cells and repeat down
            #write-host $line.UniqueID $line.GroupName $line.agency $line.Division $line.logins $line.group_calls
            $SheetE.Cells.Item($intRow,2) = $line.UniqueID
            $SheetE.Cells.Item($intRow,19) = $line.logins
            $SheetE.Cells.Item($intRow,20) = $line.group_calls
            $intRow++
          } 
        }
  "08-2011" { $intRow = 5
          foreach ($line in $dataB)
          {
            #put the data into these cells and repeat down
            #write-host $line.UniqueID $line.GroupName $line.agency $line.Division $line.logins $line.group_calls
            $SheetB.Cells.Item($intRow,2) = $line.UniqueID
            $SheetB.Cells.Item($intRow,21) = $line.logins
            $SheetB.Cells.Item($intRow,22) = $line.group_calls
            $intRow++
          } 
					$intRow = 5
          foreach ($line in $dataC)
          {
            #put the data into these cells and repeat down
            #write-host $line.UniqueID $line.GroupName $line.agency $line.Division $line.logins $line.group_calls
            $SheetC.Cells.Item($intRow,2) = $line.UniqueID
            $SheetC.Cells.Item($intRow,21) = $line.logins
            $SheetC.Cells.Item($intRow,22) = $line.group_calls
            $intRow++
          } 
					$intRow = 5
          foreach ($line in $dataE)
          {
            #put the data into these cells and repeat down
            #write-host $line.UniqueID $line.GroupName $line.agency $line.Division $line.logins $line.group_calls
            $SheetE.Cells.Item($intRow,2) = $line.UniqueID
            $SheetE.Cells.Item($intRow,21) = $line.logins
            $SheetE.Cells.Item($intRow,22) = $line.group_calls
            $intRow++
          } 
        }
  "09-2011" { $intRow = 5
          foreach ($line in $dataB)
          {
            #put the data into these cells and repeat down
            #write-host $line.UniqueID $line.GroupName $line.agency $line.Division $line.logins $line.group_calls
            $SheetB.Cells.Item($intRow,2) = $line.UniqueID
            $SheetB.Cells.Item($intRow,23) = $line.logins
            $SheetB.Cells.Item($intRow,24) = $line.group_calls
            $intRow++
          } 
					$intRow = 5
          foreach ($line in $dataC)
          {
            #put the data into these cells and repeat down
            #write-host $line.UniqueID $line.GroupName $line.agency $line.Division $line.logins $line.group_calls
            $SheetC.Cells.Item($intRow,2) = $line.UniqueID
            $SheetC.Cells.Item($intRow,23) = $line.logins
            $SheetC.Cells.Item($intRow,24) = $line.group_calls
            $intRow++
          } 
					$intRow = 5
          foreach ($line in $dataE)
          {
            #put the data into these cells and repeat down
            #write-host $line.UniqueID $line.GroupName $line.agency $line.Division $line.logins $line.group_calls
            $SheetE.Cells.Item($intRow,2) = $line.UniqueID
            $SheetE.Cells.Item($intRow,23) = $line.logins
            $SheetE.Cells.Item($intRow,24) = $line.group_calls
            $intRow++
          } 
        }
  "10-2011" { $intRow = 5
          foreach ($line in $dataB)
          {
            #put the data into these cells and repeat down
            #write-host $line.UniqueID $line.GroupName $line.agency $line.Division $line.logins $line.group_calls
            $SheetB.Cells.Item($intRow,2) = $line.UniqueID
            $SheetB.Cells.Item($intRow,25) = $line.logins
            $SheetB.Cells.Item($intRow,26) = $line.group_calls
            $intRow++
          } 
					$intRow = 5
          foreach ($line in $dataC)
          {
            #put the data into these cells and repeat down
            #write-host $line.UniqueID $line.GroupName $line.agency $line.Division $line.logins $line.group_calls
            $SheetC.Cells.Item($intRow,2) = $line.UniqueID
            $SheetC.Cells.Item($intRow,25) = $line.logins
            $SheetC.Cells.Item($intRow,26) = $line.group_calls
            $intRow++
          }
					$intRow = 5
          foreach ($line in $dataE)
          {
            #put the data into these cells and repeat down
            #write-host $line.UniqueID $line.GroupName $line.agency $line.Division $line.logins $line.group_calls
            $SheetE.Cells.Item($intRow,2) = $line.UniqueID
            $SheetE.Cells.Item($intRow,25) = $line.logins
            $SheetE.Cells.Item($intRow,26) = $line.group_calls
            $intRow++
          } 
        }
  "11-2010" { $intRow = 5
          foreach ($line in $dataB)
          {
            #put the data into these cells and repeat down
            #write-host $line.UniqueID $line.GroupName $line.agency $line.Division $line.logins $line.group_calls
            $SheetB.Cells.Item($intRow,2) = $line.UniqueID
            $SheetB.Cells.Item($intRow,27) = $line.logins
            $SheetB.Cells.Item($intRow,28) = $line.group_calls
            $intRow++
          } 
					$intRow = 5
          foreach ($line in $dataC)
          {
            #put the data into these cells and repeat down
            #write-host $line.UniqueID $line.GroupName $line.agency $line.Division $line.logins $line.group_calls
            $SheetC.Cells.Item($intRow,2) = $line.UniqueID
            $SheetC.Cells.Item($intRow,27) = $line.logins
            $SheetC.Cells.Item($intRow,28) = $line.group_calls
            $intRow++
          } 
					$intRow = 5
          foreach ($line in $dataE)
          {
            #put the data into these cells and repeat down
            #write-host $line.UniqueID $line.GroupName $line.agency $line.Division $line.logins $line.group_calls
            $SheetE.Cells.Item($intRow,2) = $line.UniqueID
            $SheetE.Cells.Item($intRow,27) = $line.logins
            $SheetE.Cells.Item($intRow,28) = $line.group_calls
            $intRow++
          } 
        }
  "12-2011" { $intRow = 5
          foreach ($line in $dataB)
          {
            #put the data into these cells and repeat down
            #write-host $line.UniqueID $line.GroupName $line.agency $line.Division $line.logins $line.group_calls
            $SheetB.Cells.Item($intRow,2) = $line.UniqueID
            $SheetB.Cells.Item($intRow,29) = $line.logins
            $SheetB.Cells.Item($intRow,30) = $line.group_calls
            $intRow++
          } 
					$intRow = 5
          foreach ($line in $dataC)
          {
            #put the data into these cells and repeat down
            #write-host $line.UniqueID $line.GroupName $line.agency $line.Division $line.logins $line.group_calls
            $SheetC.Cells.Item($intRow,2) = $line.UniqueID
            $SheetC.Cells.Item($intRow,29) = $line.logins
            $SheetC.Cells.Item($intRow,30) = $line.group_calls
            $intRow++
          } 
					$intRow = 5
          foreach ($line in $dataE)
          {
            #put the data into these cells and repeat down
            #write-host $line.UniqueID $line.GroupName $line.agency $line.Division $line.logins $line.group_calls
            $SheetE.Cells.Item($intRow,2) = $line.UniqueID
            $SheetE.Cells.Item($intRow,29) = $line.logins
            $SheetE.Cells.Item($intRow,30) = $line.group_calls
            $intRow++
          } 
        }
 }
$SheetB.SaveAs($file1)
$Excel.Close()

#Email Section Part 1
#Setting Up My PSCRedentials
$secpasswd = ConvertTo-SecureString "password$" -AsPlainText -Force
$mycreds = New-Object System.Management.Automation.PSCredential("tech", $secpasswd)
#Send email using send-mailmessage
send-mailmessage -to scotta@me.tv-from "Automated Monthly Report<ScottA@me.com>" -subject "Previous Month's Logins&GroupCalls For Systems(B,C,E)" -body "Please let me know if you have any questions. "
 -attachments $file1 -smtpserver techjunkie.tv -Cc scotta@email.org-credential $mycreds

 

Let me know what you think.

Visit my site for other scripts for your use.

PowerShell Tips, SQL Examples, Learn Php

Scott your Tech From Miami

Free Windows Admin Tool Kit Click here and download it now
January 2nd, 2011 1:09am

Hello, I believe the issue is with a bug in the excel object that does not aloow it to be run in a task in windows task scheduler unless the user has checked that the user has to be logged on to run. If you have it to run without user logged on the excel part of the script will not run correctly. I have been testing and breaking my head with this. here is my code
$filenameB = "C:\Users\administrator\Documents\My Dropbox\2010-12-31_1510_SYSB_Last_Month_GroupName_Logins_GroupCalls.csv"
$filenameC = "C:\Users\administrator\Documents\My Dropbox\2010-12-31_1510_SYSC_Last_Month_GroupName_Logins_GroupCalls.csv"
$filenameE = "C:\Users\administrator\Documents\My Dropbox\2010-12-31_1510_SYSE_Last_Month_GroupName_Logins_GroupCalls.csv"



$dataB = Import-Csv $filenameB
$dataC = Import-Csv $filenameC
$dataE = Import-Csv $filenameE
#Creat a new excel Object
$Excel = New-Object -ComObject Excel.Application
#make it invisible
$Excel.visible = $False
#No alerts 
$Excel.DisplayAlerts = $False
#NOw we add a workbook
$file1 = "C:\Users\administrator\Documents\My Dropbox\System_BCE_2011_TalkGroups_Logins_GroupCalls - Copy.xlsx"
$Excel = $Excel.Workbooks.open($file1)
$SheetB = $Excel.Worksheets.Item(1)
$SheetC = $Excel.Worksheets.Item(2)
$SheetE = $Excel.Worksheets.Item(3)
$date = (Get-Date).AddMonths(-1)
$month = Get-Date($date) -f "MM-yyyy"
Switch ($month)
 {
  "12-2010" { $intRow = 5
          foreach ($line in $dataB)
          {
            #put the data into these cells and repeat down
            #write-host $line.UniqueID $line.GroupName $line.agency $line.Division $line.logins $line.group_calls
            $SheetB.Cells.Item($intRow,2) = $line.UniqueID
            $SheetB.Cells.Item($intRow,5) = $line.logins
            $SheetB.Cells.Item($intRow,6) = $line.group_calls
					  $intRow++
          }
					$intRow = 5
					foreach ($line in $dataC){
						$SheetC.Cells.Item($intRow,2) = $line.UniqueID
            $SheetC.Cells.Item($intRow,5) = $line.logins
            $SheetC.Cells.Item($intRow,6) = $line.group_calls
						$intRow++
											}
											$intRow = 5
					foreach ($line in $dataE) {
						$SheetE.Cells.Item($intRow,2) = $line.UniqueID
            $SheetE.Cells.Item($intRow,5) = $line.logins
            $SheetE.Cells.Item($intRow,6) = $line.group_calls
						$intRow++
											}
        }
        
       
 "01-2011" { $intRow = 5
          foreach ($line in $dataB)
          {
            #put the data into these cells and repeat down
            #write-host $line.UniqueID $line.GroupName $line.agency $line.Division $line.logins $line.group_calls
            $SheetB.Cells.Item($intRow,2) = $line.UniqueID
            $SheetB.Cells.Item($intRow,7) = $line.logins
            $SheetB.Cells.Item($intRow,8) = $line.group_calls
					$intRow++
					}
					$intRow = 5
						foreach ($line in $dataC)
          {	$SheetC.Cells.Item($intRow,2) = $line.UniqueID
            $SheetC.Cells.Item($intRow,7) = $line.logins
            $SheetC.Cells.Item($intRow,8) = $line.group_calls
						$intRow++
					}
					$intRow = 5
						foreach ($line in $dataE)
					{
						$SheetE.Cells.Item($intRow,2) = $line.UniqueID
            $SheetE.Cells.Item($intRow,7) = $line.logins
            $SheetE.Cells.Item($intRow,8) = $line.group_calls
            $intRow++
          } 
        }
  "02-2011" { $intRow = 5
          foreach ($line in $dataB)
          {
            #put the data into these cells and repeat down
            #write-host $line.UniqueID $line.GroupName $line.agency $line.Division $line.logins $line.group_calls
            $SheetB.Cells.Item($intRow,2) = $line.UniqueID
            $SheetB.Cells.Item($intRow,9) = $line.logins
            $SheetB.Cells.Item($intRow,10) = $line.group_calls
						$intRow++
          }
					$intRow = 5
					foreach ($line in $dataC)
          {
					$SheetC.Cells.Item($intRow,2) = $line.UniqueID
            $SheetC.Cells.Item($intRow,9) = $line.logins
            $SheetC.Cells.Item($intRow,10) = $line.group_calls
						$intRow++
					}
					$intRow = 5
					foreach ($line in $dataE)
          {
					$SheetE.Cells.Item($intRow,2) = $line.UniqueID
            $SheetE.Cells.Item($intRow,9) = $line.logins
            $SheetE.Cells.Item($intRow,10) = $line.group_calls
						$intRow++
					}
        }
  "03-2011" { $intRow = 5
          foreach ($line in $dataB)
          {
            #put the data into these cells and repeat down
            #write-host $line.UniqueID $line.GroupName $line.agency $line.Division $line.logins $line.group_calls
            $SheetB.Cells.Item($intRow,2) = $line.UniqueID
            $SheetB.Cells.Item($intRow,11) = $line.logins
            $SheetB.Cells.Item($intRow,12) = $line.group_calls
						$intRow++
          } 
					$intRow = 5
					foreach ($line in $dataC)
          {
						$SheetC.Cells.Item($intRow,2) = $line.UniqueID
            $SheetC.Cells.Item($intRow,11) = $line.logins
            $SheetC.Cells.Item($intRow,12) = $line.group_calls
						$intRow++
          } 
					$intRow = 5
					foreach ($line in $dataE)
          {
						$SheetE.Cells.Item($intRow,2) = $line.UniqueID
            $SheetE.Cells.Item($intRow,11) = $line.logins
            $SheetE.Cells.Item($intRow,12) = $line.group_calls
						$intRow++
          } 
        }
  "04-2011" { $intRow = 5
          foreach ($line in $dataB)
          {
            #put the data into these cells and repeat down
            #write-host $line.UniqueID $line.GroupName $line.agency $line.Division $line.logins $line.group_calls
            $SheetB.Cells.Item($intRow,2) = $line.UniqueID
            $SheetB.Cells.Item($intRow,13) = $line.logins
            $SheetB.Cells.Item($intRow,14) = $line.group_calls
            $intRow++
          } 
					$intRow = 5
					foreach ($line in $dataC)
          {
            #put the data into these cells and repeat down
            #write-host $line.UniqueID $line.GroupName $line.agency $line.Division $line.logins $line.group_calls
            $SheetC.Cells.Item($intRow,2) = $line.UniqueID
            $SheetC.Cells.Item($intRow,13) = $line.logins
            $SheetC.Cells.Item($intRow,14) = $line.group_calls
            $intRow++
          } 
					$intRow = 5
					foreach ($line in $dataE)
          {
            
            $SheetE.Cells.Item($intRow,2) = $line.UniqueID
            $SheetE.Cells.Item($intRow,13) = $line.logins
            $SheetE.Cells.Item($intRow,14) = $line.group_calls
            $intRow++
          } 
        }
  "05-2011" { $intRow = 5
          foreach ($line in $dataB)
          {
            #put the data into these cells and repeat down
            #write-host $line.UniqueID $line.GroupName $line.agency $line.Division $line.logins $line.group_calls
            $SheetB.Cells.Item($intRow,2) = $line.UniqueID
            $SheetB.Cells.Item($intRow,15) = $line.logins
            $SheetB.Cells.Item($intRow,16) = $line.group_calls
            $intRow++
          } 
					$intRow = 5
					 foreach ($line in $dataC)
          {
            #put the data into these cells and repeat down
            #write-host $line.UniqueID $line.GroupName $line.agency $line.Division $line.logins $line.group_calls
            $SheetC.Cells.Item($intRow,2) = $line.UniqueID
            $SheetC.Cells.Item($intRow,15) = $line.logins
            $SheetC.Cells.Item($intRow,16) = $line.group_calls
            $intRow++
          } 
					$intRow = 5
					 foreach ($line in $dataE)
          {
            #put the data into these cells and repeat down
            #write-host $line.UniqueID $line.GroupName $line.agency $line.Division $line.logins $line.group_calls
            $SheetE.Cells.Item($intRow,2) = $line.UniqueID
            $SheetE.Cells.Item($intRow,15) = $line.logins
            $SheetE.Cells.Item($intRow,16) = $line.group_calls
            $intRow++
          } 
        }
  "06-2011" { $intRow = 5
          foreach ($line in $dataB)
          {
            #put the data into these cells and repeat down
            #write-host $line.UniqueID $line.GroupName $line.agency $line.Division $line.logins $line.group_calls
            $SheetB.Cells.Item($intRow,2) = $line.UniqueID
            $SheetB.Cells.Item($intRow,17) = $line.logins
            $SheetB.Cells.Item($intRow,18) = $line.group_calls
            $intRow++
          } 
					$intRow = 5
          foreach ($line in $dataC)
          {
            #put the data into these cells and repeat down
            #write-host $line.UniqueID $line.GroupName $line.agency $line.Division $line.logins $line.group_calls
            $SheetC.Cells.Item($intRow,2) = $line.UniqueID
            $SheetC.Cells.Item($intRow,17) = $line.logins
            $SheetC.Cells.Item($intRow,18) = $line.group_calls
            $intRow++
          } 
					$intRow = 5
          foreach ($line in $dataE)
          {
            #put the data into these cells and repeat down
            #write-host $line.UniqueID $line.GroupName $line.agency $line.Division $line.logins $line.group_calls
            $SheetE.Cells.Item($intRow,2) = $line.UniqueID
            $SheetE.Cells.Item($intRow,17) = $line.logins
            $SheetE.Cells.Item($intRow,18) = $line.group_calls
            $intRow++
          } 
        }
  "07-2011" { $intRow = 5
          foreach ($line in $dataB)
          {
            #put the data into these cells and repeat down
            #write-host $line.UniqueID $line.GroupName $line.agency $line.Division $line.logins $line.group_calls
            $SheetB.Cells.Item($intRow,2) = $line.UniqueID
            $SheetB.Cells.Item($intRow,19) = $line.logins
            $SheetB.Cells.Item($intRow,20) = $line.group_calls
            $intRow++
          } 
					$intRow = 5
          foreach ($line in $dataC)
          {
            #put the data into these cells and repeat down
            #write-host $line.UniqueID $line.GroupName $line.agency $line.Division $line.logins $line.group_calls
            $SheetC.Cells.Item($intRow,2) = $line.UniqueID
            $SheetC.Cells.Item($intRow,19) = $line.logins
            $SheetC.Cells.Item($intRow,20) = $line.group_calls
            $intRow++
          } 
					$intRow = 5
          foreach ($line in $dataE)
          {
            #put the data into these cells and repeat down
            #write-host $line.UniqueID $line.GroupName $line.agency $line.Division $line.logins $line.group_calls
            $SheetE.Cells.Item($intRow,2) = $line.UniqueID
            $SheetE.Cells.Item($intRow,19) = $line.logins
            $SheetE.Cells.Item($intRow,20) = $line.group_calls
            $intRow++
          } 
        }
  "08-2011" { $intRow = 5
          foreach ($line in $dataB)
          {
            #put the data into these cells and repeat down
            #write-host $line.UniqueID $line.GroupName $line.agency $line.Division $line.logins $line.group_calls
            $SheetB.Cells.Item($intRow,2) = $line.UniqueID
            $SheetB.Cells.Item($intRow,21) = $line.logins
            $SheetB.Cells.Item($intRow,22) = $line.group_calls
            $intRow++
          } 
					$intRow = 5
          foreach ($line in $dataC)
          {
            #put the data into these cells and repeat down
            #write-host $line.UniqueID $line.GroupName $line.agency $line.Division $line.logins $line.group_calls
            $SheetC.Cells.Item($intRow,2) = $line.UniqueID
            $SheetC.Cells.Item($intRow,21) = $line.logins
            $SheetC.Cells.Item($intRow,22) = $line.group_calls
            $intRow++
          } 
					$intRow = 5
          foreach ($line in $dataE)
          {
            #put the data into these cells and repeat down
            #write-host $line.UniqueID $line.GroupName $line.agency $line.Division $line.logins $line.group_calls
            $SheetE.Cells.Item($intRow,2) = $line.UniqueID
            $SheetE.Cells.Item($intRow,21) = $line.logins
            $SheetE.Cells.Item($intRow,22) = $line.group_calls
            $intRow++
          } 
        }
  "09-2011" { $intRow = 5
          foreach ($line in $dataB)
          {
            #put the data into these cells and repeat down
            #write-host $line.UniqueID $line.GroupName $line.agency $line.Division $line.logins $line.group_calls
            $SheetB.Cells.Item($intRow,2) = $line.UniqueID
            $SheetB.Cells.Item($intRow,23) = $line.logins
            $SheetB.Cells.Item($intRow,24) = $line.group_calls
            $intRow++
          } 
					$intRow = 5
          foreach ($line in $dataC)
          {
            #put the data into these cells and repeat down
            #write-host $line.UniqueID $line.GroupName $line.agency $line.Division $line.logins $line.group_calls
            $SheetC.Cells.Item($intRow,2) = $line.UniqueID
            $SheetC.Cells.Item($intRow,23) = $line.logins
            $SheetC.Cells.Item($intRow,24) = $line.group_calls
            $intRow++
          } 
					$intRow = 5
          foreach ($line in $dataE)
          {
            #put the data into these cells and repeat down
            #write-host $line.UniqueID $line.GroupName $line.agency $line.Division $line.logins $line.group_calls
            $SheetE.Cells.Item($intRow,2) = $line.UniqueID
            $SheetE.Cells.Item($intRow,23) = $line.logins
            $SheetE.Cells.Item($intRow,24) = $line.group_calls
            $intRow++
          } 
        }
  "10-2011" { $intRow = 5
          foreach ($line in $dataB)
          {
            #put the data into these cells and repeat down
            #write-host $line.UniqueID $line.GroupName $line.agency $line.Division $line.logins $line.group_calls
            $SheetB.Cells.Item($intRow,2) = $line.UniqueID
            $SheetB.Cells.Item($intRow,25) = $line.logins
            $SheetB.Cells.Item($intRow,26) = $line.group_calls
            $intRow++
          } 
					$intRow = 5
          foreach ($line in $dataC)
          {
            #put the data into these cells and repeat down
            #write-host $line.UniqueID $line.GroupName $line.agency $line.Division $line.logins $line.group_calls
            $SheetC.Cells.Item($intRow,2) = $line.UniqueID
            $SheetC.Cells.Item($intRow,25) = $line.logins
            $SheetC.Cells.Item($intRow,26) = $line.group_calls
            $intRow++
          }
					$intRow = 5
          foreach ($line in $dataE)
          {
            #put the data into these cells and repeat down
            #write-host $line.UniqueID $line.GroupName $line.agency $line.Division $line.logins $line.group_calls
            $SheetE.Cells.Item($intRow,2) = $line.UniqueID
            $SheetE.Cells.Item($intRow,25) = $line.logins
            $SheetE.Cells.Item($intRow,26) = $line.group_calls
            $intRow++
          } 
        }
  "11-2010" { $intRow = 5
          foreach ($line in $dataB)
          {
            #put the data into these cells and repeat down
            #write-host $line.UniqueID $line.GroupName $line.agency $line.Division $line.logins $line.group_calls
            $SheetB.Cells.Item($intRow,2) = $line.UniqueID
            $SheetB.Cells.Item($intRow,27) = $line.logins
            $SheetB.Cells.Item($intRow,28) = $line.group_calls
            $intRow++
          } 
					$intRow = 5
          foreach ($line in $dataC)
          {
            #put the data into these cells and repeat down
            #write-host $line.UniqueID $line.GroupName $line.agency $line.Division $line.logins $line.group_calls
            $SheetC.Cells.Item($intRow,2) = $line.UniqueID
            $SheetC.Cells.Item($intRow,27) = $line.logins
            $SheetC.Cells.Item($intRow,28) = $line.group_calls
            $intRow++
          } 
					$intRow = 5
          foreach ($line in $dataE)
          {
            #put the data into these cells and repeat down
            #write-host $line.UniqueID $line.GroupName $line.agency $line.Division $line.logins $line.group_calls
            $SheetE.Cells.Item($intRow,2) = $line.UniqueID
            $SheetE.Cells.Item($intRow,27) = $line.logins
            $SheetE.Cells.Item($intRow,28) = $line.group_calls
            $intRow++
          } 
        }
  "12-2011" { $intRow = 5
          foreach ($line in $dataB)
          {
            #put the data into these cells and repeat down
            #write-host $line.UniqueID $line.GroupName $line.agency $line.Division $line.logins $line.group_calls
            $SheetB.Cells.Item($intRow,2) = $line.UniqueID
            $SheetB.Cells.Item($intRow,29) = $line.logins
            $SheetB.Cells.Item($intRow,30) = $line.group_calls
            $intRow++
          } 
					$intRow = 5
          foreach ($line in $dataC)
          {
            #put the data into these cells and repeat down
            #write-host $line.UniqueID $line.GroupName $line.agency $line.Division $line.logins $line.group_calls
            $SheetC.Cells.Item($intRow,2) = $line.UniqueID
            $SheetC.Cells.Item($intRow,29) = $line.logins
            $SheetC.Cells.Item($intRow,30) = $line.group_calls
            $intRow++
          } 
					$intRow = 5
          foreach ($line in $dataE)
          {
            #put the data into these cells and repeat down
            #write-host $line.UniqueID $line.GroupName $line.agency $line.Division $line.logins $line.group_calls
            $SheetE.Cells.Item($intRow,2) = $line.UniqueID
            $SheetE.Cells.Item($intRow,29) = $line.logins
            $SheetE.Cells.Item($intRow,30) = $line.group_calls
            $intRow++
          } 
        }
 }
$SheetB.SaveAs($file1)
$Excel.Close()

#Email Section Part 1
#Setting Up My PSCRedentials
$secpasswd = ConvertTo-SecureString "password$" -AsPlainText -Force
$mycreds = New-Object System.Management.Automation.PSCredential("tech", $secpasswd)
#Send email using send-mailmessage
send-mailmessage -to scotta@me.tv-from "Automated Monthly Report<ScottA@me.com>" -subject "Previous Month's Logins&GroupCalls For Systems(B,C,E)" -body "Please let me know if you have any questions. "
 -attachments $file1 -smtpserver techjunkie.tv -Cc scotta@email.org-credential $mycreds

 

Let me know what you think.

Visit my site for other scripts for your use.

PowerShell Tips, SQL Examples, Learn Php

Scott your Tech From Miami

January 2nd, 2011 1:09am

Just to second that- in very similar scenario, powershell running from task scheduler, writing to excel, will not Save the excel workbook unless "user has to be logged on to run" is checked.  If the user is logged on, and the PC is locked, it will indeed run and save the file... This happens to work for my situtation.

Free Windows Admin Tool Kit Click here and download it now
October 3rd, 2011 3:14pm

I too have run into this problem and did not find a solution. Only way was as said

"user has to be logged on to run"

Maybe a fix for the future?

January 7th, 2012 10:40pm

Hi,

there is a solution in another thread which I can't find yet. So sorry for not linking to it.

 

You have to create a folder (or two on a 64bit-windows):

(32Bit, always)

C:\Windows\System32\config\systemprofile\Dektop

(64Bit)

C:\Windows\SysWOW64\config\systemprofile\Desktop

 

I have had the same problem and this was the only solution i have found.

  • Proposed as answer by cml9076 Monday, April 02, 2012 8:55 PM
Free Windows Admin Tool Kit Click here and download it now
January 8th, 2012 12:01pm

Hi,

there is a solution in another thread which I can't find yet. So sorry for not linking to it.

 

You have to create a folder (or two on a 64bit-windows):

(32Bit, always)

C:\Windows\System32\config\systemprofile\Dektop

(64Bit)

C:\Windows\SysWOW64\config\systemprofile\Desktop

 

I have had the same problem and this was the only solution i have found.

  • Proposed as answer by cml9076 Monday, April 02, 2012 8:55 PM
January 8th, 2012 12:01pm

Thank you for that JensKalski.....that seems to do the trick.
Free Windows Admin Tool Kit Click here and download it now
January 8th, 2012 8:24pm

JensKalski

Well I'll be damned, that did the trick!  Great find!

April 2nd, 2012 8:56pm

Hi,

there is a solution in another thread which I can't find yet. So sorry for not linking to it.

You have to create a folder (or two on a 64bit-windows):

(32Bit, always)

C:\Windows\System32\config\systemprofile\Dektop

(64Bit)

C:\Windows\SysWOW64\config\systemprofile\Desktop

I have had the same problem and this was the only solution i have found.

Thanks Jens!

http://www.patton-tech.com/2012/05/printing-from-scheduled-task-as.html

Free Windows Admin Tool Kit Click here and download it now
May 19th, 2012 9:10pm

Works a treat, thanks Jens.
June 21st, 2012 2:27pm

This worked for me too - thanks!
Free Windows Admin Tool Kit Click here and download it now
August 6th, 2012 2:00pm

Hi,

there is a solution in another thread which I can't find yet. So sorry for not linking to it.

 

You have to create a folder (or two on a 64bit-windows):

(32Bit, always)

C:\Windows\System32\config\systemprofile\Dektop

(64Bit)

C:\Windows\SysWOW64\config\systemprofile\Desktop

 

I have had the same problem and this was the only solution i have found.

Wow it worked for me! (note the first folder name is Desktop not dektop). I was having a problem with the excel saveas command in a powershell script when it was being called from within a SQL command on a sql server.  Which is basically the same as the way the scheduled task runs them.  Definitely a Bug somewhere, likely in the Excel Com Objects.

Tags: ComObject Excel.Application .SaveAs Powershell TransactSQL Stored procedure

August 21st, 2012 8:05pm

I tried just adding the Desktop folder and it did not solve my problem.  Turns out it was due to permissions for the service account I was using to run the scheduled task.  

If you are running the task as a user without local admin privileges there are a few other settings which need to be changed beyond adding the Desktop folder.

For the account you have specified for running the task:  (Paths below are in system32 or SysWow64 depending on 64 or 32bit.)

    • Add Local Launch and Local Activation permissions to the Microsoft Excel Application DCOM Config.
    • Grant "Read & Execute, List folder contents, Read" permissions on the \config\systemprofile\Desktop folder.
    • Grant "Modify, Read & Execute, List Folder Content, Read, Write" permissions for the account on the following folders:
      \config\systemprofile\AppData\Roaming\Microsoft
      \config\systemprofile\AppData\Local\Microsoft

Check out the link below for full directions.

http://troyvssharepoint.blogspot.com/2012/07/stumbled-upon-interesting-one-today.html

Free Windows Admin Tool Kit Click here and download it now
September 11th, 2012 3:10pm

I'm also looking for a solution to generating a excel file within a powershell script run as scheduled task.

I've tried to create the folders as listet, but this does not do the trick for me.

I'm running on Windows Server 2012, maybe this is the problem? The scheduled task is configured to "Run with highest privileges" and "Configure for: Windows Server 2012". But also when "Configure for: Windows Server 2008 R2" is set, it does not help.

My user used for the scheduled task has Admin-Rights on the server and when logging in with it the script works fine. When running as scheduled task the script only sends an email, without generating the excel file (and sending it as attachment).

February 7th, 2013 6:50am

Mansch, you have me worried.  I had this problem, and created the folders, and it now works as a Scheduled Task on Server 2008 R2 - but I am going to be trying to migrate to Server 2012.

Let me know if you get anywhere with this.

I might try the permissions that Onpeak above talked about on my Server 2012 machine.

jj

Free Windows Admin Tool Kit Click here and download it now
May 7th, 2013 2:45am

Hmmm, would be interesting to know if the same config that worked for you with 2008 R2 will also work with 2012.

Maybe you could keep us informed, as I have not found any other special solution for 2012 yet.

May 7th, 2013 7:53am

This was a good find!  I was about to re-write my entire script because I couldnt export the data to excel.

Thank you VERY much for posting!

Mark

Free Windows Admin Tool Kit Click here and download it now
July 20th, 2013 12:46pm

I have the same problem with Excel on a Windows Server 2012, which is invoked by a C# program via Microsoft.Office.Interop.Excel. It runs when started by doubleclicking at the program. When i start it via scheduler it generates an exception at:

wk=excel.Workbooks.Add(Type.Missing);

The error message:

Microsoft Office Excel cannot open or save any more documents because there is not enough available memory or disk space.

  To make more memory available, close workbooks or programs you no longer need.

  To free disk space, delete files you no longer need from the disk you are saving to.

__________________________________________________________________________________

i start the program with scheduler and the same user, which is able to start it by doubleclicking.

i have set the checkbox "With highest privileges". The free memory is about 1 GB.

I tried to add the "Desktop"-folder as described above and which works for Windows Server 2008.

Nothing helped. I am at a loss. Has someone experience with such problems on Windows Server 2012?

July 29th, 2013 11:28am

Seems you have got the same problem as I have and that this solution does not work with Windows Server 2012.

Is there any other option to make an excel file without using Excel? ;)

Free Windows Admin Tool Kit Click here and download it now
July 29th, 2013 11:35am

not really, because this program is taking the data from SAP and generates a list of open sales orders grouped by week of delivery.

This file is for end-users and they don't want to open an ascii-file with excel and grouping the data by themself.

July 29th, 2013 11:47am

Not really any other option or not really the same problem?

I also try to make an excel file (with PowerShell).

And I know other programs, that create excel files, that do not use excel APIs, but instead generate an excel file (xls format).

Free Windows Admin Tool Kit Click here and download it now
July 29th, 2013 11:54am

I don't know if this will help anyone running Server 2012 but I had a like problem on Windows Server 2008 R2 using PowerShell 3 and Excel 2003 SP3 as a COM object.  My script and task would work fine unless running the task with "Run whether user is logged on or not" was checked.  In that case no Excel file would be created and I would get Windows Logs, System events like the following:

The machine-default permission settings do not grant Local Activation permission for the COM Server application with CLSID {00024500-0000-0000-C000-000000000046} and APPID Unavailable
 to the user DOMAIN\thicks SID (S-1-5-21-1437597970-832611207-2781745148-1166) from address LocalHost (Using LRPC). This security permission can be modified using the Component Services administrative tool.

The solution was to run DCOMCNFG and right click on Component Services, Computer, My Computer and choose Properties.  Then on the COM Security tab, Launch and Activation Permissions, Edit Default I added the specific user that was running the script, checked all four boxes and life is good again!

July 30th, 2013 3:06pm

Seems not to work with Windows Server 2012.
Free Windows Admin Tool Kit Click here and download it now
July 31st, 2013 2:43pm

Very helpful thread - thanks to all who have posted. I can confirm that Onpeak's steps worked on Win7-64, using PowerShell 64-bit with COM activation of 32-bit Excel 2010. Both the DCOM tweak and the .\Desktop folder creation were required.
September 4th, 2013 7:09pm

I have create PowerShell Excel Export that works without COM and without Excel!
look if this solves your Problems on Server 2012:

http://gallery.technet.microsoft.com/scriptcenter/Export-XLSX-PowerShell-f2f0c0

September 7th, 2013 3:36pm

Can this Excel Export Script also work with formatting and multiple sheets?
September 9th, 2013 7:22am

Can this Excel Export Script also work with formatting and multiple sheets?

No!

But in a first step you can Export PowerShell Objects to an Excel workbook worksheet.

And in a second step you can do formatting (with COM). 

Free Windows Admin Tool Kit Click here and download it now
September 9th, 2013 12:03pm

I can confirm it runs OK in a 2012 server environment.  We have a powershell script that is using the Excel COM object.
February 14th, 2014 8:59pm

This worked great.

Any explanation as to why this works?  Why would Excel start under the system profile instead of the profile the script is running under?

Thanks! 

Free Windows Admin Tool Kit Click here and download it now
February 21st, 2014 5:40pm

This solved a long pending problem of mine. Thank you soooo much.:)

March 2nd, 2014 1:36am

Holy Guacamole! Not sure if I should jump up with joy or punch someone. This solution works and also makes no sense. In case someone got as confused as I did, you just have to create the folder below and do nothing else ...and my Excel files get generated by my task scheduler application!

=======================

Hi,

there is a solution in another thread which I can't find yet. So sorry for not linking to it.

You have to create a folder (or two on a 64bit-windows):

(32Bit, always)

C:\Windows\System32\config\systemprofile\Desktop

(64Bit)

C:\Windows\SysWOW64\config\systemprofile\Desktop

I have had the same problem and this was the only solution i have found.

Free Windows Admin Tool Kit Click here and download it now
March 6th, 2014 11:04pm

Thanks Jens Kalski it did fixed the issue... 
April 1st, 2014 10:28am

I have not been able to get this to work on 2012. I've stooped to running it with my own account, which is an admin on the box. I've created the Desktop folders for the systemprofile, I've checked DCOM permissions and even though I'm an administrator, I specifically added my user account. I've tried running in 2008, 2008R2 and 2012 modes. I've run x86 PowerShell, x64 PowerShell, I've changed COM permissions, tried running it with -noexit... I've run procmon and I can see that Excel opens the CSV I'm converting, but it doesn't seem to do the SaveAs. I've checked file and share permissions.... I'm pretty much at a loss. It runs every time without fail from the console, it's just that I can't get it to run as a scheduled task.

Any thoughts as to what else I could be missing? I'm beyond grasping at straws at this point.

Edit: Figured it out. After some more time with Procmon, I found that Excel was trying to do a CreateFile operation in C:\Windows\SysWOW64\config\systemprofile\AppData\Local\Microsoft\Windows\Temporary Internet Files

The problem was the Temporary Internet Files folder didn't exist in that location. Once I added that folder and re-ran my scheduled task, it worked just fine.


  • Edited by WBrady1965 Wednesday, April 30, 2014 7:22 PM Update
  • Proposed as answer by Iroqouiz Friday, October 17, 2014 7:47 AM
Free Windows Admin Tool Kit Click here and download it now
April 30th, 2014 6:32pm

I have not been able to get this to work on 2012. I've stooped to running it with my own account, which is an admin on the box. I've created the Desktop folders for the systemprofile, I've checked DCOM permissions and even though I'm an administrator, I specifically added my user account. I've tried running in 2008, 2008R2 and 2012 modes. I've run x86 PowerShell, x64 PowerShell, I've changed COM permissions, tried running it with -noexit... I've run procmon and I can see that Excel opens the CSV I'm converting, but it doesn't seem to do the SaveAs. I've checked file and share permissions.... I'm pretty much at a loss. It runs every time without fail from the console, it's just that I can't get it to run as a scheduled task.

Any thoughts as to what else I could be missing? I'm beyond grasping at straws at this point.

Edit: Figured it out. After some more time with Procmon, I found that Excel was trying to do a CreateFile operation in C:\Windows\SysWOW64\config\systemprofile\AppData\Local\Microsoft\Windows\Temporary Internet Files

The problem was the Temporary Internet Files folder didn't exist in that location. Once I added that folder and re-ran my scheduled task, it worked just fine.


  • Edited by WBrady1965 Wednesday, April 30, 2014 7:22 PM Update
  • Proposed as answer by Iroqouiz Friday, October 17, 2014 7:47 AM
April 30th, 2014 6:32pm

That solved my problem.

Free Windows Admin Tool Kit Click here and download it now
May 12th, 2014 11:01am

Holy Guacamole! Not sure if I should jump up with joy or punch someone. This solution works and also makes no sense. In case someone got as confused as I did, you just have to create the folder below and do nothing else ...and my Excel files get generated by my task scheduler application!

=======================

Hi,

there is a solution in another thread which I can't find yet. So sorry for not linking to it.

You have to create a folder (or two on a 64bit-windows):

(32Bit, always)

C:\Windows\System32\config\systemprofile\Desktop

(64Bit)

C:\Windows\SysWOW64\config\systemprofile\Desktop

I have had the same problem and this was the only solution i have found.

When I tried this, my reaction was similar. 

But the most important thing is: After many spent hours trying different proposed solutions,  FINALLY, IT WORKS.

Happy user

May 12th, 2014 11:05am

I have not been able to get this to work on 2012. I've stooped to running it with my own account, which is an admin on the box. I've created the Desktop folders for the systemprofile, I've checked DCOM permissions and even though I'm an administrator, I specifically added my user account. I've tried running in 2008, 2008R2 and 2012 modes. I've run x86 PowerShell, x64 PowerShell, I've changed COM permissions, tried running it with -noexit... I've run procmon and I can see that Excel opens the CSV I'm converting, but it doesn't seem to do the SaveAs. I've checked file and share permissions.... I'm pretty much at a loss. It runs every time without fail from the console, it's just that I can't get it to run as a scheduled task.

Any thoughts as to what else I could be missing? I'm beyond grasping at straws at this point.

Edit: Figured it out. After some more time with Procmon, I found that Excel was trying to do a CreateFile operation in C:\Windows\SysWOW64\config\systemprofile\AppData\Local\Microsoft\Windows\Temporary Internet Files

The problem was the Temporary Internet Files folder didn't exist in that location. Once I added that folder and re-ran my scheduled task, it worked just fine.


Thank you! This solved my problem on Server 2012, tried everything else in this thread.
Free Windows Admin Tool Kit Click here and download it now
October 17th, 2014 7:48am

Hi WBrady1965 and Iroqouiz,

I have the similar issue, the schedule task run the task and when it took the out to a file, i get the following error.

Exception calling "Add" with "0" argument(s): "Microsoft Excel cannot open or 
save any more documents because there is not enough available memory or disk 
space. 
 To make more memory available, close workbooks or programs you no longer 
need. 
 To free disk space, delete files you no longer need from the disk you are 
saving to."
At D:\Scripts\test.ps1:24 char:1
+ $workbook = $excel.Workbooks.Add()
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
    + CategoryInfo          : NotSpecified: (:) [], MethodInvocationException
    + FullyQualifiedErrorId : ComMethodTargetInvocation
 
(:) [], RuntimeException
    + FullyQualifiedErrorId : InvokeMethodOnNull
 
You cannot call a method on a null-valued expression.
At  D:\Scripts\test.ps1:26 char:1
+ $failedclient.Cells.Item(1,1) = 'SL No'

November 13th, 2014 3:22pm

Hi Vishsb,

has the excel file macros in it? In my case it had to do with security problems. I added the path of the excel file in the trusted pathes and then it worked. You can also try to open the excel file on the machine where the script runs with the same user as the scrupt runs and then check if it the options button appears.

regards

elu

Free Windows Admin Tool Kit Click here and download it now
November 14th, 2014 6:36am

Wow,

I used this solution on 2008 Server R2.

It work like a charm. Thanks

:-)

January 2nd, 2015 7:14pm

Nice! I've added at start of my PS script:

# Excel cannot open file bug fix
if (-Not (Test-Path C:\Windows\System32\config\systemprofile\Dektop)) { New-Item C:\Windows\System32\config\systemprofile\Dektop -type Directory }
if (-Not (Test-Path C:\Windows\SysWOW64\config\systemprofile\Desktop)) { New-Item C:\Windows\SysWOW64\config\systemprofile\Desktop -type Directory }
Works fine now.

Windows Server 2012 R2

Runs without stored password or highest privileges.

Free Windows Admin Tool Kit Click here and download it now
January 15th, 2015 10:45am

Beware of your typo here:

if (-Not (Test-Path C:\Windows\System32\config\systemprofile\Dektop)) {
    New-Item C:\Windows\System32\config\systemprofile\Dektop -type Directory
}

This error will only affect 64 bit processes.  The 32 bit version in SysWow64 is correct in the above exa

January 15th, 2015 11:28am

Hi,

there is a solution in another thread which I can't find yet. So sorry for not linking to it.

 

You have to create a folder (or two on a 64bit-windows):

(32Bit, always)

C:\Windows\System32\config\systemprofile\Dektop

(64Bit)

C:\Windows\SysWOW64\config\systemprofile\Desktop

 

I have had the same problem and this was the only solution i have found.

I, too, was experiencing the same issue on Windows Server 2012 R2 x64 and Excel 2013. Sadly, this did not work for me. I did get it working however and wanted to post for anyone else still struggling with this problem.

First I was getting an access denied error. I created the above folders without it working. I then went into Component Services >  Microsoft Excel Application >  Launch and Activation Permissions and explicitly added my account (I was part of the admin group which was already there, but wanted to rule it out). That didn't work. I then explicitly added the account to My Computer > Com Permissions > Both Launch and Access permissions (Again, I was part of the admin group, but wanted to rule it out). Once adding my account there the access denied error message went away.

Then I started getting the "Failed to open" error that I see a lot of people getting. I had already added the desktop folders to no avail. I added the following folders per another post I found:

C:\Windows\SysWOW64\config\systemprofile\AppData\Local\Microsoft\Windows\Temporary Internet Files

C:\Windows\SysWOW64\config\systemprofile\AppData\Roaming\Microsoft\Windows\Temporary Internet Files

C:\Windows\System32\config\systemprofile\AppData\Local\Microsoft\Windows\Temporary Internet Files

C:\Windows\System32\config\systemprofile\AppData\Roaming\Microsoft\Windows\Temporary Internet Files

Still didn't work. So I ran ProcMon and started looking at what was being referenced. I found two additional directories it was trying to reference and was failing:

C:\Windows\SysWOW64\config\systemprofile\Documents

C:\Windows\SysWOW64\config\systemprofile\AppData\Local\Microsoft\Windows\INetCache

Once I added those two additional directories, my files opened properly using the Excel COM object without needing to be logged in.

Sorry for the long post, but I wanted to include all troubleshooting attempts I made as I have not gone back and checked which ones were needed and which ones were not. But I wanted to get the fix out there to help others having the same frustrations I have had the past 2 days.


Free Windows Admin Tool Kit Click here and download it now
May 21st, 2015 9:47am

No one should do this.  You have just destroyed most of the system accounts security wrapper.  The system is totally vulnerable to many attack mechanism.

Microsoft says that Office products cannot be run "headless" for a reason.

May 21st, 2015 9:54am

Hi,

there is a solution in another thread which I can't find yet. So sorry for not linking to it.

 

You have to create a folder (or two on a 64bit-windows):

(32Bit, always)

C:\Windows\System32\config\systemprofile\Dektop

(64Bit)

C:\Windows\SysWOW64\config\systemprofile\Desktop

 

I have had the same problem and this was the only solution i have found.

I, too, was experiencing the same issue on Windows Server 2012 R2 x64 and Excel 2013. Sadly, this did not work for me. I did get it working however and wanted to post for anyone else still struggling with this problem.

First I was getting an access denied error. I created the above folders without it working. I then went into Component Services >  Microsoft Excel Application >  Launch and Activation Permissions and explicitly added my account (I was part of the admin group which was already there, but wanted to rule it out). That didn't work. I then explicitly added the account to My Computer > Com Permissions > Both Launch and Access permissions (Again, I was part of the admin group, but wanted to rule it out). Once adding my account there the access denied error message went away.

Then I started getting the "Failed to open" error that I see a lot of people getting. I had already added the desktop folders to no avail. I added the following folders per another post I found:

C:\Windows\SysWOW64\config\systemprofile\AppData\Local\Microsoft\Windows\Temporary Internet Files

C:\Windows\SysWOW64\config\systemprofile\AppData\Roaming\Microsoft\Windows\Temporary Internet Files

C:\Windows\System32\config\systemprofile\AppData\Local\Microsoft\Windows\Temporary Internet Files

C:\Windows\System32\config\systemprofile\AppData\Roaming\Microsoft\Windows\Temporary Internet Files

Still didn't work. So I ran ProcMon and started looking at what was being referenced. I found two additional directories it was trying to reference and was failing:

C:\Windows\SysWOW64\config\systemprofile\Documents

C:\Windows\SysWOW64\config\systemprofile\AppData\Local\Microsoft\Windows\INetCache

Once I added those two additional directories, my files opened properly using the Excel COM object without needing to be logged in.

Sorry for the long post, but I wanted to include all troubleshooting attempts I made as I have not gone back and checked which ones were needed and which ones were not. But I wanted to get the fix out there to help others having the same frustrations I have had the past 2 days.


Free Windows Admin Tool Kit Click here and download it now
May 21st, 2015 1:40pm

Hi,

there is a solution in another thread which I can't find yet. So sorry for not linking to it.

 

You have to create a folder (or two on a 64bit-windows):

(32Bit, always)

C:\Windows\System32\config\systemprofile\Dektop

(64Bit)

C:\Windows\SysWOW64\config\systemprofile\Desktop

 

I have had the same problem and this was the only solution i have found.

I, too, was experiencing the same issue on Windows Server 2012 R2 x64 and Excel 2013. Sadly, this did not work for me. I did get it working however and wanted to post for anyone else still struggling with this problem.

First I was getting an access denied error. I created the above folders without it working. I then went into Component Services >  Microsoft Excel Application >  Launch and Activation Permissions and explicitly added my account (I was part of the admin group which was already there, but wanted to rule it out). That didn't work. I then explicitly added the account to My Computer > Com Permissions > Both Launch and Access permissions (Again, I was part of the admin group, but wanted to rule it out). Once adding my account there the access denied error message went away.

Then I started getting the "Failed to open" error that I see a lot of people getting. I had already added the desktop folders to no avail. I added the following folders per the post above:

C:\Windows\SysWOW64\config\systemprofile\AppData\Local\Microsoft\Windows\Temporary Internet Files

C:\Windows\SysWOW64\config\systemprofile\AppData\Roaming\Microsoft\Windows\Temporary Internet Files

C:\Windows\System32\config\systemprofile\AppData\Local\Microsoft\Windows\Temporary Internet Files

C:\Windows\System32\config\systemprofile\AppData\Roaming\Microsoft\Windows\Temporary Internet Files

Still didn't work. So I ran ProcMon and started looking at what was being referenced. I found two additional directories it was trying to reference and was failing:

C:\Windows\SysWOW64\config\systemprofile\Documents

C:\Windows\SysWOW64\config\systemprofile\AppData\Local\Microsoft\Windows\INetCache

Once I added those two additional directories, my files opened properly using the Excel COM object without needing to be logged in.

Sorry for the long post, but I wanted to include all troubleshooting attempts I made as I have not gone back and checked which ones were needed and which ones were not. But I wanted to get the fix out there to help others having the same frustrations I have had the past 2 days.



  • Edited by James Culpovich Tuesday, May 26, 2015 1:43 PM Corrected post referrence
May 21st, 2015 1:40pm

Mind explaining how this affects the service account's security wrapper? I am not using the service account.

Additionally, How is adding my name to a list that I am already a part of open me up to many attacks?
Free Windows Admin Tool Kit Click here and download it now
May 22nd, 2015 10:55am

Mind explaining how this affects the service account's security wrapper? I am not using the service account.

Additionally, How is adding my name to a list that I am already a part of open me up to many attacks?

Sorry - I thought you had posted that you added you account to the folders security.  I see I misread the post.

If you don't change security on system objects in the system folders then you are probably ok.  Expect that the script will continue to break for new reasons as MS tightens the security on the system. Be careful that you use full paths in your scripts that run under scheduler as the default will be the system profile. 

Be sure to set "Start In" to an accessible location for the account running the script.

 

May 22nd, 2015 11:08am

Boy - that worked for me!!!!  I could generate Excel file on local drive but not on network drive.  Checked to make sure all 8 directories existed - if not I made them.  Running server 2012 - running scheduled task as server 2008.
Free Windows Admin Tool Kit Click here and download it now
August 11th, 2015 3:46pm

This topic is archived. No further replies will be accepted.

Other recent topics Other recent topics