添加链接
link之家
链接快照平台
  • 输入网页链接,自动生成快照
  • 标签化管理网页链接

There's a PowerShell script named itunesForward.ps1 that makes the 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 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 hardcoded 30 seconds value?

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

Call it with

powershell.exe -file itunesForward.ps1 -step 15
                what if the parameter is a string?  What is the syntax? would it be something like -step '15' or -step "15"
                    – Andrew Gray
                Nov 2 '15 at 0:04
                @Andrew First of all you have to change the type of the parameter to [string]. If you then want to pass a string as parameter you can use either ' or ". If there is no space (or quotes) inside the string you can even omit the quotes.
                    – Ocaso Protal
                Nov 2 '15 at 8:31
                FYI, to use multiple params, use this syntax: param([string]$env,[string]$s3BucketName)
                    – Josh Padnick
                Dec 29 '15 at 23:12
                It is missing "-file". It doesnt work for me until i added this. See the complete code: powershell.exe -file itunesForward.ps1 -step 15
                    – Charles
                Feb 22 '16 at 23:43
                @Charles Thanks for the hint. You are right: The -file is missing from the call. The call without might work with Powershell Version 1.0 but I can't test it. Updated the answer.
                    – Ocaso Protal
                Feb 23 '16 at 8:36
                Found this easier than the accepted solution, PLUS you can directly use $args[0] anywhere in the script (no need to be first line). PS: Tip on passing strings as arguments: They must be enclosed in single quotes.
                    – ADTC
                Jul 14 '12 at 6:21
                Both this and the accepted solution work, the main difference is that this reads parameters by position, while the accepted solution does it by name. When multiple parameters need to be passed, passing by name might be cleaner.
                    – Florin Dumitrescu
                Nov 22 '13 at 15:57
                This answer is getting so much attention, please check out the related one which is much more complete. stackoverflow.com/questions/6359618/…
                    – Emiliano Poggi
                Sep 1 '16 at 13:55

let Powershell analyze and decide the data type
Internally uses a 'Variant' for this...
and generally does a good job...

param( $x )
$iTunes = New-Object -ComObject iTunes.Application
if ( $iTunes.playerstate -eq 1 ) 
    { $iTunes.PlayerPosition = $iTunes.PlayerPosition + $x }

or if you need to pass multiple parameters

param( $x1, $x2 )
$iTunes = New-Object -ComObject iTunes.Application
if ( $iTunes.playerstate -eq 1 ) 
    $iTunes.PlayerPosition = $iTunes.PlayerPosition + $x1 
    $iTunes.<AnyProperty>  = $x2
param([string]$path)
Get-ChildItem $path | Where-Object {$_.LinkType -eq 'SymbolicLink'} | select name, target

This creates a script with a path parameter. It will list all symboliclinks within the path provided as well as the specified target of the symbolic link.

You can also define a variable directly in the PowerShell command line and then execute the script. The variable will be defined there, too. This helped me in a case where I couldn't modify a signed script.

Example:

 PS C:\temp> $stepsize = 30
 PS C:\temp> .\itunesForward.ps1

with iTunesForward.ps1 being

$iTunes = New-Object -ComObject iTunes.Application
if ($iTunes.playerstate -eq 1)
  $iTunes.PlayerPosition = $iTunes.PlayerPosition + $stepsize
        

Thanks for contributing an answer to Stack Overflow!

  • Please be sure to answer the question. Provide details and share your research!

But avoid

  • Asking for help, clarification, or responding to other answers.
  • Making statements based on opinion; back them up with references or personal experience.

To learn more, see our tips on writing great answers.

site design / logo © 2019 Stack Exchange Inc; user contributions licensed under cc by-sa 4.0 with attribution required. rev 2019.9.23.34994