执行作为字符串值的 powershell 脚本文件 [英] Executing powershell script file which is a string value

查看:51
本文介绍了执行作为字符串值的 powershell 脚本文件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I have script file Get-ProcesWithParam.ps1 as

 param(
 $name
 )

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

}

List-Process -name $name

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

In another script I get this file name as string variable

$scriptFile = Get-ExecFile # returns "C:\Get-ProcesWithParam.ps1"
#execute the script
# ... other code ...

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

problem is i need to execute this file (pass the argument to file as well!)

我试过调用命令

 invoke-command -scriptblock { param($name) $scriptFile -name $name } -ArgumentList "chrome"

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

but it did not work, it just prints the file name how can i execute the file which is there in the string variable $stringFile ?

推荐答案

试试&:

$foo = ".\Get-ProcesWithParam.ps1"
$bar="iexplore"
& $foo $bar

这篇关于执行作为字符串值的 powershell 脚本文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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