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

3okqufwl  于 2023-03-23  发布在  Shell
关注(0)|答案(8)|浏览(198)

有一个名为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秒值?

ui7jx7zq

ui7jx7zq1#

测试为工作:

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

advanced parameters的一些示例,例如 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"

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

2w3kk1z5

2w3kk1z52#

你也可以使用$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
vxf3dgd4

vxf3dgd43#

从批处理文件(*.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"

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或Windows PowerShell

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

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
afdcj2ne

afdcj2ne4#

让PowerShell分析并决定数据类型。它在内部使用“Variant”。
而且一般来说它做得很好...

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
}
liwlm1x9

liwlm1x95#

# 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
}
ohtdti5x

ohtdti5x6#

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

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

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

c3frrgcw

c3frrgcw7#

您也可以直接在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
}
cygmwpex

cygmwpex8#

在我的特定用例中,我想从忽略params的配置文件中访问参数,并简单地检查参数的存在性(本质上是一个开关)。
我有一个运行ps1文件的批处理脚本,但有一个配置文件。profile.ps1在加载时输出文本。在一些脚本中,如果我不需要,我想禁用该文本的输出。为此,我创建了一些简单的开关,如下面的示例。
这是一个非常基本的版本,但如果你愿意,你可以扩展它。使用'静音运行'模式的例子,然后...

在我的批处理脚本中:

C:\path\to\pwsh.exe -Command "%~dpn0.ps1" -QuietProfile

PowerShell脚本(profile.ps1内部):

[Boolean]$global:QuietProfile = [Boolean]([Environment]::GetCommandLineArgs() -match "-QuietProfile")

if (-not $global:QuietProfile) {
  Write-Host "I am some text which should not appear when -QuietProfile is passed"
}

默认情况下(如果您刚刚打开pwsh.exe),那么$global:QuietProfile值将为false,您将看到正常的输出。

相关问题