Menu

[ea00bd]: / ps-usage / PS-ParamsExample.ps1  Maximize  Restore  History

Download this file

51 lines (48 with data), 1.4 kB

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
#
# paramsExample.ps1
#
Param(
[switch]$x,
[switch]$y,
[switch]$z
)
if ($x) {Write-Output "You called this script with -x"}
if ($y) {Write-Output "You called this script with -y"}
if ($z) {Write-Output "You called this script with -z"}
Function someFunction {
Param(
[switch]$a,
[switch]$b,
[switch]$c
)
Write-Output "(someFunction): a is: $a b is: $b c is: $c"
if ($x) {Write-Output "you also called this script with -x"}
if ($y) {Write-Output "you also called this script with -y"}
if ($z) {Write-Output "you also called this script with -z"}
}
#
#####
#
# calling our internal function with switch options
#
Write-Output "calling: `"someFunction -a`": $(someFunction -a)"
Write-Output "calling: `"someFunction -b`": $(someFunction -b)"
Write-Output "calling: `"someFunction -b:`$False`": $(someFunction -b:$false)"
Write-Output "calling: `"someFunction -c`": $(someFunction -c)"
Write-Output "calling: `"someFunction -a -b`": $(someFunction -a -b)"
Write-Output "calling: `"someFunction -c -b`": $(someFunction -c -b)"
Write-Output "calling: `"someFunction -a -b -c`": $(someFunction -a -b -c)"
#
#####
#
# Script switches can be accessed by running the script with switches, as in:
#
# ./paramsExample.ps1 -x -y -z
# ./paramsExample.ps1 -x:True -y:False -z
# ./paramsExample.ps1 -x:False -z:True
# ./paramsExample.ps1 -x
# ./paramsExample.ps1 -y -z
# [...]
#
# Experiment!
#
Want the latest updates on software, tech news, and AI?
Get latest updates about software, tech news, and AI from SourceForge directly in your inbox once a month.