locked
Restore-SqlDatabase fails when specifying -Credential RRS feed

  • Question

  • Hi

    Im getting an error when running the below PowerShell. I need to run as a specific user, any ideas? I have tried replacing localhost for the server name but still no luck. The error i get is:

    Restore-SqlDatabase : Failed to connect to server localhost.

    $password = ConvertTo-SecureString '*********' -AsPlainText -Force
    
    $cred = New-Object System.Management.Automation.PSCredential ('testadmin', $password)
    
    #Relocate the Database file
    
    $RelocateData = New-Object Microsoft.SqlServer.Management.Smo.RelocateFile("DB_training", "S:\Data\Testing.mdf")
    
    #Relocate the Log file
    
    $RelocateLog = New-Object Microsoft.SqlServer.Management.Smo.RelocateFile("DB_training_Log", "L:\Log\Testing.ldf")
    
    #Restore the template database
    
    Restore-SqlDatabase -ServerInstance "localhost" -Database "Testing" -Credential $cred -BackupFile "C:\Template\backup_V7_201810011430.bak" -RelocateFile @($RelocateData,$RelocateLog)
    
    
    

    Thanks

    Shane


    Friday, July 5, 2019 2:55 PM

Answers

  • Thanks for the response.

    I mentioned above that i have tried changing localhost for the server name.

    I figured out what was causing this, i have to specify -SQLCredential and not -credential and it worked.

    Thanks

    Shane

    Monday, July 8, 2019 8:36 AM

All replies

  • There is no such server instance named "localhost". You need to use the actual instance name.  The default instance is the local computer name.

    Test like this:

    $c = Invoke-SqlCmd -Query 'select * from syscolumns'

    This will test connectivity to the local server default instance.  To test with alternate credentials.

    $c = Invoke-SqlCmd -Query 'select * from syscolumns' -Credential $cred


    \_(ツ)_/

    Friday, July 5, 2019 6:42 PM
  • Thanks for the response.

    I mentioned above that i have tried changing localhost for the server name.

    I figured out what was causing this, i have to specify -SQLCredential and not -credential and it worked.

    Thanks

    Shane

    Monday, July 8, 2019 8:36 AM
  • Hi,

    Was your issue resolved?

    If you resolved it using our solution, please "mark it as answer" to help other community members find the helpful reply quickly.

    If you resolve it using your own solution, please share your experience and solution here. It will be very beneficial for other community members who have similar questions.

    If no, please reply and tell us the current situation in order to provide further help.

    Best Regards,

    Lee


    Just do it.

    Wednesday, July 31, 2019 8:30 AM