使用远程会话调用命令:无法验证参数上的参数 [英] Invoke-Command with remote session: Cannot validate argument on parameter

查看:60
本文介绍了使用远程会话调用命令:无法验证参数上的参数的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我编写了一个脚本来重新启动远程服务器上的一些 ASP.NET 网站:

I wrote a script to restart a few ASP.NET websites on a remote server:

$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 无法正常运行,出现以下错误消息:

For some reasons my Invoke-Command do not run properly, I have the following error message:

无法验证参数名称"上的参数.参数为空.为参数提供一个有效值,然后再次尝试运行该命令.

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 参数,知道如何解决这个问题吗?

When the commands are run after an Enter-PSSession it works fine within a -ScriptBlock it kinda mess up the -Name parameter, any idea how to fix that up?

推荐答案

远程会话无法访问您在本地定义的变量.它们可以通过 $using:variable

The remote session cannot access the variables you have defined locally. They can be referenced with $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

这篇关于使用远程会话调用命令:无法验证参数上的参数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆