Solved

Powershell script will not run correctly when run from Task Scheduler

Posted on 2016-08-06
11
226 Views
Last Modified: 2016-08-08
I have a script that looks for a specific folder in Outlook, then looks for specific computer names in the subject line.  When it's done it sends me an email of any errors

Because of the problems I was having I send some debugging info to a file.  When I run the script from the Task Scheduler, the subject line is never output to the file which means to me that it never finds the folder.  When I run it via Run or a Powershell window everything is good

The computer names are kept in a txt file
The script is attached, Let me know if you want to see something from the Task Scheduler
clear-host

$computerFoundArray = @()
$computerNotFoundArray = @()
$computerFailedArray = @()

$inputpath = "C:\Bat\Data\computers.txt"
[System.Collections.ArrayList]$computerArray = gc $inputpath

$olFolderInbox = 6
$outlook = new-object -com outlook.application;
$ns = $outlook.GetNameSpace("MAPI");
$inbox = $ns.GetDefaultFolder($olFolderInbox)
$targetfolder = $inbox.Folders | where-object { $_.name -eq "Daily Report" }

$targetfolder.items | foreach {
	foreach ($computer in $computerArray){$debuglog = $debuglog + $_.subject + " = Subject `r`n"
			if($_.subject -match $computer) {
			$computerFoundArray = $computerFoundArray + $computer
		}
	}
	}
foreach ($computer in $computerArray){
	if($computerFoundArray -notcontains $computer){$debuglog = $debuglog + "  " + $computer + " Not Found `r`n"
		$computerNotFoundArray = $computerNotFoundArray + $computer
	}
}

$debuglog | Out-File 'c:\bat\data\debug.txt' -Append
sendDailyJobErrors $computerNotFoundArray
find-missingAcronis

Open in new window

0
Comment
Question by:c7c4c7
  • 3
  • 2
  • 2
  • +3
11 Comments
 
LVL 53

Expert Comment

by:Joe Winograd, EE MVE
ID: 41745918
Try setting "Run with highest privileges" (which runs the script as Administrator, i.e., elevated):

task scheduler run elevated
Regards, Joe
1
 

Author Comment

by:c7c4c7
ID: 41745926
It is all ready set that way as well as Run whether the user is logged on or not
0
 
LVL 40

Accepted Solution

by:
footech earned 250 total points
ID: 41745975
Having "Run whether user is logged on or not" is likely your problem.
From my reading trying to use Office COM objects in a non-interactive session is very problematic.  If it works when you uncheck that setting, I wouldn't put any money on getting it to work otherwise.
1
Best Practices: Disaster Recovery Testing

Besides backup, any IT division should have a disaster recovery plan. You will find a few tips below relating to the development of such a plan and to what issues one should pay special attention in the course of backup planning.

 
LVL 32

Expert Comment

by:Robberbaron (robr)
ID: 41746107
is the user you test with the same as the one assigned in the Scheduled Task ?

I have found it necesasry to havce it hust that way as it ensures DLL's etc are registered correctly. ( i created a TaskRunner user for this purpose so they have a local profile)
0
 
LVL 69

Expert Comment

by:Qlemo
ID: 41746118
As said above by the other Experts: I'm 100% confident you need to use the same user for the task and interactive, and to run the task only when logged in - you can't open Outlook for the same PST/OST files more than once, so you have to reuse an open Outlook, which again requires to act inside of the current interactive session.
Run with highest privileges might be necessary or not, I cannot tell.
0
 
LVL 80

Assisted Solution

by:David Johnson, CD, MVP
David Johnson, CD, MVP earned 125 total points
ID: 41746120
uncheck run with highest privileges as you must run as a user that has an outlook profile.
I put an exit as the last line of the script or it will continue running.

If I run as an administrator
MyDocuments:\WindowsPowerShell\Scripts
$ . 'D:\Documents\WindowsPowershell\Scripts\ee07082016.ps1'
[6,12: New-Object] Retrieving the COM class factory for component with CLSID {00
06F03A-0000-0000-C000-000000000046} failed due to the following error: 80080005 
Server execution failed (Exception from HRESULT: 0x80080005 (CO_E_SERVER_EXEC_FA
ILURE)).
[7,1] You cannot call a method on a null-valued expression.
[8,1] You cannot call a method on a null-valued expression.

MyDocuments:\WindowsPowerShell\Scripts
$ 

Open in new window

I call it from a batch file .. runme.cmd
c:\windows\system32\WindowsPowershell\v1.0\powershell -noprofile d:\Documents\WindowsPowershell\Scripts\ee07082016.ps1
exit

Open in new window

0
 
LVL 80

Expert Comment

by:David Johnson, CD, MVP
ID: 41746131
If I set the task to run only when the user is logged in it works whether outlook is open or not.
if task is set to run with highest privileges and outlook is not open it opens in a hidden window
using a try/catch block and the error is
The error message was Call was rejected by callee. (Exception from HRESULT: 0x80010001 (RPC_E_CALL_REJECTED))
if outlook is open
The error message was Retrieving the COM class factory for component with CLSI
D {0006F03A-0000-0000-C000-000000000046} failed due to the following error: 8008
0005 Server execution failed (Exception from HRESULT: 0x80080005 (CO_E_SERVER_EX
EC_FAILURE)).
0
 
LVL 69

Assisted Solution

by:Qlemo
Qlemo earned 125 total points
ID: 41746179
On W7, PS5 without UAC, I performed a simple test running
$olk = New-Object -Com Outlook.Application
$AdminInbox = $olk.Session.Folders("Administrator@domain.com").Folders("Inbox").Items

$adminInbox.Count | Out-File C:\Temp\EE\test.txt

Open in new window

with
powershell -command "& {start-process powershell '-version 4 -File C:\Temp\EE\test-outlook.ps1' -RedirectStandardOutput c:\Temp\EE\test.log -RedirectStandardError C:\Temp\ee\test-error.log}"

Open in new window

which runs no matter whether Outlook is open, the task is running elevated, or the session is interactive or not. A non-interactive session is much slower, having to start Outlook is much slower too, but that are the only effects I see.
0
 

Author Comment

by:c7c4c7
ID: 41746608
The user account is the same as the one assigned in the Scheduled Task

Running without the "Run with Highest privileges" selected and "Only when the user logged on" takes care of my problem as stated

It does open a powershell window when it runs, why is that

The script was to run whether I was logged on or not, it runs in the middle of the night.  Any suggestions
0
 
LVL 69

Expert Comment

by:Qlemo
ID: 41746734
What's the issue with staying logged in over night?
You can also try if switching to the access way I showed makes a difference (not using MAPI namespace).
1

Featured Post

Back Up Your Microsoft Windows Server®

Back up all your Microsoft Windows Server – on-premises, in remote locations, in private and hybrid clouds. Your entire Windows Server will be backed up in one easy step with patented, block-level disk imaging. We achieve RTOs (recovery time objectives) as low as 15 seconds.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Synchronize a new Active Directory domain with an existing Office 365 tenant
This article explains how to prepare an HTML email signature template file containing dynamic placeholders for users' Azure AD data. Furthermore, it explains how to use this file to remotely set up a department-wide email signature policy in Office …
With the advent of Windows 10, Microsoft is pushing a Get Windows 10 icon into the notification area (system tray) of qualifying computers. There are many reasons for wanting to remove this icon. This two-part Experts Exchange video Micro Tutorial s…
Two types of users will appreciate AOMEI Backupper Pro: 1 - Those with PCIe drives (and haven't found cloning software that works on them). 2 - Those who want a fast clone of their boot drive (no re-boots needed) and it can clone your drive wh…

828 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question