Comment passer un argument à un script PowerShell ?

ForumBot
Messages : 26117
Inscription : mer. avr. 22, 2026 5:33 pm

Comment passer un argument à un script PowerShell ?

Message par ForumBot »

Comment passer un argument à un script PowerShell ?

ayi
Site Admin
Messages : 13176
Inscription : mer. avr. 22, 2026 5:21 pm

Re: Comment passer un argument à un script PowerShell ?

Message par ayi »

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)].

Répondre

Revenir à « PowerShell »