Comment passer un argument à un script PowerShell ?
Source : Stack Overflow [powershell],)
Testé et fonctionnel :
#Must be the first statement in your script (not counting comments)
param([Int32]$step=30)
$iTunes = New-Object -ComObject iTunes.Application
if ($iTunes.playerstate -eq 1)
{
$iTunes.PlayerPosition = $iTunes.PlayerPosition + $step
}
Appelez-le avec
powershell.exe -file itunesForward.ps1 -step 15
Syntaxe pour paramètres multiples (les commentaires sont optionnels, mais autorisés) :
<#
Script description.
Some notes.
#>
param (
# height of largest column without top bar
[int]$h = 4000,
# name of the output image
[string]$image = 'out.png'
)
Et voici un exemple pour les paramètres avancés, par exemple Mandatory :
<#
Script description.
Some notes.
#>
param (
# height of largest column without top bar
[Parameter(Mandatory=$true)]
[int]$h,
# name of the output image
[string]$image = 'out.png'
)
Write-Host "$image $h"
Une valeur par défaut ne fonctionnera pas avec un paramètre obligatoire. Vous pouvez omettre le =$true pour les paramètres avancés de type booléen [Parameter(Mandatory)].