Powershell - Cannot bind argument to parameter 'Name' because it is null
<p>I'm trying to stop a remote service using this Powershell script:</p>

<pre><code>$hostname1 = "myhost"
$serviceName = "myservice"
Write-Host "Hostname: $hostname1"
Write-Host "Service name: $serviceName"
Invoke-Command -ComputerName $hostname1 -ScriptBlock {
  Stop-Service -Name $serviceName -Force
}
</code></pre>

<p>But getting this error message:</p>

<pre><code>Cannot bind argument to parameter 'Name' because it is null.
    + CategoryInfo          : InvalidData: (:) [Stop-Service], ParameterBindin
   gValidationException
    + FullyQualifiedErrorId : ParameterArgumentValidationErrorNullNotAllowed,M icrosoft.PowerShell.Commands.StopServiceCommand
</code></pre>

<p>If I remove <code>Invoke-Command</code> from the script, I can see the variables are initialized correctly:</p>

<pre><code>$hostname1 = "myhost"
$serviceName = "myservice"
Write-Host "Hostname: $hostname1"
Write-Host "Service name: $serviceName"
</code></pre>

<p>Returns:</p>

<pre><code>Hostname: myhost
Service name: myservice
</code></pre>

<p>What can be a reason for this?</p>

<p>Thanks,
Roman</p>