0

I have a Powershell script which is running fine in Windows power shell IDE. But when I am running it from SQL agent job it is running with no error but it isn't doing any operation. Below is the message which I am getting in job history

     Executed as user: SERVER\SYSTEM. The string starting:  At line:1 
    char:1  +  <<<< "D:\FOLDER\POWERSCRIPT.ps1?  is missing the terminator: ". 
     At line:1 char:23  + "D:\FOLDER\POWERSCRIPT.ps1? <<<<      
     + CategoryInfo          : ParserError: (D:\FOLDER\POWERSCRIPT.ps1?:String) [],       ParentContainsErrorRecordException     
 + FullyQualifiedErrorId : 
TerminatorExpectedAtEndOfString.  Process Exit Code 0.  The step succeeded.

Though, I have ran it from service account which is "DOMAIN\SERVICEACCOUNT" but it showing me "SERVERNAME**SYSTEM**

Let me know if more details is required.

Edit- Script of Job

DECLARE @jobId BINARY(16)
EXEC @ReturnCode =  msdb.dbo.sp_add_job @job_name=N'MYJOB', 
        @enabled=1, 
        @notify_level_eventlog=0, 
        @notify_level_email=0, 
        @notify_level_netsend=0, 
        @notify_level_page=0, 
        @delete_level=0, 
        @description=N'No description available.', 
        @category_name=N'[Uncategorized (Local)]', 
        @owner_login_name=N'DOMAIN\SERVICEAACCOUNT', @job_id = @jobId OUTPUT
IF (@@ERROR <> 0 OR @ReturnCode <> 0) GOTO QuitWithRollback
/****** Object:  Step [RUN MONITOR]    Script Date: 5/15/2015 11:01:29 AM ******/
EXEC @ReturnCode = msdb.dbo.sp_add_jobstep @job_id=@jobId, @step_name=N'RUN MONITOR', 
        @step_id=1, 
        @cmdexec_success_code=0, 
        @on_success_action=1, 
        @on_success_step_id=0, 
        @on_fail_action=2, 
        @on_fail_step_id=0, 
        @retry_attempts=0, 
        @retry_interval=0, 
        @os_run_priority=0, @subsystem=N'CmdExec', 
        @command=N'powershell “D:\FOLDER\POWERSCRIPT.ps1″', 
        @flags=0
2
  • This might be a problem with the SQL Agent job definition rather than the powershell. The error suggests to me a problem with the string that tells the job where to find the Powershell script. Can you post the script for the agent job step? Commented May 15, 2015 at 10:59
  • @ChrisFlynn I have added script to my question Commented May 15, 2015 at 11:04

1 Answer 1

1
@command=N'powershell “D:\FOLDER\POWERSCRIPT.ps1″',

The quote marks here don't look right. You should remove them and re-type them as double-quote marks, like this:

@command=N'powershell "D:\FOLDER\POWERSCRIPT.ps1"',

Can you see the difference? This sometimes occurs when copying and pasting values.

Sign up to request clarification or add additional context in comments.

2 Comments

+1 That works. But do you have any idea why it is not executing as service account SERVERName\SYSTEM. Due to this I am not able to run my powershell script.
I'm sorry, I'm not sure. However, the following documentation might help; there is a parameter for @@database_user_name on sp_add_jobstep. msdn.microsoft.com/en-gb/library/ms187358.aspx. The parameter, and also @@owner_login_name, have some dependencies on sysadmin and database users, so maybe that'll help you? (Note double-@ otherwise SO thinks I'm tagging a user)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.