Notions de base la programmation de Powershell
.
Sortie
PowerShell dispose de nombreuses options de texte de sortie. Voici quelques exemples. La valeur par défaut est de diriger la sortie dans le tube. Cela peut être fait par Write-Output ou par la simple énumération de la chaîne sur une ligne seule. Vous pouvez forcer un texte à la console via Write-Host.
Write-Output Hello
Write-Host Hello
"Hello"
Variables
.
# declare variables Set-Variable -name num -value 5 Set-Variable -name char -value 'a' Set-Variable -name string -value "This is a string" # print the variables "Number is $num" "Character is `'$char`'" "String is `"$string`""
# declare variables $num=5 $char='a' $string="This is a string" # print the variables "Number is $num" "Character is `'$char`'" "String is `"$string`""
Constantes
.
#constant
Arithmétique
.
# integer arithmetic operations $width=5 $length=6 $area=$width * $length # output results "The area of a square (width=$width, length=$length) is $area" # boolean logic operations $result=$true -and $false -or $true # output results "The statement (true AND false OR true) is $result"
.
Set-Variable pi -option Constant -value 3.14159265359 # set approximation of π $radius=3 # set radius $area=$pi * [Math]::Pow($radius, 2) # calculate area "The area of a circle is $area" # output result
.
Set-Variable pi -option Constant -value 3.14159265359 # set apoximation of π "The cosine of π/4 is: " + [Math]::Cos($pi/4) # print calculation of cos(π/4)
Entrée
.
$name = Read-Host "Enter your name: " Write-Output "Hello $name"
Choix
.
# prompt user and get input $number = [int](Read-Host "Input a number: ") # evaluate input and print result if ( $number -gt 0 ) { "Number is greater than 0" } elseif ( $number -lt 0 ) { "Number is less than 0" } else { "Number is 0" }
.
$keypress = Read-Host "Input a character: " # prompt user and get input $keypress = $keypress.substring(0,1) # substring for only 1st char # switch w/ regexp construction switch -regex -casesensitive ($keypress) { "[a-z]" { "Lowercase letter"} "[A-Z]" { "Uppercase letter"} "[0-9]" { "Digit" } default { "Punctuation, whitespace, or other" } }
.
$keypress = Read-Host "Input a character: " # prompt user and get input $keypress = $keypress.substring(0,1) # substring for only 1st char # evaluate keypress matches pattern if ($keypress -cmatch "[a-z]") { "Lowercase letter" } elseif ($keypress -cmatch "[A-Z]") { "Uppercase letter" } elseif ($keypress -cmatch "[0-9]") { "Digit" } else { "Punctuation, whitepace, or other" }
Boucles
.
$count = 10 while ($count -gt 0) { "Count is $count" $count-- }
for ($count=10; $count -gt 0; $count--) { "Count is $count"
foreach($count in 10..1) { "Count is $count" }
10..1 | foreach { "Count is $_" }
# while not done loop
# loop through complete listing with foreach foreach($item in Get-ChildItem) { # cycle through directory listing if ($item.PsIsContainer) { # test if object is directory "$item is directory" } else { "$item is not directory" } }
# iterate through listing by each item through foreach-object Get-ChildItem | ForEach-Object { # pipe in listing, iterate through items if ($_.PsIsContainer) { # test if default object is directory "$_ is directory" } else { "$_ is not directory" } }
# iterate through listing using switch switch (Get-ChildItem) { $_.PsIsContainer { "$_ is directory" } default { "$_ is not directory" } }
Tableaux
.
# create an empty list to force data type to be an array $nicknames = @() # populate array with each item $nicknames += "bob" $nicknames += "ed" $nicknames += "steve" $nicknames += "ralph" $nicknames += "joe" $nicknames += "deb" $nicknames += "kate" # output results "The number of nicknames is: " + $nicknames.length # print size of array "The nicknames are: $nicknames" # print enumerated list
# build array using comma operator $nicknames = "bob", "ed", "steve", "ralph", "joe", "deb", "kate" # print results "The names are: " foreach ($name in $nicknames) { # cycle through array " $name" # print out each element }
# build array using comma operator $nicknames = "bob", "ed", "steve", "ralph", "joe", "deb", "kate" # print results "The names are: " for ($count = 0; $count -lt $nicknames.length; $count++) { # iterate by index " nicknames[$count] = " + $nicknames[$count] # print index and element }
Tableaux associatifs
.
# create empty hash $ages = @{} # individually build hash $ages["bob"]=34 $ages["ed"]=58 $ages["steve"]=32 $ages["ralph"]=23 $ages["deb"]=46 $ages["kate"]=19 # print out results "Keys (names): " + $ages.keys # print enumerated list of keys "Values (ages): " + $ages.values # print enumerated list of values
En PHP, vous devez utiliser des fonctions pour faire une fusion simple.
# build/populate hash $ages = @{"bob"=34; "ed"=58; "steve"=32; "ralph"=23} # merge new hash into existing hash $ages += @{"deb"=46; "kate"=19} # output results foreach ($name in $ages.keys) { # cycle through whole list " ages[$name]=" + $ages[$name] # print out each element }