Executing a powershell script file, which is a string value

I have a 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 a string variable

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

The problem is that I need to execute this file (pass the argument to the file as well!)

I tried Invoke-Command

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

but it didn’t work, it just prints the file name, how can I execute the file that is in the string variable $ stringFile?

+7
source share
1 answer

Try & :

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

All Articles