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 ?
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.
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.
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.
Try &
:
$foo = ".\Get-ProcesWithParam.ps1"
$bar="iexplore"
& $foo $bar
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With