locked
Variable binding error for Set-Service cmdlet RRS feed

  • Question

  • Hello,

    When I execute the following command from a script

    invoke-command -<g class="gr_ gr_90 gr-alert gr_spell gr_inline_cards gr_disable_anim_appear ContextualSpelling" data-gr-id="90" id="90">computername</g> $Servername -ScriptBlock {Set-Service -Name $name -Status Running} -Credential $MyCredentials

    I get the following error:

    Cannot bind argument to parameter 'Name' because it is null.

        + CategoryInfo          : InvalidData: (:) [Set-Service], ParameterBindingValidationException

        + FullyQualifiedErrorId : ParameterArgumentValidationErrorNullNotAllowed,Microsoft.PowerShell.Commands.SetServiceCommand

        + PSComputerName        : xxxxxxxxx

     

    Variables $name has a value ( I print the variable $name just before executing the command) of the service that I want to start on $Servername. If I remover the variable and hard code the service name it works. So, I know it's not an access issue. 

    Any guidance will be appreciated!

    -Jai

     
    Thursday, February 28, 2019 6:08 PM

Answers

  • The "inside" of the scriptblock does not know about the variables "outside" of the scriptblock by default. There are some options to come around this: Get-Help about_Remote_Variables.

    Regardless of that: Please disable or update your grammarly. It makes your text unreadable. And please do not post pre colored code. Use the code posting tool instead provided on the edit bar of the post editor. Thanks


    Live long and prosper!

    (79,108,97,102|%{[char]$_})-join''



    • Edited by BOfH-666 Thursday, February 28, 2019 9:51 PM
    • Marked as answer by JC75082 Tuesday, March 5, 2019 5:03 PM
    Thursday, February 28, 2019 9:49 PM

All replies

  • The "inside" of the scriptblock does not know about the variables "outside" of the scriptblock by default. There are some options to come around this: Get-Help about_Remote_Variables.

    Regardless of that: Please disable or update your grammarly. It makes your text unreadable. And please do not post pre colored code. Use the code posting tool instead provided on the edit bar of the post editor. Thanks


    Live long and prosper!

    (79,108,97,102|%{[char]$_})-join''



    • Edited by BOfH-666 Thursday, February 28, 2019 9:51 PM
    • Marked as answer by JC75082 Tuesday, March 5, 2019 5:03 PM
    Thursday, February 28, 2019 9:49 PM
  • Thanks!
    Tuesday, March 5, 2019 4:33 PM
  • That solved the issue. 
    Tuesday, March 5, 2019 5:03 PM