Powershell:如何远程传递带有invoke-command和-filepath的参数? [英] Powershell: How to pass parameter with invoke-command and -filepath remotely?
问题描述
因此,我发现了很多与此类似的主题,但是找不到与我的问题相关的任何东西.
so there's a lot of similar topics to this I've found, but I can't find anything that correlates with my issue.
invoke-command -computername $serverLocation -Credential $cred -filepath "C:\path\script2.ps1" -ArgumentList $configPath
我正在为存储在我的本地计算机上的脚本调用,以在另一台服务器上运行.除了传递"configPath"变量外,其他所有操作均按预期进行.这就是我启动脚本的方式-
I am calling a for a script that is stored on my local machine to run on another server. Everything works as intended except for passing the "configPath" variable. This is how I initiate the script -
. .\script1.ps1 -serverLocation 'serverNameHere' -username 'userNameHere' -configPath 'configPathHere'
它正确地捕获了我传递给第一个脚本的所有其他参数.它只是不会将configPath传递给文件路径"中的第二个脚本.
It correctly grabs all the other parameters I pass into the first script. It just won't pass the configPath to the second script in the 'filepath'.
如何获取此参数?非常感谢.
How can I get this parameter to pass through? Thanks so much.
推荐答案
在script2.ps1
中,您应该定义一个接受参数的param()
块,然后您可以使用提供的任何名称来引用它.如果您不想使用param()
块,则可以使用$args[0]
.
In script2.ps1
, you should define a param()
block that accepts the parameter, and then you can refer to it by whatever name you give it. If you don't want to use a param()
block then using $args[0]
should work.
这篇关于Powershell:如何远程传递带有invoke-command和-filepath的参数?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!