如何将参数传递给PowerShell脚本? [英] How to pass an argument to a PowerShell script?
本文介绍了如何将参数传递给PowerShell脚本?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
有一个 PowerShell
脚本命名为 itunesForward.ps1
,使iTunes快进30秒:
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
是否可以从命令行传递参数,并将其应用于脚本,而不是硬编码的30秒值?
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
}
使用
powershell.exe -file itunesForward.ps1 -step 15
这篇关于如何将参数传递给PowerShell脚本?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文