I'm trying to stop a remote service using this Powershell script:
$hostname1 = "myhost"
$serviceName = "myservice"
Write-Host "Hostname: $hostname1"
Write-Host "Service name: $serviceName"
Invoke-Command -ComputerName $hostname1 -ScriptBlock {
Stop-Service -Name $serviceName -Force
}
But getting this error message:
Cannot bind argument to parameter 'Name' because it is null.
+ CategoryInfo : InvalidData: (:) [Stop-Service], ParameterBindin
gValidationException
+ FullyQualifiedErrorId : ParameterArgumentValidationErrorNullNotAllowed,M icrosoft.PowerShell.Commands.StopServiceCommand
If I remove Invoke-Command
from the script, I can see the variables are initialized correctly:
$hostname1 = "myhost"
$serviceName = "myservice"
Write-Host "Hostname: $hostname1"
Write-Host "Service name: $serviceName"
Returns:
Hostname: myhost
Service name: myservice
What can be a reason for this?
Thanks, Roman