Problems with $(...) powershell sintax in SQL Server

  • Hi All

    I have a doubt about it

    I have a job running on SQL Server 2008 R2 that has a step with Powershell commands but it does not works, there is a problem when I use $() to encapsule a variable, for example I want to search files with this statement

    $BackupFiles=get-childItem -name -include *_$($NumberOfWeek)_*

    where $NumberOfWeek is used to get all BAK files that has in their name _14_

    for example it must retrieve:

    20120409_1129_14_database.bak

    anything_14_database.bak

    In the job there is a sintax error in the statement mentioned before, but when I don use "$()"

    there is no problem

    someone knows how can I solve it?

    Thanks in advance

  • Hi

    It was solved with other option

    Instead of using *_$($NumberOfWeek)_*

    I created three variables more

    $Open="*_"

    $Close="_*"

    $PartialName=$Open+$NumberOfWeek+$Close

    and it replaced that piece of statement

    If someone has other solution using $(...), it would be great

  • constant strings have to be quoted out i think,

    so I'm fairly sure this is the right syntax:

    $BackupFiles=get-childItem -name -include "*_"+$NumberOfWeek+"_*"

    Lowell


    --help us help you! If you post a question, make sure you include a CREATE TABLE... statement and INSERT INTO... statement into that table to give the volunteers here representative data. with your description of the problem, we can provide a tested, verifiable solution to your question! asking the question the right way gets you a tested answer the fastest way possible!

  • Thanks!

    You are right!, my mistake was take all as an only string.

    Although it is strange that SQL Server does not accept $()

    because I tried it on Powershell and it works correctly, but when it was implemented on SQL Server, it threw an error

    Anyway, thanks

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

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