Powershell syntax error in SQL server Agent job

  • Hi,

    I am running below code, running fine in PS terminal and ISE.  When I am running it from SQL agent job it's failing with

    "Message - Unable to start execution of step 1 (reason: line(1): Syntax error). The step failed."

    ---------------------------------------------------------------------

    #Create folder

    $f = New-Item "E:\Archive_backups\$(get-date -f yyyy-MM-dd-hh-mm-ss)" -ItemType Directory -Force

    #Copy all files

    Copy-Item E:\MainBackups\* $f.FullName

    ------------------------------------

    Please suggest.

    • This topic was modified 3 years, 3 months ago by  sdbjakeer.
  • Does drive E: exist on the SQL Server machine, or is it a mapped drive?

    If you haven't even tried to resolve your issue, please don't expect the hard-working volunteers here to waste their time providing links to answers which you could easily have found yourself.

  • E drive is local drive not an network mapped.

  • As stated in the other thread you posted to - the problem is that SQL Server Agent uses tokens, and tokens are defined by $(token).  Since your code has this: $(get-date -f yyyy-MM-dd-hh-mm-ss), SQL Server Agent is attempting to parse that out as a token.

    Change your code so it doesn't use that structure, something like:

    $directoryName = "E:\Archive_Backups\" + (Get-Date -f yyyy-MM-dd-hh-mm-ss);
    $f = New-Item $directoryName -ItemType Directory -Force;

    With that said - you can simplify your code to just this:

    Copy-Item -Path E:\MainBackups\ -Destination "E:\Archive_Backups\" + (Get-Date -f yyyy-MM-dd-hh-mm-ss);

     

    Jeffrey Williams
    “We are all faced with a series of great opportunities brilliantly disguised as impossible situations.”

    ― Charles R. Swindoll

    How to post questions to get better answers faster
    Managing Transaction Logs

Viewing 4 posts - 1 through 3 (of 3 total)

You must be logged in to reply to this topic. Login to reply