如何将参数传递给PowerShell脚本?

Bor*_*vić 408 powershell command-line automation itunes argument-passing

有一个PowerShell名为的脚本itunesForward.ps1使iTunes快进30秒:

$iTunes = New-Object -ComObject iTunes.Application

if ($iTunes.playerstate -eq 1)
{
  $iTunes.PlayerPosition = $iTunes.PlayerPosition + 30
}
Run Code Online (Sandbox Code Playgroud)

它使用快速行命令执行:

powershell.exe itunesForward.ps1
Run Code Online (Sandbox Code Playgroud)

是否可以从命令行传递参数并将其应用于脚本而不是硬编码的30秒值?

Oca*_*tal 563

测试工作:

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
}
Run Code Online (Sandbox Code Playgroud)

叫它

powershell.exe -file itunesForward.ps1 -step 15
Run Code Online (Sandbox Code Playgroud)

  • 仅供参考,要使用多个参数,请使用以下语法:`param([string] $ env,[string] $ s3BucketName)` (57认同)
  • @Andrew首先你必须将参数的类型改为`[string]`.如果你想传递一个字符串作为参数,你可以使用`'`或```.如果字符串中没有空格(或引号),你甚至可以省略引号. (7认同)
  • 如果参数是一个字符串怎么办?语法是什么?会不会像-step'15'或-step"15" (6认同)
  • 它缺少"-file".在我添加之前,它对我不起作用.请参阅完整代码:powershell.exe -file itunesForward.ps1 -step 15 (3认同)
  • @Charles 谢谢你的提示。你是对的:调用中缺少 `-file`。调用 without 可能适用于 Powershell 1.0 版,但我无法对其进行测试。更新了答案。 (2认同)

Emi*_*ggi 345

你也可以使用$args变量(就像位置参数一样):

$step=$args[0]

$iTunes = New-Object -ComObject iTunes.Application

if ($iTunes.playerstate -eq 1)
{
  $iTunes.PlayerPosition = $iTunes.PlayerPosition + $step
}
Run Code Online (Sandbox Code Playgroud)

然后它可以像这样调用:

powershell.exe -file itunersforward.ps1 15
Run Code Online (Sandbox Code Playgroud)

  • 发现这比接受的解决方案更容易,PLUS你可以直接在脚本中的任何地方使用$ args [0](不需要是第一行).PS:提示传递字符串作为参数:它们必须用单引号括起来. (55认同)
  • 这个和公认的解决方案都有效,主要区别在于它按位置读取参数,而接受的解决方案按名称进行.当需要传递多个参数时,按名称传递可能更清晰. (26认同)
  • 在已接受的解决方案中命名为params也会自动填充get-help (3认同)
  • 这个答案得到了如此多的关注,请查看相关的更完整的答案.http://stackoverflow.com/questions/6359618/pass-parameter-from-batch-file-to-the-powershell-script/6362404#6362404 (2认同)

小智 9

# ENTRY POINT MAIN()
Param(
    [Parameter(Mandatory=$True)]
    [String] $site,
    [Parameter(Mandatory=$True)]
    [String] $application,
    [Parameter(Mandatory=$True)]
    [String] $dir,
    [Parameter(Mandatory=$True)]
    [String] $applicationPool
)

# Create Web IIS Application
function ValidateWebSite ([String] $webSiteName)
{
    $iisWebSite = Get-Website -Name $webSiteName
    if($Null -eq $iisWebSite)
    {
        Write-Error -Message "Error: Web Site Name: $($webSiteName) not exists."  -Category ObjectNotFound
    }
    else
    {
        return 1
    }
}

# Get full path from IIS WebSite
function GetWebSiteDir ([String] $webSiteName)
{
    $iisWebSite = Get-Website -Name $webSiteName
    if($Null -eq $iisWebSite)
    {
        Write-Error -Message "Error: Web Site Name: $($webSiteName) not exists."  -Category ObjectNotFound
    }
    else
    {
        return $iisWebSite.PhysicalPath
    }
}

# Create Directory
function CreateDirectory([string]$fullPath)
{
    $existEvaluation = Test-Path $fullPath -PathType Any
    if($existEvaluation -eq $false)
    {
        new-item $fullPath -itemtype directory
    }
    return 1
}

function CreateApplicationWeb
{
    Param(
        [String] $WebSite,
        [String] $WebSitePath,
        [String] $application,
        [String] $applicationPath,
        [String] $applicationPool
        )
    $fullDir = "$($WebSitePath)\$($applicationPath)"
    CreateDirectory($fullDir)
    New-WebApplication -Site $WebSite -Name $application -PhysicalPath $fullDir -ApplicationPool $applicationPool -Force
}

$fullWebSiteDir = GetWebSiteDir($Site)f($null -ne $fullWebSiteDir)
{
    CreateApplicationWeb -WebSite $Site -WebSitePath $fullWebSiteDir -application $application  -applicationPath $dir -applicationPool $applicationPool
}
Run Code Online (Sandbox Code Playgroud)


ZEE*_*ZEE 6

让Powershell分析并确定数据类型在
内部为此使用'Variant'...
通常做得很好...

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

或者如果您需要传递多个参数

param( $x1, $x2 )
$iTunes = New-Object -ComObject iTunes.Application
if ( $iTunes.playerstate -eq 1 ) 
    { 
    $iTunes.PlayerPosition = $iTunes.PlayerPosition + $x1 
    $iTunes.<AnyProperty>  = $x2
    }
Run Code Online (Sandbox Code Playgroud)


Jom*_*oma 6

从Bath文件(* .bat)或CMD调用脚本

Powershell核心

pwsh.exe -NoLogo -ExecutionPolicy Bypass -Command "./Script.ps1 -Param1 Hello -Param2 World"

pwsh.exe -NoLogo -ExecutionPolicy Bypass -Command "path-to-script/Script.ps1 -Param1 Hello -Param2 World"
Run Code Online (Sandbox Code Playgroud)

电源外壳

powershell.exe -NoLogo -ExecutionPolicy Bypass -Command "./Script.ps1 -Param1 Hello -Param2 World"

powershell.exe -NoLogo -ExecutionPolicy Bypass -Command "path-to-script/Script.ps1 -Param1 Hello -Param2 World"
Run Code Online (Sandbox Code Playgroud)


从Powershell呼叫

Powershell Core或Windows Powershell

& path-to-script/Script.ps1 -Param1 Hello -Param2 World
& ./Script.ps1 -Param1 Hello -Param2 World
Run Code Online (Sandbox Code Playgroud)

Script.ps1-脚本代码

param(
    [Parameter(Mandatory=$True, Position=0, ValueFromPipeline=$false)]
    [System.String]
    $Param1,

    [Parameter(Mandatory=$True, Position=1, ValueFromPipeline=$false)]
    [System.String]
    $Param2
)

Write-Host $Param1
Write-Host $Param2
Run Code Online (Sandbox Code Playgroud)


JDe*_*nis 5

在文件中使用以下代码创建 PowerShell 脚本。

param([string]$path)
Get-ChildItem $path | Where-Object {$_.LinkType -eq 'SymbolicLink'} | select name, target
Run Code Online (Sandbox Code Playgroud)

这将创建一个带有路径参数的脚本。它将列出提供的路径中的所有符号链接以及符号链接的指定目标。