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

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

$iTunes = New-Object -ComObject iTunes.Application


if ($iTunes.playerstate -eq 1)
{
$iTunes.PlayerPosition = $iTunes.PlayerPosition + 30
}

它由提示行命令执行:

powershell.exe itunesForward.ps1

是否有可能从命令行传递一个参数,并将其应用到脚本中,而不是硬编码的30秒值?

838678 次浏览

测试为工作状态:

#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'
)

以及先进的参数的一些例子,例如强制性的:

<#
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"

默认值不能与强制参数一起使用。对于布尔[Parameter(Mandatory)]类型的高级参数,可以省略=$true

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

$step = $args[0]


$iTunes = New-Object -ComObject iTunes.Application


if ($iTunes.playerstate -eq 1)
{
$iTunes.PlayerPosition = $iTunes.PlayerPosition + $step
}

然后可以这样调用:

powershell.exe -file itunersforward.ps1 15

让PowerShell分析并决定数据类型。它在内部使用了一个“变体”。

总的来说,它做得很好……

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

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

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

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

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

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

您还可以直接在PowerShell命令行中定义一个变量,然后执行脚本。变量也将在那里定义。这在无法修改签名脚本的情况下帮助了我。

例子:

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

iTunesForward。ps1被

$iTunes = New-Object -ComObject iTunes.Application


if ($iTunes.playerstate -eq 1)
{
$iTunes.PlayerPosition = $iTunes.PlayerPosition + $stepsize
}

从批处理文件(*.bat)或CMD调用脚本

PowerShell Core

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"


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


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


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

< >强PowerShell < / >强

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"


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


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


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

从PowerShell调用

PowerShell Core or Windows PowerShell

& path-to-script/Script.ps1 -Param1 Hello -Param2 World
& ./Script.ps1 -Param1 Hello -Param2 World

脚本。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
# 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
}