执行powershell脚本文件,这是一个字符串值

Jee*_*van 7 powershell

我有脚本文件Get-ProcesWithParam.ps1 as

 param(
 $name
 )

 function List-Process($name)
 {
    Write-Host "Process List"
    get-process -name $name
    #get-process

}

List-Process -name $name
Run Code Online (Sandbox Code Playgroud)

在另一个脚本中,我将此文件名作为字符串变量

$scriptFile = Get-ExecFile # returns "C:\Get-ProcesWithParam.ps1"
#execute the script
# ... other code ...
Run Code Online (Sandbox Code Playgroud)

问题是我需要执行此文件(也将参数传递给文件!)

我试过Invoke-Command

 invoke-command -scriptblock { param($name) $scriptFile -name $name } -ArgumentList "chrome"
Run Code Online (Sandbox Code Playgroud)

但它没有用,它只是打印文件名,我怎么能执行字符串变量$ stringFile中的文件?

Oca*_*tal 12

试试&:

$foo = ".\Get-ProcesWithParam.ps1"
$bar="iexplore"
& $foo $bar
Run Code Online (Sandbox Code Playgroud)