Solved

Powershell script will not run correctly when run from Task Scheduler

Posted on 2016-08-06
11
128 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 51

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 39

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
 
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 68

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
6 Surprising Benefits of Threat Intelligence

All sorts of threat intelligence is available on the web. Intelligence you can learn from, and use to anticipate and prepare for future attacks.

 
LVL 78

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 78

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 68

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 68

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

IT, Stop Being Called Into Every Meeting

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

Join & Write a Comment

Suggested Solutions

In this previous article (https://oddytee.wordpress.com/2016/05/05/provision-new-office-365-user-and-mailbox-from-exchange-hybrid-via-powershell/), we made basic license assignments to users in O365. When I say basic, the method is the simplest way …
Set OWA language and time zone in Exchange for individuals, all users or per database.
Windows 8 comes with a dramatically different user interface known as Metro. Notably missing from the new interface is a Start button and Start Menu. Many users do not like it, much preferring the interface of earlier versions — Windows 7, Windows X…
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…

706 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

Need Help in Real-Time?

Connect with top rated Experts

20 Experts available now in Live!

Get 1:1 Help Now