调用命令无法绑定参数错误
Invoke-Command Cannot Bind Argument Error
Invoke-Command 没有拉取我的变量吗?
我抓住了这个。我可以使用额外的眼睛!
我从远程机器中提取服务并按编号分配它们,然后根据用户输入将停止/启动传递给远程机器。我对我的变量有争论。
请原谅代码设置我是新的,我先写再清理。出于隐私考虑,一些服务和名称已被删除。
代码::
$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
错误::
无法将参数绑定到参数 'Name',因为它为空。
+ CategoryInfo : InvalidData: (:) [停止服务], ParameterBindingValidationException
+ FullyQualifiedErrorId : ParameterArgumentValidationErrorNullNotAllowed,Microsoft.PowerShell.Commands.StopServiceCommand
无法将参数绑定到参数 'Name',因为它为空。
+ CategoryInfo : InvalidData: (:) [Start-Service], ParameterBindingValidationException
+ FullyQualifiedErrorId : ParameterArgumentValidationErrorNullNotAllowed,Microsoft.PowerShell.Commands.StartServiceCommand
我修改了代码,现在可以正常工作了。
您面临的问题是因为在脚本块内,$svc 没有任何价值,因为它甚至不在范围内。要使其在范围内,您必须作为 ArgumentList 传递,并且必须在块内作为 param 启动。这就是为什么你得到 Null
使用下面的代码。我刚刚修改了Invoke部分
$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
希望你现在明白了这个问题。
Invoke-Command 没有拉取我的变量吗?
我抓住了这个。我可以使用额外的眼睛!
我从远程机器中提取服务并按编号分配它们,然后根据用户输入将停止/启动传递给远程机器。我对我的变量有争论。
请原谅代码设置我是新的,我先写再清理。出于隐私考虑,一些服务和名称已被删除。
代码::
$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
错误::
无法将参数绑定到参数 'Name',因为它为空。 + CategoryInfo : InvalidData: (:) [停止服务], ParameterBindingValidationException + FullyQualifiedErrorId : ParameterArgumentValidationErrorNullNotAllowed,Microsoft.PowerShell.Commands.StopServiceCommand
无法将参数绑定到参数 'Name',因为它为空。 + CategoryInfo : InvalidData: (:) [Start-Service], ParameterBindingValidationException + FullyQualifiedErrorId : ParameterArgumentValidationErrorNullNotAllowed,Microsoft.PowerShell.Commands.StartServiceCommand
我修改了代码,现在可以正常工作了。 您面临的问题是因为在脚本块内,$svc 没有任何价值,因为它甚至不在范围内。要使其在范围内,您必须作为 ArgumentList 传递,并且必须在块内作为 param 启动。这就是为什么你得到 Null
使用下面的代码。我刚刚修改了Invoke部分
$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
希望你现在明白了这个问题。