是否可以将另一个脚本中的powershell脚本作为变量调用?

EGr*_*EGr 2 parameters powershell parameter-passing powershell-2.0

我有一个Powershell脚本,用户将脚本作为参数传递.传入之后,我无法使用$ scriptvariable调用脚本.当需要从变量调用一个脚本时,有没有办法从另一个Powershell脚本中调用Powershell脚本.

param(
  [string]hostval,
  [string]$scriptpath
)

Invoke-Command -Computer $hostval -Scriptblock { $scriptpath } -credential $cred
Run Code Online (Sandbox Code Playgroud)

这不起作用,我不确定我想要的是否可能.是否有可以使用的参数类型(例如:[script] $ scriptpath),因此可以从$ scriptpath调用脚本?

mjo*_*nor 5

听起来你需要使用-FilePath参数,而不是-Scriptblock:

-FilePath <String>
    Runs the specified local script on one or more remote computers. Enter the path and file name of the script, or pipe a script path to Invoke-Command. The script must reside on the local computer or in a directory that the local computer can access. Use the ArgumentList parameter to specify the values of parameters in the script.
Run Code Online (Sandbox Code Playgroud)