4

I have heard that it is a best practice to maintain a QA environment for SharePoint. I would like to script a nightly restore from our production SharePoint 2013 farm, which I'm successfully backing up with Backup-SPFarm.

I have a working PowerShell script that includes, in a nutshell,

Restore-SPFarm -Directory $backupdir -RestoreMethod New  -NewDatabaseServer $qadbserver `
    -FarmCredentials $farmcred `
    -Item 'Farm\Microsoft SharePoint Foundation Web Application'  -Confirm:$false 

It works, but the problem is that it pauses prompts interactively for all sorts of additional details. For example,

PS C:\Windows\system32> C:\MY_MOSS\Backup_restore\restore.ps1
Loading C:\MY_MOSS\Backup_restore\common.ps1
Change location/name for: Farm\Microsoft SharePoint Foundation Web Application\SharePoint - 80
New web application name: (default: SharePoint - 80):

So how can I schedule it? There aren't any additional documented options to Restore-SPFarm to answer the additional questions. I'm pretty new to PowerShell. Is there a best-practice way of somehow piping answers to interactive prompts?

UPDATE 5/29

In response to Falak Mahmood's questions, here is a screenshot of what happens when I run it with powershell -noninteractive:

Running restore.ps1 with -noninteractive

It is trying to prompt, to ask me further questions for confirmation, or in the case of -RestoreMethod Overwrite, which is what I really need to restore the prod backups into the QA environment, it is asking for a whole bunch of new app and database names and other questions.

I will try to post a screenshot of that interactive conversation tomorrow. I don't think this is going to be possible with Powershell + MOSS 2013 + Restore-SPFarm OotB. Maybe I can automate a smaller restore, like all the individual site collections.

  • Not sure if it is compatible with the powershell switch -NonInteractive ie. c:\Windows\System32\WindowsPowerShell\v2.0\powershell.exe -NonInteractive. Because I can't test this for sure I won't post as an answer. – Hugh Wood May 23 '13 at 8:21
  • Thanks @HughWood. When I try powershell.exe -Noninteractive -file .\restore.ps1 it errors out with a NativeCommandError, powershell.exe : Restore-SPFarm : Windows PowerShell is in NonInteractive mode. Read and Prompt ... functionality is not available. on the line where I call Restore-SPFarm. – Nathan May 23 '13 at 17:42
  • That's kinda what I feared. Doesn't look like it can do unattended from powershell. – Hugh Wood May 24 '13 at 8:13
1

Okay the only answer to this then is. Don't use powershell.

This is available through the object model here:

http://msdn.microsoft.com/en-GB/library/cc264319%28v=office.14%29.aspx

  • 2
    Well if you can do it in object model you can do it in PowerShell, as PowerShell uses the object model? – Anders Rask May 27 '13 at 11:43
  • Well with using the NonInteractive flag not working according to the OP this is the "alternate" solution to the issue. However if Falak's script does work I can only presume something didn't work when it was tried before. I presume it doesn't work because it doesn't supply all the required details. This however can work in SSOM since the options are there to be set. This makes me believe the powershell cmdlet is using public calls available in SSOM where it usually uses private for things we cannot access. – Hugh Wood May 28 '13 at 8:18
1

Make sure that you use the following to fully automate the script:

  • Create a PowerShell file say restore.ps1 with PowerShell variables and some other functions which helps you with restore.

  • Use -NonInteractive parameter to ensure PowerShell does not present an interactive prompt to the user.

  • Create a new Task in Task Scheduler with additional arguments (As seen in the image below)

enter image description here

Your restore.ps1 will look like this (note this script is for restoring a sample web application, but change it for your own purpose):

Add-PSSnapin Microsoft.SharePoint.PowerShell

# assignment collection for resource management
Start-SPAssignment -Global

# set up your variables
$farmuser = "MyDomain\dev_sp_Farm"
$farmpassword = "pass@word4"
$directory = "C:\backuppath"
$webapp = "http://test.dev.int"

# use the "ConvertTo-SecureString" function to create a variable with the password converted to "SecureString" type
$securefarmpassword = ConvertTo-SecureString $farmpassword -AsPlainText -Force

# use the $farmuser variable and the $securefarmpassword variable to create a PsCredential object
$farmcred = New-Object System.Management.Automation.PsCredential $farmuser, $securefarmpassword

# pass your variables into the Restore-SPFarm command
Restore-SPFarm -Directory $directory -RestoreMethod Overwrite -Item $webapp -FarmCredentials $farmcred –Verbose

# dispose of objects
Stop-SPAssignment -Global

Information on how to create a tasks scheduler: http://blogs.technet.com/b/heyscriptingguy/archive/2012/08/11/weekend-scripter-use-the-windows-task-scheduler-to-run-a-windows-powershell-script.aspx

  • Is this tested as it was already suggested to be tried and the results came back as it didn't work. This was due to not all information being given that Restore-SPFarm requires ie the "New web application name:" – Hugh Wood May 28 '13 at 8:15
  • Yeah, that's why I took time to post everything in detail. – Falak Mahmood May 28 '13 at 8:19
  • Thank you for taking the time to compose this answer, @FalakMahmood. Unfortunately, -Noninteractive doesn't change the issue and the code above does not work for me. Even including -Confirm:$false it tries to prompt for more input. Running interactively, it's showing me "Confirm: Warning: All selected items will be overwritten... (y/n)?" Also, I'm pretty sure I need -RestoreMethod New to restore my QA environment, not my production one, and it prompts for additional names and so forth. – Nathan May 28 '13 at 21:56
  • I have no clue what are you getting as errors/Additional details until you post it. Could you post a picture? Second, have you tried running it as a simple new task? – Falak Mahmood May 29 '13 at 7:46
  • Are you also using -RestoreMethod Overwrite to overwrite? – Falak Mahmood May 29 '13 at 7:47
0

Thanks for the attempts, guys. The answer I've come to is

Use a different backup method if you need to script and schedule the restore process.

I have successfully implemented a solution backing up and restoring the content databases using only SQL tools, and I have tested a solution using Backup-SPSite and Restore-SPSite on individual site collections.

This is better anyway, because I don't think I really need to replicate all the service applications and other plumbing into the QA environment. I got it initially very production-like using Restore-SPFarm interactively and that's good enough.

The central part of my restore script, which mostly follows Allen White's great article on scripting SQL Server 2012 backup/restore with PowerShell, is:

    Invoke-Sqlcmd  -Database master -Query "alter database $dbname set offline with rollback immediate"

    Restore-SqlDatabase -ServerInstance $qa_host -Database $dbname -BackupFile $fullfile.FullName -ReplaceDatabase -NoRecovery
    Restore-SqlDatabase -ServerInstance $qa_host -Database $dbname -BackupFile $difffile.FullName -ReplaceDatabase

    Set-SPContentDatabase -Identity $db -Status Online
    Invoke-Sqlcmd  -Database master -Query "alter database $dbname set online"

Scripting Backup-SPSite is even easier, something like this on my app server to back up all the sites to a snapshotted storage appliance:

Add-PSSnapin "Microsoft.SharePoint.PowerShell"
$backupdir = '\\backups\MOSS2013_granular_sites'

foreach ($site in Get-SPSite) {
    $backfile = $backupdir + '\' + $site.url.replace('http://','').replace('/', '_') + '.bak'
    echo "Backing up $site to $backfile"
    Backup-SPSite $site.url -Path $backfile -Force -Confirm:$false
}

And the restore on the QA server would look like

Restore-SPSite $qa_url -DatabaseName $qa_content_db -Path "$backupdir\$qa_root_bak" -Force -Confirm:$false

Once I get my ducks in a row I'll write a blog post with more complete script examples.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.