有一个名为itunesForward的PowerShell脚本。让iTunes快进30秒的ps1:
$iTunes = New-Object -ComObject iTunes.Application
if ($iTunes.playerstate -eq 1)
{
$iTunes.PlayerPosition = $iTunes.PlayerPosition + 30
}
它由提示行命令执行:
powershell.exe itunesForward.ps1
是否有可能从命令行传递一个参数,并将其应用到脚本中,而不是硬编码的30秒值?
测试为工作状态:
#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
}
用
powershell.exe -file itunesForward.ps1 -step 15
多参数语法(注释是可选的,但允许):
<#
Script description.
Some notes.
#>
param (
# height of largest column without top bar
[int]$h = 4000,
# name of the output image
[string]$image = 'out.png'
)
以及一些高级参数的示例,例如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"
默认值不能与强制参数一起使用。对于布尔[参数(必选)]类型的高级参数,可以省略=$true。
测试为工作状态:
#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
}
用
powershell.exe -file itunesForward.ps1 -step 15
多参数语法(注释是可选的,但允许):
<#
Script description.
Some notes.
#>
param (
# height of largest column without top bar
[int]$h = 4000,
# name of the output image
[string]$image = 'out.png'
)
以及一些高级参数的示例,例如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"
默认值不能与强制参数一起使用。对于布尔[参数(必选)]类型的高级参数,可以省略=$true。