Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Executing powershell script file which is a string value

Tags:

powershell

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!)

I tried Invoke-Command

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

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 ?

like image 297
Jeevan Avatar asked May 27 '11 07:05

Jeevan


People also ask

What is a string value in PowerShell?

A string in PowerShell is simply an object with that type. Let's start by using the command Get-Location, and using the variable $ourPath to hold that object. We'll then get the value of the variable, and see what object type is returned. $ourPath = Get-Location.

How do I get the content of a text file in PowerShell?

The Get-Content cmdlet is a very popular PowerShell cmdlet that will retrieve all text from a text file specified by the Path parameter. At it's simplest, you can pass the Path parameter with the file path to a text file as the argument to the Get-Content cmdlet.

How do you reference a variable in a string in PowerShell?

PowerShell has another option that is easier. You can specify your variables directly in the strings. $message = "Hello, $first $last." The type of quotes you use around the string makes a difference.


1 Answers

Try &:

$foo = ".\Get-ProcesWithParam.ps1"
$bar="iexplore"
& $foo $bar
like image 65
Ocaso Protal Avatar answered Nov 08 '22 13:11

Ocaso Protal