Click here to monitor SSC
SQLServerCentral is supported by Redgate
 
Log in  ::  Register  ::  Not logged in
 
 
 


Problem copying .bak with "$" in file path/name (PS running in SQL job step)


Problem copying .bak with "$" in file path/name (PS running in SQL job step)

Author
Message
mstjean
mstjean
SSC-Addicted
SSC-Addicted (449 reputation)SSC-Addicted (449 reputation)SSC-Addicted (449 reputation)SSC-Addicted (449 reputation)SSC-Addicted (449 reputation)SSC-Addicted (449 reputation)SSC-Addicted (449 reputation)SSC-Addicted (449 reputation)

Group: General Forum Members
Points: 449 Visits: 2540
I'm using OLA's SQL JOBs to do my database maintenance. So far, I really like them; they're very handy, especially when apps create new databases each month (WILDCARD dbnames!).

I have to copy files across server instances on occasion; since we're deploying SQL2008r2 I've started using Powershell to do this.
I've got a powershell job step that works fine to get bak files from my old servers to new servers.

But now I have to copy a backup created by the Ola backup process... it includes $ in the path (as servername$instancename) and at the start of the backup file name.

It LOOKS like the $ is confusing Powershell...

Is there a control character I can use to allow the "$" into the path/filename, or is there another way to do this?

#Example: (this is a powershell step in a sql job)

# get yesterdays date as YYYYMMDD
$dd = (get-date).AddDays(-1).ToString("yyyyMMdd")

# here's the original source/target location for the backup file
#$from = "\\srvrname\r$\SQL\MSSQL10_50.instname\MSSQL\Backup\srvrname$instname\DBNAME\FULL\srvrname$instname_DBNAME_FULL_" + $dd + "*.BAK"
#$to = "\\SA01SQL002\u$\SQL\MSSQL10_50.TEST\MSSQL\Backup\from$instname\"
#and here's the error on the copy-item line:
# 'An object at the specified path \\srvrname\r$\SQL\MSSQL10_50.instname\MSSQL\Backup\srvrname\DBNAME\FULL does not exist.'
# ********
# whoops... that should say \srvrname$instname\
#

# that blows up so tried wrapping $from and $to within a double quote... this didn't fly either.
$from = """" + ...TheOriginalString... + """"
$to = """" + ...TheOriginalString... + """"

# the error references this line
copy-item $from $to -Force -Recurse



Cursors are useful if you don't know SQL
mstjean
mstjean
SSC-Addicted
SSC-Addicted (449 reputation)SSC-Addicted (449 reputation)SSC-Addicted (449 reputation)SSC-Addicted (449 reputation)SSC-Addicted (449 reputation)SSC-Addicted (449 reputation)SSC-Addicted (449 reputation)SSC-Addicted (449 reputation)

Group: General Forum Members
Points: 449 Visits: 2540
Well... this fixed it. But now I'd like to know if anybody knows WHY it fixed it...

#this fails
$from = "\\srvrname\r$\SQL\MSSQL10_50.instname\MSSQL\Backup\srvrname$instname\DBNAME\FULL\srvrname$instname_DBNAME_FULL_" + $dd + "*.BAK"

#but this works
$from = "\\srvrname\r$\SQL\MSSQL10_50.instname\MSSQL\Backup\srvrname" + "$" + "instname\DBNAME\FULL\srvrname" + "$" + "instname_DBNAME_FULL_" + $dd + "*.BAK"



Cursors are useful if you don't know SQL
Gary Varga
Gary Varga
SSCrazy Eights
SSCrazy Eights (8.3K reputation)SSCrazy Eights (8.3K reputation)SSCrazy Eights (8.3K reputation)SSCrazy Eights (8.3K reputation)SSCrazy Eights (8.3K reputation)SSCrazy Eights (8.3K reputation)SSCrazy Eights (8.3K reputation)SSCrazy Eights (8.3K reputation)

Group: General Forum Members
Points: 8253 Visits: 6126
When using double quotes (") PowerShell looks to replace variables with their values whereas if you use single quotes (') it does not. So as you used double quotes it interpreted $instname as a variable, for example.

If you have no embedded variables then you can use the following:

$from = '\\srvrname\r$\SQL\MSSQL10_50.instname\MSSQL\Backup\srvrname$instname\DBNAME\FULL\srvrname$instname_DBNAME_FULL_' + $dd  + "*.BAK"



Or you can escape the dollar signs ($) with a grave accent (`) like this:

$from = "\\srvrname\r`$\SQL\MSSQL10_50.instname\MSSQL\Backup\srvrname`$instname\DBNAME\FULL\srvrname`$instname_DBNAME_FULL_" + $dd  + "*.BAK"



The following should work too:

$from = "\\srvrname\r`$\SQL\MSSQL10_50.instname\MSSQL\Backup\srvrname`$instname\DBNAME\FULL\srvrname`$instname_DBNAME_FULL_$dd*.BAK"



Hope this helps.

Gaz

-- Stop your grinnin' and drop your linen...they're everywhere!!!
mstjean
mstjean
SSC-Addicted
SSC-Addicted (449 reputation)SSC-Addicted (449 reputation)SSC-Addicted (449 reputation)SSC-Addicted (449 reputation)SSC-Addicted (449 reputation)SSC-Addicted (449 reputation)SSC-Addicted (449 reputation)SSC-Addicted (449 reputation)

Group: General Forum Members
Points: 449 Visits: 2540
THANKS for the info!!! It's very much appreciated.

(like your sig... so in the same spirit, "Game over, man... GAME OVER!!!")

...edit after reading ALL the examples... WHOA, that last one is slick as ... well, it's slick!


Cursors are useful if you don't know SQL
Gary Varga
Gary Varga
SSCrazy Eights
SSCrazy Eights (8.3K reputation)SSCrazy Eights (8.3K reputation)SSCrazy Eights (8.3K reputation)SSCrazy Eights (8.3K reputation)SSCrazy Eights (8.3K reputation)SSCrazy Eights (8.3K reputation)SSCrazy Eights (8.3K reputation)SSCrazy Eights (8.3K reputation)

Group: General Forum Members
Points: 8253 Visits: 6126
You are most welcome - makes a change for me being the helper as opposed to the helped!!!

Gaz

-- Stop your grinnin' and drop your linen...they're everywhere!!!
Go


Permissions

You can't post new topics.
You can't post topic replies.
You can't post new polls.
You can't post replies to polls.
You can't edit your own topics.
You can't delete your own topics.
You can't edit other topics.
You can't delete other topics.
You can't edit your own posts.
You can't edit other posts.
You can't delete your own posts.
You can't delete other posts.
You can't post events.
You can't edit your own events.
You can't edit other events.
You can't delete your own events.
You can't delete other events.
You can't send private messages.
You can't send emails.
You can read topics.
You can't vote in polls.
You can't upload attachments.
You can download attachments.
You can't post HTML code.
You can't edit HTML code.
You can't post IFCode.
You can't post JavaScript.
You can post emoticons.
You can't post or upload images.

Select a forum

































































































































































SQLServerCentral


Search