none
Parameter 'ServerInstance' is null or empty in -ScriptBlock...

    Question

  • Hi all,

    I have a script that remotely restores a database on a test server.  Here is where it is failing...

    Invoke-Command -ComputerName $TestDbServer -ScriptBlock { Restore-SqlDatabase -ServerInstance $TestDbServer -Database MyDatabase -BackupFile "C:\DbBackup\MyDatabase.bak" } -Credential $credentia

    The error I'm getting is 

    If I change the above script and hard code the actual Server Instance name, the restore succeeds...

    Invoke-Command -ComputerName $TestDbServer -ScriptBlock { Restore-SqlDatabase -ServerInstance TEST-MACHINE-1 -Database MyDatabase -BackupFile "C:\DbBackup\MyDatabase.bak" } -Credential $credential                                                                              

    So, why is the variable passed in containing the server name seen as null in the script block?  Is that a no-no of some sort in PowerShell?  Do I need to escape it with some character(s)?

    Any help Greatly Appreciated!


    Thursday, December 6, 2018 7:04 PM

Answers

  • I also recommend that you use the following method for formatting code as it will help you understand how the code is intended to work.'

    $sb = {
        param ($rsv)
        Restore-SqlDatabase -ServerInstance $srv -Database MyDatabase -BackupFile C:\DbBackup\MyDatabase.bak
    }
    
    Invoke-Command -ScriptBlock $sb -ArgumentList $TestDbServer -ComputerName $TestDbServer -Credential $credential   

    It is now very easy to see that you misspelled the parameter name.  Fix that and you are good to go.


    \_(ツ)_/

    Thursday, December 6, 2018 10:03 PM
    Moderator

All replies

  • Just curious try this 

    "$TestDbServer"

    Thursday, December 6, 2018 7:11 PM
  • Think I tried "$TestDBServer" and '$TestDBServer' and I believe both failed, but will try again.

    UPDATE:  Yep, that failed too.

    • Edited by NitLions Thursday, December 6, 2018 7:13 PM More info
    Thursday, December 6, 2018 7:12 PM
  • You have to pass all external values as arguments.

    Read all of the help for Invoke-Command carefully to learn how to pass arguments.

    help invoke-command -ShowWindow

    Quoting variables is seldom an issue and is a bad habit to get into.

    See:

    help about_quoting

    When first learning PowerShell it critical that you first read the help for an issue before asking others.


    \_(ツ)_/

    Thursday, December 6, 2018 8:46 PM
    Moderator
  • OK, so I have to use ArgumentList to pass my server name into the script block and I've seen some examples of that, however the  example just had just the script block.

    I converted my script block to this...

    { (param $srv) Restore-SqlDatabase -ServerInstance $TestDbServer -Database MyDatabase -BackupFile "C:\DbBackup\MyDatabase.bak" }

    How or where do I pass in -ArgumentList $TestDbServer?

    I've tried after the script block, at the very end after the credential parameter, but nothing seems to work.

    Thursday, December 6, 2018 9:41 PM
  • Go back and read the help.  The examples are explicit and better than I can do.

    You are still not passing the argument.

    help invoke-command -Parameter ArgumentList

    Keep reading this until you understand what it is telling you. Don't just quickly skim it.


    \_(ツ)_/

    Thursday, December 6, 2018 9:46 PM
    Moderator
  • I'm just not getting it.  I think I know how to pass the param into the script block with -ArgumentList, but I don't know if that applies when the overall command contains more than just the block.

    What I have now and moving ArgumentList around isn't working...

    Invoke-Command -ComputerName $TestDbServer -ScriptBlock { param($srv) Restore-SqlDatabase -ServerInstance $srv -Database MyDatabase -BackupFile ""C:\DbBackup\MyDatabase.bak"" } -ArgumentList $TestDbServer -Credential $credential     

    Stumped!                                                                                        

    Actually, the error now sees -backupfile as null.  And I see I had ""  "" around that from other stuff I was trying.

    I'll try again.

    • Edited by NitLions Thursday, December 6, 2018 10:05 PM
    Thursday, December 6, 2018 9:56 PM
  • I also recommend that you use the following method for formatting code as it will help you understand how the code is intended to work.'

    $sb = {
        param ($rsv)
        Restore-SqlDatabase -ServerInstance $srv -Database MyDatabase -BackupFile C:\DbBackup\MyDatabase.bak
    }
    
    Invoke-Command -ScriptBlock $sb -ArgumentList $TestDbServer -ComputerName $TestDbServer -Credential $credential   

    It is now very easy to see that you misspelled the parameter name.  Fix that and you are good to go.


    \_(ツ)_/

    Thursday, December 6, 2018 10:03 PM
    Moderator
  • I got it going and yes, I like your code layout better!  THANK YOU!  THANK YOU!! THANK YOU!!!
    Thursday, December 6, 2018 10:16 PM
  • Great!

    This can be helpful as you learn PowerShell:

    1. PowerShell Style Guidelines


    \_(ツ)_/

    Thursday, December 6, 2018 10:17 PM
    Moderator