使用远程会话调用命令:无法验证参数的参数
Invoke-Command with remote session: Cannot validate argument on parameter
我写了一个脚本来重启远程服务器上的几个 ASP.NET 网站:
$computerName = #...
$password = #...
$secureStringPassword = ConvertTo-SecureString -AsPlainText -Force -String $password
$userName = #...
$credential= New-Object System.Management.Automation.PSCredential ($userName, $secureStringPassword)
$websiteNames = #..., #..., #...
Get-PSSession -ComputerName $computerName -Credential $credential | Remove-PSSession
$psSession = New-PSSession -ComputerName $computerName -Credential $credential
Invoke-Command -Session $psSession -ScriptBlock { $websiteNames | foreach{ Stop-Website -Name $_ } }
Invoke-Command -Session $psSession -ScriptBlock { $websiteNames | foreach{ Start-Website -Name $_ } }
$psSession | Remove-PSSession
由于某些原因,我的 Invoke-Command
没有正确 运行,我收到以下错误消息:
Cannot validate argument on parameter 'Name'. The argument is null. Provide a valid value for the argument, and then try running the command again.
当命令在 Enter-PSSession
之后 运行 它在 -ScriptBlock
中工作正常它有点搞乱 -Name
参数,知道如何解决这个问题?
实际上只需要将参数传递给 -ScriptBlock
的 -ArgumentList
并使用 $args
在功能块中引用它:
Invoke-Command -Session $psSession -ScriptBlock { $args | foreach{ Stop-Website -Name $_ } } -ArgumentList $websiteNames
Invoke-Command -Session $psSession -ScriptBlock { $args | foreach{ Start-Website -Name $_ } } -ArgumentList $websiteNames
远程会话无法访问您在本地定义的变量。它们可以用 $using:variable
来引用
Invoke-Command -Session $psSession -ScriptBlock { $using:websiteNames | foreach{ Stop-Website -Name $_ } }
Invoke-Command -Session $psSession -ScriptBlock { $using:websiteNames | foreach{ Start-Website -Name $_ } }
about_remote_variables
帮助中的更多信息:
get-help about_remote_variables -Full
我写了一个脚本来重启远程服务器上的几个 ASP.NET 网站:
$computerName = #...
$password = #...
$secureStringPassword = ConvertTo-SecureString -AsPlainText -Force -String $password
$userName = #...
$credential= New-Object System.Management.Automation.PSCredential ($userName, $secureStringPassword)
$websiteNames = #..., #..., #...
Get-PSSession -ComputerName $computerName -Credential $credential | Remove-PSSession
$psSession = New-PSSession -ComputerName $computerName -Credential $credential
Invoke-Command -Session $psSession -ScriptBlock { $websiteNames | foreach{ Stop-Website -Name $_ } }
Invoke-Command -Session $psSession -ScriptBlock { $websiteNames | foreach{ Start-Website -Name $_ } }
$psSession | Remove-PSSession
由于某些原因,我的 Invoke-Command
没有正确 运行,我收到以下错误消息:
Cannot validate argument on parameter 'Name'. The argument is null. Provide a valid value for the argument, and then try running the command again.
当命令在 Enter-PSSession
之后 运行 它在 -ScriptBlock
中工作正常它有点搞乱 -Name
参数,知道如何解决这个问题?
实际上只需要将参数传递给 -ScriptBlock
的 -ArgumentList
并使用 $args
在功能块中引用它:
Invoke-Command -Session $psSession -ScriptBlock { $args | foreach{ Stop-Website -Name $_ } } -ArgumentList $websiteNames
Invoke-Command -Session $psSession -ScriptBlock { $args | foreach{ Start-Website -Name $_ } } -ArgumentList $websiteNames
远程会话无法访问您在本地定义的变量。它们可以用 $using:variable
Invoke-Command -Session $psSession -ScriptBlock { $using:websiteNames | foreach{ Stop-Website -Name $_ } }
Invoke-Command -Session $psSession -ScriptBlock { $using:websiteNames | foreach{ Start-Website -Name $_ } }
about_remote_variables
帮助中的更多信息:
get-help about_remote_variables -Full