PowerShellスクリプトに引数を渡すには?

iTunesを30秒早送りするitunesForward.ps1という名前のPowerShellスクリプトがあります。

$iTunes = New-Object -ComObject iTunes.Application

if ($iTunes.playerstate -eq 1)
{
  $iTunes.PlayerPosition = $iTunes.PlayerPosition + 30
}

プロンプトラインコマンドで実行されます。

powershell.exe itunesForward.ps1

コマンドラインから引数を渡して、ハードコードされた30秒の値の代わりにスクリプトで適用させることは可能ですか?

ソリューション

動作確認済みです。

param([Int32]$step=30) #Must be the first statement in your script

$iTunes = New-Object -ComObject iTunes.Application

if ($iTunes.playerstate -eq 1)
{
  $iTunes.PlayerPosition = $iTunes.PlayerPosition + $step
}

で呼び出します。

powershell.exe -file itunesForward.ps1 -step 15
解説 (13)

また、$args変数(位置パラメーターのようなもの)も使用できます。

$step=$args[0]

$iTunes = New-Object -ComObject iTunes.Application

if ($iTunes.playerstate -eq 1)
{
  $iTunes.PlayerPosition = $iTunes.PlayerPosition + $step
}

とすれば、次のように呼び出すことができます。

powershell.exe -file itunersforward.ps1 15
解説 (4)

Powershellにデータ型を解析させて決定させる

解説 (0)