powersell script execute from sql server agent job failed

Ashwan 536 Reputation points
2020-11-25T01:16:55.207+00:00

Hi I created power shell script and unable to execute from the SQL server agen job. But through commnad promt powershell prompt etc. only diffrentl is I manually did uisng my local user. but sql server agent run with diffrent user. I already given permission on C:\app\DBA.

Version: 2016 SP2 enterpirse
+++++++++++++++++++++++++++++++++++++

42300-ps1.png

42323-ps2.png

42324-ps2log.png

42371-ps3.png

SQL Server Other
0 comments No comments
{count} votes

3 answers

Sort by: Most helpful
  1. m 4,276 Reputation points
    2020-11-25T08:47:25.39+00:00

    Hi @Ashwan

    The system can't find the file specified.

    1.The error messages tell us, the path or the file name is not correct. Make sure the path in your command is one absolute path and the filename is correct.
    2.Change Type from Operating system(CmdExec) to PowerShell in your job.

    42499-20201125checknamepath.png

    Firstly, make sure the Domain\Account has the permission to access the file.--From your description, you have already done this;
    Then make sure the file path and name is correct, for example test on my own side, if I named the file as 1.txt, use the command as this copy C:\powershelltest\1.txt C:\powershelltest\11.txt, it will not be copied,the error message shows can not find the file specified, because the real name of the file is 1.txt.txt, then I change the file as 1, then the command is executed correctly.
    42641-202011251txttxt.png
    42642-202011125txttxtcode.png
    42500-20201125checkthefilenamepath.png

    Steps as next:

    --Step 1: create one txt file and then save it as one batch file
    Note: Write the absolute path here, because there is no way to enter the current path directly after SQL Server
    42467-20201125absolutepath.png

    --Step2: create the job,Note: choose the Type: PowerShell
    42517-20201125typeandjob.png

    --Step3, verify whether there is one 11 txt file.
    42620-20201112511txt.png

    BR,
    Mia


    If the answer is helpful, please click "Accept Answer" and upvote it.

    0 comments No comments

  2. m 4,276 Reputation points
    2020-11-26T01:30:58.313+00:00

    Hi @Ashwan ,

    Is the reply helpful?

    BR,
    Mia


    If the answer is helpful, please click "Accept Answer" and upvote it.

    0 comments No comments

  3. Ashwan 536 Reputation points
    2020-11-30T09:39:31.05+00:00

    Hi Mia Many thanks for the reply . Yes looks job is complete. but following "Restore-SqlDatabase "commnad didnt executed . I can confirmed "Get-Process" executed. bit strange wht didnt executed "restore" command? and no error on that. But when I run the script manually through the powershell , then it is executed successfully .

    Get-Process | Out-File -FilePath C:\app\DBA\1.txt
    Restore-SqlDatabase -ServerInstance $TargetSqlServerInstance -Database $TargetDb -BackupFile $FileToRestore -RelocateFile @($RelocateData,$RelocateData1,$RelocateData2,$RelocateData3,$RelocateData4,$RelocateLog) -ReplaceDatabase -ConnectionTimeout 0
    Restore-SqlDatabase -ServerInstance $TargetSqlServerInstance -Database $TargetDb -BackupFile $FileToRestore -RelocateFile @($RelocateData,$RelocateData1,$RelocateData2,$RelocateData3,$RelocateData4,$RelocateLog) -ReplaceDatabase

    0 comments No comments

Your answer

Answers can be marked as Accepted Answers by the question author, which helps users to know the answer solved the author's problem.