Learn how to a build a cloud-first strategyRegister Now

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 2724
  • Last Modified:

How do I pass 2 parameters into a powershell script?

I have two parameters I want to pass into a poweshell script

i.e. PS c:\>.\test.ps1 value 1,value 2
0
SiHodgy007
Asked:
SiHodgy007
  • 4
  • 2
1 Solution
 
Chris DentPowerShell DeveloperCommented:

Without the comma:

test.ps1 value1 value2

Then you can deal with the parameters as you see fit within the script itself.

Chris
0
 
SiHodgy007Author Commented:
how do I call these values in the script say if i just want to echo them?
0
 
Chris DentPowerShell DeveloperCommented:

You have quite a bit of choice about how you deal with parameters.

You can used named or positional, using named is nice if you want someone to use the script, positional is fine if it's nothing more than an internal command.

A few little samples of both here. Each just echoes the parameter using Write-Host.

Chris
# Command to call the scripts below
 
# Named Paramaters
./Test1.ps1 -Param1 "Bob"
./Test1.ps1 -Param1 "Bob" -Param2 "Tim"
./Test1.ps1 -Param2 "Alan"
./Test1.ps1 -Param2 "Beth" -Param3 "Rachel"
 
# Positional Parameters
./Test2.ps1 "Bob" "Tim"
./Test2.ps1 "Bob"
 
# Test1.ps1
Param(
  $Param1 = "Default Value",
  $Param2 = $(Throw "This parameter is required"),
  $Param3)
 
Write-Host "Param1 has a default value. Current value is: $Param1"
Write-Host "Param2 is required. Current value is: $Param2"
Write-Host "Param3 is optional. Current value is: $Param3"
 
# Test2.ps1
Write-Host "Total Arguments: $($Args.Length)"
ForEach ($Arg in $Args) {
  $i++; Write-Host "$i : $Arg"
}

Open in new window

0
Get your Disaster Recovery as a Service basics

Disaster Recovery as a Service is one go-to solution that revolutionizes DR planning. Implementing DRaaS could be an efficient process, easily accessible to non-DR experts. Learn about monitoring, testing, executing failovers and failbacks to ensure a "healthy" DR environment.

 
Chris DentPowerShell DeveloperCommented:

You can use Functions to test these instead of saving things off as scripts. Might just be easier to play around with param there, it works in exactly the same way as with scripts.

They'll copy and paste into the shell and live as long as you keep that shell open.

Chris
# Functions
 
Function Test1 {
  Param(
    $Param1 = "Default Value",
    $Param2 = $(Throw "This parameter is required"),
    $Param3)
 
  Write-Host "Param1 has a default value. Current value is: $Param1"
  Write-Host "Param2 is required. Current value is: $Param2"
  Write-Host "Param3 is optional. Current value is: $Param3"
}
 
Function Test2 {
  Write-Host "Total Arguments: $($Args.Length)"
  ForEach ($Arg in $Args) {
    $i++; Write-Host "$i : $Arg"
  }
}
 
# Command to call the functions above
 
# Named Paramaters
Test1 -Param1 "Bob"
Test1 -Param1 "Bob" -Param2 "Tim"
Test1 -Param2 "Alan"
Test1 -Param2 "Beth" -Param3 "Rachel"
 
# Positional Parameters
Test2 "Bob" "Tim"
Test2 "Bob"

Open in new window

0
 
SiHodgy007Author Commented:
Again Chris I don't understand, I have to pass two parameters from the command line into the script.  for zipping

ie test.ps1 detination sourcefile


$7Zip = 'C:\"Program Files"\7-Zip\7z.exe '
$Archive = "param1"
$Path = "param2"
 
Get-ChildItem $Path | ?{ $_.Name -like "*.txt" } | %{
  Invoke-Expression "$7Zip a -mx=9 $Archive $($_.FullName) "
0
 
Chris DentPowerShell DeveloperCommented:

I would do it like this (with the script below).

Then you would run the command like this:

./Zip.ps1 -File "SomeFile.txt" -Archive "archive.7z"

Using the named parameters like this means it doesn't matter which order you enter the details.

Is that the kind of thing you had in mind?

Chris
# Script Zip.ps1
Param(
  $Archive = $(Throw "Archive Name is required"),
  $File = $(Throw "File to add to Archive is required"))
 
$7Zip = 'C:\"Program Files"\7-Zip\7z.exe'
 
Invoke-Expression "$7Zip a -mx=9 $Archive $File"

Open in new window

0

Featured Post

Veeam and MySQL: How to Perform Backup & Recovery

MySQL and the MariaDB variant are among the most used databases in Linux environments, and many critical applications support their data on them. Watch this recorded webinar to find out how Veeam Backup & Replication allows you to get consistent backups of MySQL databases.

  • 4
  • 2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now