There's a PowerShell script named itunesForward.ps1
that makes iTunes fast forward 30 seconds:
$iTunes = New-Object -ComObject iTunes.Application if ($iTunes.playerstate -eq 1) { $iTunes.PlayerPosition = $iTunes.PlayerPosition + 30 }
It is executed with a prompt line command:
powershell.exe itunesForward.ps1
Is it possible to pass an argument from the command line and have it applied in the script instead of the hardcoded 30 seconds value?
You can run scripts with parameters in any context by simply specifying them while running the PowerShell executable like powershell.exe -Parameter 'Foo' -Parameter2 'Bar' . Once you open cmd.exe, you can execute a PowerShell script like below.
To pass multiple parameters you must use the command line syntax that includes the names of the parameters. For example, here is a sample PowerShell script that runs the Get-Service function with two parameters. The parameters are the name of the service(s) and the name of the Computer.
The PowerShell parameter is a fundamental component of any script. A parameter is a way that developers enable script users to provide input at runtime. If a PowerShell script's behavior needs to change in some way, a parameter provides an opportunity to do so without changing the underlying code.
The name of the parameter is preceded by a hyphen ( - ), which signals to PowerShell that the word following the hyphen is a parameter name. The parameter name and value can be separated by a space or a colon character. Some parameters do not require or accept a parameter value.
Tested as working:
#Must be the first statement in your script (not coutning comments) param([Int32]$step=30) $iTunes = New-Object -ComObject iTunes.Application if ($iTunes.playerstate -eq 1) { $iTunes.PlayerPosition = $iTunes.PlayerPosition + $step }
Call it with
powershell.exe -file itunesForward.ps1 -step 15
Multiple parameters syntax (comments are optional, but allowed):
<# Script description. Some notes. #> param ( # height of largest column without top bar [int]$h = 4000, # name of the output image [string]$image = 'out.png' )
And some example for advanced parameters, e.g. 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"
A default value will not work with a mandatory parameter. You can omit the =$true
for advanced parameters of type boolean [Parameter(Mandatory)]
.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With