Passing Arguments between script files - powershell

I need to pass a few arguments from one script in file to another one. I load path to my current script file to variable and add name, arguments of other script that I want to call.
Here is the sample of calling and passing argument that I got in Script1.ps1:
Param([string]$argument)
$thisScript = Split-Path -Path $MyInvocation.MyCommand.Definition -Parent
.($thisScript+'\anotherScript.ps1 -passedArgument '+$argument)
Here is the part of the script Script2.ps1 that I'm calling:
Param([string]$passedArgument)
$passedArgument = "do some work with it HERE"
When I start the first script like this
C:\Users\user1\Desktop\Script1.ps1 -argument datatopass
it writes the error
The term 'C:\Users\user1\Desktop\Script2.ps1 -passedArgument datatopass' is not recognized as the name of a cmdlet, function, script file, or operable program. Check the spelling of the name, or if a path was included, verify that the path is correct and try again.
When I try use the script manually like this
C:\Users\user1\Desktop\Script2.ps1 -passedArgument datatopass
it works fine and doesn't report any error with wrong path or name.
I don't know where the problem is, and I couldn't find anything about this error.

You dont have to concat the passedArgument with its value to a string. Try:
& (Join-Path $thisScript 'anotherScript.ps1') -passedArgument $argument

Related

Get-Content splits path with spaces in powershell

I want to get the content of a simple text file with powershell.
This is my code:
$versionPath = "$PSScriptRoot/../VERSION"
Get-Content $versionPath -Raw
I get this error:
C:\Users\Rik : The term 'C:\Users\Rik' is not recognized as the name of a cmdlet, function, script file, or operable
program. Check the spelling of the name, or if a path was included, verify that the path is correct and try again.
At line:1 char:1
+ C:\Users\Rik van\source\repos\
+ FullyQualifiedErrorId : CommandNotFoundException
Get-Content splits the path by spaces, I tried the suggestions here
but nothing seems to work. any suggestions?
My spidey-sense says you're calling your script on the command line like this:
C:\> powershell C:\Users\Rik van\source\repos\myscript.ps1
The problem isn't the content of your script - it's the path to it.
PowerShell is treating the space as the end of the path to the script, and assumes the rest of the filename is parameters to pass to it. The only problem is it can't find a PowerShell script called C:\Users\Rik.
If you wrap the filename in quotes it'll probably just work:
C:\> powershell "C:\Users\Rik van\source\repos\myscript.ps1"

Trying to escape parenthesis when passing arguments in a PowerShell script

I made a PowerShell script that will send a toast notification via/arguments. In the script, I use the param function in the beginning of the script something like....this
param($UserID)
Now I open a command prompt to load a PowerShell script and adding these arguments
powershell C:\file\in\directory\PowershellScript.ps1 -UserID "Mikey (TEST)"
When I sent the prompt over to PowerShell, it gave me this red error that says
TEST : The term 'TEST' is not recognized as the name of a cmdlet, function,
script file, or operable program. Check the spelling of the name, or if a path
was included, verify that the path is correct and try again.
At line:1 char:64
Assumingly the parentheses ( ) could be the culprit as PowerShell is thinking of loading a module of some sort instead of applying as a string value. I even tried escaping the parentheses using the backslash \(TEST\) but that didn't work either as it said TEST\ : The term 'TEST\' is not recognized as the name of a cmdlet, function, script file, or operable program..
Is there something I'm missing or something part of a script I should add to?
Use single quotes inside the ps command:
powershell C:\file\in\directory\PowershellScript.ps1 -UserID 'Mikey (TEST)'

Import functions in a script with parameters

I have a script with parameters:
param (
[Parameter(Mandatory=$true)][string]$VaultName,
[Parameter(Mandatory=$true)][string]$SecretName,
[Parameter(Mandatory=$true)][bool]$AddToMono = $false
)
...
In this script I want to include functions that I wrote in another ps1 file : common.ps1
I usually import this with
. .\common.ps1
but if I do that in the script I get:
The term '.\common.ps1' is not recognized as the name of a cmdlet, function, script file, or operable program. Check the spelling of the name, or if a path was included, verify that the
path is correct and try again.
How do I import common.ps1 in this script?
Thanks!
The problem is that you are running the script from a different directory. PowerShell is looking for .\common.ps1 using the current directory, not the directory of the script. To get around this, use the built-in variable $PSScriptRoot, which contains the path of the current script. (I'm assuming you are using PowerShell v3.0 or later.)
common.ps1
function foo {
return "from foo"
}
with_params.ps1
param (
[Parameter(Mandatory=$true)][string]$VaultName,
[Parameter(Mandatory=$true)][string]$SecretName,
[Parameter(Mandatory=$true)][bool]$AddToMono = $false
)
. $PSScriptRoot\common.ps1
Write-Output "Vault name is $VaultName"
foo
I then executed this:
PS> .\some_other_folder\with_params.ps1 -VaultName myVault -SecretName secretName -AddToMono $false
and got this output:
Vault name is myVault
from foo

PowerShell - Including .ps1 files - invoke from other folder

Im struggling with a PowerShell challenge, the setup is as follows:
In the C:\update folder I have ReInstall.ps1 powershell script, that will try to run a script in a folder on another drive:
q:\test\install.ps1
In the q:\test folder, I have a Powershell file callled Install.ps1 that tries to include another ps file called InstallFunctions.ps1
. .\installfunctions.ps1
The two Install ps files works nicely when executed from the q:\test\folder.
But if I try to run the ReInstall.ps1 script from the c:\Update folder, it nicely starts the q:\install.ps1, but then fails because it can't find the Installfunctions.ps1.
It tries to find the InstallFunctions.ps1 in the c:\update folder, instead of the q:\test folder.
The term '.\installfunctions.ps1' is not recognized as the name of a cmdlet, >function, script file, or operable program.
Check the spelling of the name, or if a path was included, verify that the path >is correct and try again.
Since the Install ps files are inside a ISO file, and must work in any scenario, I can't change them. So I have to figure out a way to make the Install.ps1 believe it runs from q:\test folder.
You have to retrieve the script folder like this (PS 2.0):
$scriptPath = Split-Path $MyInvocation.MyCommand.Definition
. "$scriptPath\installfunctions.ps1"
#or . (Join-Path $scriptPath "installfunctions.ps1")
In PS 3.0 $PSScriptRoot holds the info you need:
. "$PSScriptRoot\installfunctions.ps1"
#or . (Join-Path $PSScriptRoot "installfunctions.ps1")

How can I use an Environmental Variable as a start of a directory location in powershell?

I was given a powershell script by a coworker to try and figure out. I have very little expereince with it so I've gotten stuck.
We need to pull a user defined cmdlet from a .ps1 file form another part of the drive.
Normally you would do it something like this:
. .\scripts\thing.ps1
But we want to use an environmental variable set in command prompt to start the location. We have something like:
. $Env:JobDir\scripts\thing.ps1
But this returns the error
. : The term '\scripts\thing.ps1' is not recognized as the name of a cmdlet, function, script file, or operable program.
Is there anyway to make something like this work?
You'd need to make it a string, otherwise it's interpreting the entire path as a variable.
"$Env:JobDir\scripts\thing.ps1"
or
$Env:JobDir + "\scripts\thing.ps1"
You can do something like this:
# build the path to the job
$job = Join-path -Path $Env:JobDir -ChildPath "scripts\thing.ps1"
# execute the job
$job
or
.$("$Env:JobDir\scripts\thing.ps1")