namerg
asked on
Issue when scheduling a Powershell script
Hello,
I am having a weird behavior when executing a schedule task that executes a powershell script.
I logged in as a Domain Admin, we have an account called script_svc with domain admin rights.
I create the schedule task to be executed by the script_svc domain admin account.
The schedule task executes the script "fine" but with unexpected results.
But if I do Run As on the Powershell Icon and enter the "script_svc" credentials and execute the script manually, run 100% fine with the expected results.
Thoughts ?
Thanks,
I am having a weird behavior when executing a schedule task that executes a powershell script.
I logged in as a Domain Admin, we have an account called script_svc with domain admin rights.
I create the schedule task to be executed by the script_svc domain admin account.
The schedule task executes the script "fine" but with unexpected results.
But if I do Run As on the Powershell Icon and enter the "script_svc" credentials and execute the script manually, run 100% fine with the expected results.
Thoughts ?
Thanks,
ASKER
There is nothing wrong with the script. The Powershell gurus from EE contributed a lot :)
The unexpected results is that emails only two attachments: DupClockNumber.csv and NewADUsers.csv and the odd thing is the NewADUsers.csv is empty.
And, the expected results are four attachments: DupClockNumber.csv, NewADUsers..csv, ExistingCompanyUsers.csv and CSVADMismatch.csv. And these attachments have info.
I have been working on this script for almost a month daily. It runs fine on the console manually.
The unexpected results is that emails only two attachments: DupClockNumber.csv and NewADUsers.csv and the odd thing is the NewADUsers.csv is empty.
And, the expected results are four attachments: DupClockNumber.csv, NewADUsers..csv, ExistingCompanyUsers.csv and CSVADMismatch.csv. And these attachments have info.
I have been working on this script for almost a month daily. It runs fine on the console manually.
Make sure you provided correct/exact path for report files in script..
ASKER
Subsun, yes, I did...
Are you using system paths? Are these mapped under the svc account
Are you running the schedule from same computer where you are able to test the script successfully?
Do you have any firewall/AV on computer (Where you schedule the script) which is blocking connection for PowerShell?
Do you have any firewall/AV on computer (Where you schedule the script) which is blocking connection for PowerShell?
ASKER
Not using system paths.. I am troubleshooting it right now. Works fine through the command line but not via schedule task. :(
ASKER
Yes. I am at the Script server where we run all the scripts.. No FW, No AV
ASKER
Wow...this is f^%$# weird...have no clue what is going on. Subsun, do you want to see the code?
You can post it and explain how you scheduled the script.. Also did you check if your AV/firewall on the server causing any issue?
ASKER
Thanks subsun for following. This is the script that we have been working together, I put it in production, and I have a feeling that our AD is not replicating well :( Anyway, I will put another question about how to point the LDAP Query in a single domain controller instead doing the LDAP query for the whole domain.
Here it is, the schedule task
Here it is, the schedule task
<?xml version="1.0" encoding="UTF-16"?>
<Task version="1.2" xmlns="http://schemas.microsoft.com/windows/2004/02/mit/task">
<RegistrationInfo>
<Date>2013-06-24T20:15:40.8357861</Date>
<Author>COMPANY\DomainAdmin</Author>
</RegistrationInfo>
<Triggers>
<TimeTrigger>
<StartBoundary>2013-06-24T20:20:00</StartBoundary>
<Enabled>true</Enabled>
</TimeTrigger>
</Triggers>
<Principals>
<Principal id="Author">
<UserId>UPICOLO\RoseroGA</UserId>
<LogonType>Password</LogonType>
<RunLevel>HighestAvailable</RunLevel>
</Principal>
</Principals>
<Settings>
<MultipleInstancesPolicy>IgnoreNew</MultipleInstancesPolicy>
<DisallowStartIfOnBatteries>true</DisallowStartIfOnBatteries>
<StopIfGoingOnBatteries>true</StopIfGoingOnBatteries>
<AllowHardTerminate>true</AllowHardTerminate>
<StartWhenAvailable>false</StartWhenAvailable>
<RunOnlyIfNetworkAvailable>false</RunOnlyIfNetworkAvailable>
<IdleSettings>
<StopOnIdleEnd>true</StopOnIdleEnd>
<RestartOnIdle>false</RestartOnIdle>
</IdleSettings>
<AllowStartOnDemand>true</AllowStartOnDemand>
<Enabled>true</Enabled>
<Hidden>false</Hidden>
<RunOnlyIfIdle>false</RunOnlyIfIdle>
<WakeToRun>false</WakeToRun>
<ExecutionTimeLimit>P3D</ExecutionTimeLimit>
<Priority>7</Priority>
</Settings>
<Actions Context="Author">
<Exec>
<Command>powershell.exe</Command>
<Arguments>-command "C:\Scripts\Ceridian\Test_IDMgmt.ps1"</Arguments>
</Exec>
</Actions>
</Task>
Schedule task configuration looks ok to me..
I will put another question about how to point the LDAP Query in a single domain controller instead doing the LDAP query for the whole domain.Anyways it will be connecting to a domain controller to collect information about the domain. If you have multiple DC's then it will be random connection. If you want to specify a DC to connect then you can..
ASKER
I think I need to point it into another DC, i have to put lots of sleep times so far it worked fine but it broke on
I think the the solution for this thread is to point the script into a domain controller instead of the domain which has multiple domain controllers. I did put a question about it.
Rename-ADObject : Directory object not found
At C:\scripts\ceridian\Test_IDMgmt.ps1:171 char:7
+ Rename-ADObject -Identity $newdn -NewName $CN
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
+ CategoryInfo : ObjectNotFound: (CN=williamsp,CN=Users,DC=company,DC=com:ADObject) [Rename-ADObject], ADIdentityNotFoundException
+ FullyQualifiedErrorId : Directory object not found,Microsoft.ActiveDirectory.Management.Commands.RenameADObject
I think the the solution for this thread is to point the script into a domain controller instead of the domain which has multiple domain controllers. I did put a question about it.
Depends.. IMO, If you have a replication problem then you need to fix it... If there is a replication delay then you need to add wait time in script..
ASKER
Subsun, i did point the script to point to a domain controller. But, is not working fine through the schedule task. :(
ASKER
If i go to the command prompt with Run As like the following:
powershell.exe -file "C:\Scripts\Ceridian\Test_ IDMgmt.ps1 "
It runs fine, i added the -file into the schedule task and same problem..it does not create the AD users,nothing...how do i echo the schedule task so i can see what is going on ?
powershell.exe -file "C:\Scripts\Ceridian\Test_
It runs fine, i added the -file into the schedule task and same problem..it does not create the AD users,nothing...how do i echo the schedule task so i can see what is going on ?
Add -executionpolicy bypass & -noexit as a parameter to powershell.exe, and see if you get any error on console..
For example..
powershell.exe -executionpolicy bypass -noexit -file <script>
Or try directing the output to a txt file by adding >c:\log.txt at the end of command..
For example..
powershell.exe -executionpolicy bypass -noexit -file <script>
Or try directing the output to a txt file by adding >c:\log.txt at the end of command..
ASKER
I did add at the beginning of the script the following:
Start-Transcript -Path "C:\TEMP\Temp.log"and i got interesting results after executing in it through the schedule task
**********************
Windows PowerShell transcript start
Start time: 20130625120518
Username : company\companySCRIPT_SVC
Machine : companySCRIPT (Microsoft Windows NT 6.1.7601 Service Pack 1)
**********************
Transcript started, output file is C:\TEMP\Temp.log
Exception calling "Open" with "1" argument(s): "Microsoft Excel cannot access
the file 'C:\scripts\ceridian\06-25-2013.xls'. There are several possible
reasons:
• The file name or path does not exist.
• The file is being used by another program.
• The workbook you are trying to save has the same name as a currently open
workbook."
At C:\Scripts\Ceridian\Test_IDMgmt.ps1:36 char:2
+ $objworkbook=$objExcel.Workbooks.Open($CeridianFilePath)
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
+ CategoryInfo : NotSpecified: (:) [], MethodInvocationException
+ FullyQualifiedErrorId : ComMethodTargetInvocation
You cannot call a method on a null-valued expression.
At C:\Scripts\Ceridian\Test_IDMgmt.ps1:37 char:2
+ $objworkbook.SaveAs($savePath,6)
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
+ CategoryInfo : InvalidOperation: (:) [], RuntimeException
+ FullyQualifiedErrorId : InvokeMethodOnNull
You cannot call a method on a null-valued expression.
At C:\Scripts\Ceridian\Test_IDMgmt.ps1:38 char:2
+ $objworkbook.Close($false)
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~
+ CategoryInfo : InvalidOperation: (:) [], RuntimeException
+ FullyQualifiedErrorId : InvokeMethodOnNull
Import-Csv : Could not find file 'C:\scripts\ceridian\06-25-2013.csv'.
At C:\Scripts\Ceridian\Test_IDMgmt.ps1:53 char:2
+ Import-Csv $CSVsavePathFile | ?{$_."Div Descrip" -match
"Administration"} | sel ...
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~
+ CategoryInfo : OpenError: (:) [Import-Csv], FileNotFoundExcepti
on
+ FullyQualifiedErrorId : FileOpenFailure,Microsoft.PowerShell.Commands.Im
portCsvCommand
Compare-Object : Cannot bind argument to parameter 'DifferenceObject' because
it is null.
At C:\Scripts\Ceridian\Test_IDMgmt.ps1:234 char:21
+ Compare-Object $AD $CSV -property "Last Name","First Name","Job
Title","Clock N ...
+ ~~~~
+ CategoryInfo : InvalidData: (:) [Compare-Object], ParameterBind
ingValidationException
+ FullyQualifiedErrorId : ParameterArgumentValidationErrorNullNotAllowed,M
icrosoft.PowerShell.Commands.CompareObjectCommand
new-object : Exception calling ".ctor" with "1" argument(s): "Could not find
file 'C:\scripts\ceridian\06-25-2013_ExistingCOMPANYUsersOU_LOG.csv'."
At C:\Scripts\Ceridian\Test_IDMgmt.ps1:290 char:12
+ $att4 = new-object Net.Mail.Attachment($emailattachment4)
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
+ CategoryInfo : InvalidOperation: (:) [New-Object], MethodInvoca
tionException
+ FullyQualifiedErrorId : ConstructorInvokedThrowException,Microsoft.Power
Shell.Commands.NewObjectCommand
new-object : Exception calling ".ctor" with "1" argument(s): "Could not find
file 'C:\scripts\ceridian\06-25-2013_CSVADMismatch_LOG.csv'."
At C:\Scripts\Ceridian\Test_IDMgmt.ps1:291 char:12
+ $att5 = new-object Net.Mail.Attachment($emailattachment5)
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
+ CategoryInfo : InvalidOperation: (:) [New-Object], MethodInvoca
tionException
+ FullyQualifiedErrorId : ConstructorInvokedThrowException,Microsoft.Power
Shell.Commands.NewObjectCommand
Exception calling "Add" with "1" argument(s): "Value cannot be null.
Parameter name: item"
At C:\Scripts\Ceridian\Test_IDMgmt.ps1:300 char:4
+ $msg.Attachments.Add($att4)
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~
+ CategoryInfo : NotSpecified: (:) [], MethodInvocationException
+ FullyQualifiedErrorId : ArgumentNullException
Exception calling "Add" with "1" argument(s): "Value cannot be null.
Parameter name: item"
At C:\Scripts\Ceridian\Test_IDMgmt.ps1:301 char:4
+ $msg.Attachments.Add($att5)
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~
+ CategoryInfo : NotSpecified: (:) [], MethodInvocationException
+ FullyQualifiedErrorId : ArgumentNullException
You cannot call a method on a null-valued expression.
At C:\Scripts\Ceridian\Test_IDMgmt.ps1:305 char:4
+ $att4.Dispose()
+ ~~~~~~~~~~~~~~~
+ CategoryInfo : InvalidOperation: (:) [], RuntimeException
+ FullyQualifiedErrorId : InvokeMethodOnNull
You cannot call a method on a null-valued expression.
At C:\Scripts\Ceridian\Test_IDMgmt.ps1:306 char:4
+ $att5.Dispose()
+ ~~~~~~~~~~~~~~~
+ CategoryInfo : InvalidOperation: (:) [], RuntimeException
+ FullyQualifiedErrorId : InvokeMethodOnNull
**********************
Windows PowerShell transcript end
End time: 20130625120600
**********************
If it's a win 2008 server then check this..
http://social.msdn.microsoft.com/Forums/office/en-US/ac2549f5-fc1e-494d-9015-70cb31d3aeb2/opening-excel-workbook-fails-when-run-from-scheduled-task-on-windows-server-2008-rw
http://social.msdn.microsoft.com/Forums/office/en-US/ac2549f5-fc1e-494d-9015-70cb31d3aeb2/opening-excel-workbook-fails-when-run-from-scheduled-task-on-windows-server-2008-rw
ASKER
Almost there, is not creating the CSV file on this code and i have a win Server 2008 R2 Standard with Excel 2010 instaled. I did create the directory that Subsun suggested on the link and nada..
#Write-Host "BEGIN Convert_XLS_To_CSV Function"
$CSVext = ".csv"
$CeridianFilePath = "C:\scripts\ceridian\" + $CeridianFile + $XLSext
$savePath = "C:\scripts\ceridian\" + $CeridianFile + $CSVext
$objExcel = New-Object -ComObject Excel.Application
$objworkbook=$objExcel.Workbooks.Open($CeridianFilePath)
$objworkbook.SaveAs($savePath,6)
$objworkbook.Close($false)
$objExcel.Quit()
$CSVsavePathFile = $savePath
#Write-Host "END Convert_XLS_To_CSV Function"
return $CSVsavePathFile
Dumb question but still.. Does the account which you use to run the schedule task has local admin right?
ASKER
Yes sir.
On my client, windows 7 the schedule task runs fine.
Also, I created a check for the csv and is clear is not creating it on the Script Server
On my client, windows 7 the schedule task runs fine.
Also, I created a check for the csv and is clear is not creating it on the Script Server
Can you try changing the volume? "C:\scripts\ceridian\" to "D:\scripts\ceridian\"
(or any available volume other than c:\)
(or any available volume other than c:\)
ASKER
Nothing :(
I got the email ERROR: dd-mm-yyyy.CSV was not created
I got the email ERROR: dd-mm-yyyy.CSV was not created
I couldn't find any other reason..After creating folder C:\Windows\SysWOW64\config \systempro file\Deskt op, Did you restart the server?
ASKER
Rebooted the server and it looks like I have to execute the schedule task with the option "Run only when user is logged on" Not good. thank you Microsoft. :(
ASKER
I am going to try by logging in locally with a local admin account and execute the powershell script but i do get the following: WARNING: Error initializing default drive: 'The server has rejected the client credentials.'.
PS C:\Users\admin>
Thoughts ?
PS C:\Users\admin>
Thoughts ?
I didn't get time to check this on my test box.. I will let you know as soon as I can..
Mean time you can try disabling the UAC on server and see if it makes any change...
Mean time you can try disabling the UAC on server and see if it makes any change...
ASKER
Thanks Subsun. UAC is disable and same thing..Let me know what you find...
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER
You got it..
If it runs correctly as an Admin user but not as a scheduled task running under an Admin user then probably there is something wrong in your script but you are asking to debug a question.