Search code examples
powershellinvoke-command

Invoke-Command Cannot Bind Argument Error


Is Invoke-Command not pulling my variables?

Im caught on this one. I could use an xtra set of eyes!

Im pulling services from a remote machine and assigning them by number then passing a stop / start to the remote machine based off user input. Im getting a argument on my variable.

Please forgive the code setup Im new and I write then I clean. Some services and names have been removed for privacy.

Code::

$prepend = "ssssssssss"
$append = "sss"
$Fprepend = "tttttttt"
$Fappend = "tt"
$sitenumber = Read-Host 'What is the site number? ex. 1111'
 $name = $prepend + $sitenumber + $append  
 $Fname = $Fname = $Fprepend + $sitenumber + $Fappend

      $global:i=0
Get-service -Name Service,Instance,Server,Integration,Data,Message,FTP,Provider -ComputerName $name |
Select @{Name="Item";Expression={$global:i++;$global:i}},Name -OutVariable menu | Format-Table -AutoSize

$r = Read-Host "Select a service to restart by number"
$svc = $menu | where {$_.item -eq $r}

Write-Host "Restarting $($svc.name)" -ForegroundColor Green

Invoke-Command -ComputerName $Fname -ScriptBlock {Stop-Service -Name $svc.name -Force}
 sleep 3
Invoke-Command -ComputerName $Fname -ScriptBlock {Start-Service -Name $svc.name -Force}
Get-service -Name $svc.name -Computername $name

Error::

Cannot bind argument to parameter 'Name' because it is null. + CategoryInfo : InvalidData: (:) [Stop-Service], ParameterBindingValidationException + FullyQualifiedErrorId : ParameterArgumentValidationErrorNullNotAllowed,Microsoft.PowerShell.Commands.StopServiceCommand

Cannot bind argument to parameter 'Name' because it is null. + CategoryInfo : InvalidData: (:) [Start-Service], ParameterBindingValidationException + FullyQualifiedErrorId : ParameterArgumentValidationErrorNullNotAllowed,Microsoft.PowerShell.Commands.StartServiceCommand


Solution

  • I have modified the code and now its working fine. The issue what you are facing is because inside the script block , $svc is not holding any value because its not in the scope even. To make it within the scope you have to pass as an ArgumentList and it has to be initiated inside the block as param. Thats why ou are getting as Null

    Use the below code. I just modified the Invoke part

    $prepend = "ssssssssss"
    $append = "sss"
    $Fprepend = "tttttttt"
    $Fappend = "tt"
    $sitenumber = Read-Host 'What is the site number? ex. 1111'
     $name = $prepend + $sitenumber + $append  
     $Fname = $Fname = $Fprepend + $sitenumber + $Fappend
    
          $global:i=0
    Get-service -Name Service,Instance,Server,Integration,Data,Message,FTP,Provider -ComputerName $name |
    Select @{Name="Item";Expression={$global:i++;$global:i}},Name -OutVariable menu | Format-Table -AutoSize
    
    $r = Read-Host "Select a service to restart by number"
    $svc = $menu | where {$_.item -eq $r}
    
    Write-Host "Restarting $($svc.name)" -ForegroundColor Green
    # You have to pass the $svc as an argumentlist and the same has to be initiated as param inside the script block.
    Invoke-Command -ComputerName $Fname -ScriptBlock {param($svc)Stop-Service -Name $svc.name -Force} -ArgumentList $svc
     sleep 3
    Invoke-Command -ComputerName $Fname -ScriptBlock {param($svc)Start-Service -Name $svc.name -Force} -ArgumentList $svc
    Get-service -Name $svc.name -Computername $name
    

    Hope you understand the issue now.