Windows 7 Ultimate x64 Task Scheduler and Powershell 2.0

User avatar
geedeearr
StarLounger
Posts: 52
Joined: 04 Feb 2010, 17:14
Location: Brookings, South Dakota

Windows 7 Ultimate x64 Task Scheduler and Powershell 2.0

Post by geedeearr »

Hi All,
I've got a batch file which used to execute a Powershell script.

Code: Select all

powershell -command "& 'C:\JobsScripts\ScriptFiles\AgentJobStatusEmail.ps1' "
The PoSh script queries multiple instances of SQL Server jobs on their last run time, etc., creates an Excel workbook (using COM object), formats and enters the job data into the worksheet. The workbook is then saved to a folder and from that location, attached to an email and sent to various recipients.
The PoSh script executes successfully from within the PowerGUI script editor and the batch file executes the PoSh script successfully from a command prompt. (Both executions use my AdminAccount)
However, when executed via the Task Scheduler, everything seems to be working, except for the creation (and hence the attachment) of the Excel file, as there is no .xlsx file in the saved to folder and an attachment-less email is received. The task itself, is set to use my AdminAccount with "run with highest priviledges".

I have a NormalUser AD account that I use to log into my work station. Both my NormalUser and my AdminAccount are members of an AD group which is a member of the Administrators group on my work station. For opening the PowerGUI script editor or the command prompt, I use RunAs in a shortcut and use my AD AdminAccount, as it is this account which is a SQL Server sysadmin and member of the Administrators group on all the servers running an instance of SQL Server.

Code: Select all

%windir%\system32\runas.exe /user:domain\AdminAccount "C:\WINDOWS\system32\cmd.exe"
%windir%\system32\runas.exe /user:domain\AdminAccount "C:\Program Files (x86)\PowerGUI\ScriptEditor.exe"
I can't get the Task Scheduler to open that way, so I have used the extended right click menu (<Shift><Right Click>) and "Run As a Different User" selection using my AdminAccount. This, however, does not grant the elevated Administrator credentials that, I think, are necessary for the creation and manipulation of the Excel worksheet/book. So I have also tried the right click menu to use the "Run As Administrator" and set the schedule I am creating to use my AdminAccount to "run with highest priviledges"....which, of course, it doesn't seem to. To me, it seems as if my AdminAccount credentials are not "filtering down" to the PoSh script.

So....what are the permission(s)/method(s) needed or a work around so I can accomplish this task?

Thank you.
gary

Those who dance are considered insane by those who can't hear the music. - George Carlin                    Image

JoeP
SilverLounger
Posts: 2069
Joined: 25 Jan 2010, 02:12

Re: Windows 7 Ultimate x64 Task Scheduler and Powershell 2.0

Post by JoeP »

Is the destination folder local to the machine where the script is executed?

Is that scheduled job set to only run when the user account is logged on?

Joe
Joe

User avatar
geedeearr
StarLounger
Posts: 52
Joined: 04 Feb 2010, 17:14
Location: Brookings, South Dakota

Re: Windows 7 Ultimate x64 Task Scheduler and Powershell 2.0

Post by geedeearr »

Hi Joe,
The batch file is located in a folder that is within the same parent folder as is the PoSh script: C:\JobsScripts\BatFiles
The Excel file(s) is/are also located in a folder that is within the same parent folder as is the PoSh script: C:\JobsScripts\ExcelFiles
The job is set to "Run whether user is logged on or not."

Thanks.
gary

Those who dance are considered insane by those who can't hear the music. - George Carlin                    Image