How to pass arguments or variables from one powershell script to another?

In cmd, I could pass arguments from one bit to another, listing them after the bat-run-run file was specified. Then the future bat received them as% 1,% 2,% 3, etc. Can this be done in Powershell?

I have one ps1 script, script1, which asks the user for a location. The script took care of this. This place is stored as a variable; $ Loc. In the first script, there is a point at which the user can choose an option that will run another ps1 script, script2, which has more options. I want to pass $ loc to script2 from script1.

In script1, I tried the following:

param ($loc) start-process "\script2.ps1" -ArgumentList $loc start-process "\script2.ps1" -$loc start-process "\script2.ps1" 

Then script 2

 args[0] $loc 

I know that I probably just don't understand the passing arguments. The fact is that other options are called bat script. The one I use is -ArgumentList $ loc, and it conveys this beautiful. I select this argument in bat script using "Set loc =% 1"

+5
source share
3 answers

You do not need Start-Process to run one PowerShell script from another PowerShell script. Just call the second script with any parameters you want:

 # script1.ps1 $loc = Read-Host 'Enter location' C:\path\to\script2.ps1 $loc 'other parameter' 

In the second script, a list of arguments can be obtained, for example, through the $args array:

 # script2.ps1 Write-Host $args[0] Write-Host $args[1] 

You can also define named parameters as follows:

 # script2.ps1 Param($Location, $Foo) Write-Host $Location Write-Host $Foo 

or (more complete):

 # script2.ps1 [CmdletBinding()] Param( [Parameter(Mandatory=$true)] [string]$Location, [Parameter(Mandatory=$false)] [string]$Foo ) Write-Host $Location Write-Host $Foo 

Defining named parameters allows you to pass arguments without worrying about their order:

 C:\path\to\script2.ps1 -Foo 'other parameter' -Location $loc 

or automatically have parameters checked without having to perform checks in the function body:

 # script2.ps1 Param( [ValidateSet('a', 'b', 'c')] [string]$Location, [ValidatePattern('^[az]+$')] [string]$Foo ) Write-Host $Location Write-Host $Foo 

If more arguments are passed than named parameters are defined, additional arguments are stored in the $args array:

  PS C: \> cat test.ps1
 Param ($ Foo)

 Write-Host $ Foo
 Write-Host $ args [0]
 PS C: \> . \ Test.ps1 'foo' 'bar'
 foo
 bar 

For more information, see Get-Help about_Functions_Advanced_Parameters .

+9
source
 param ($loc) 

At the top of the .ps1 script, which defines the parameter for the script, so name it

  PathToMyScript \ MyScript.ps1 -loc ValueOfLoc

All attributes that can be applied , including the use of [CmdletBinding()] in the param operator in a script function, as well.

+1
source

Variables declared in Variables.ps1 are in the "Script Scope". That is, you cannot see them outside the scope of the script that declares them. One way to transfer variables in Variables.ps1 to the main.ps1 area is through "dot source" Variables.ps1. This essentially launches Variables.ps1 in the main.ps1 area. To do this, simply insert a period and a space before calling the script:

 . .\Variables.ps1 $var1 $var2 
-1
source

Source: https://habr.com/ru/post/1206147/


All Articles