Want to protect your cyber security and still get fast solutions? Ask a secure question today.Go Premium

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 501
  • Last Modified:

Powershell script will not run correctly when run from Task Scheduler

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
c7c4c7
Asked:
c7c4c7
  • 3
  • 2
  • 2
  • +3
3 Solutions
 
Joe Winograd, EE MVE 2015&2016DeveloperCommented:
Try setting "Run with highest privileges" (which runs the script as Administrator, i.e., elevated):

task scheduler run elevated
Regards, Joe
1
 
c7c4c7Author Commented:
It is all ready set that way as well as Run whether the user is logged on or not
0
 
footechCommented:
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
Who's Defending Your Organization from Threats?

Protecting against advanced threats requires an IT dream team – a well-oiled machine of people and solutions working together to defend your organization. Download our resource kit today to learn more about the tools you need to build you IT Dream Team!

 
Robberbaron (robr)Commented:
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
 
QlemoC++ DeveloperCommented:
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
 
David Johnson, CD, MVPOwnerCommented:
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
 
David Johnson, CD, MVPOwnerCommented:
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
 
QlemoC++ DeveloperCommented:
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
 
c7c4c7Author Commented:
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
 
QlemoC++ DeveloperCommented:
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

Free Tool: ZipGrep

ZipGrep is a utility that can list and search zip (.war, .ear, .jar, etc) archives for text patterns, without the need to extract the archive's contents.

One of a set of tools we're offering as a way to say thank you for being a part of the community.

  • 3
  • 2
  • 2
  • +3
Tackle projects and never again get stuck behind a technical roadblock.
Join Now