• Misha_SQL - Friday, September 18, 2015 8:59 AM

    Thank you for the article! For those of us powershell-challenged, can you explain this part in the RESTORE stetment: $($tlog.name) ? I understand the part inside the parenthesis, but why do we need enclose it in $() again? Thank you!

    Powershell's double-quoted strings allow one to have a $variable's value get injected into the string. In most "real" programming languages, you're concatenating string literals with variable values to do the same.

    "RESTORE LOG [RestoreDemo] FROM DISK=N'$($tlog.name)' WITH NORECOVERY;"

    So... "RESTORE...$($tlog.name)..." 

    Because $tlog is an object reference, and not a string value, we see the special sauce we have to use to pull this off:
    $($tlog.name) is the Powershell idiom to properly inject the current value of the $tlog.name property value into that double-quoted string. 
    If you were to try this instead:
    "RESTORE ... $tlog.name...", Powershell will "stringify" the $tlog object to inject that into things, which is probably not what you want to happen. It shouldn't break anything directly there, but it won't be the droid you're looking for.