Hoe kan ik een argument doorgeven aan een PowerShell-script?

Er is een PowerShell-script met de naam itunesForward.ps1die iTunes snel vooruitzet 30 seconden:

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

Het wordt uitgevoerd met een Prompt Line-opdracht:

powershell.exe itunesForward.ps1

Is het mogelijk om een ​​argument uit de opdrachtregel door te geven en deze in het script moet worden toegepast in plaats van de hardcodeerde waarde van 30 seconden?


Antwoord 1, Autoriteit 100%

getest als werken:

#Must be the first statement in your script (not coutning comments)
param([Int32]$step=30) 
$iTunes = New-Object -ComObject iTunes.Application
if ($iTunes.playerstate -eq 1)
{
  $iTunes.PlayerPosition = $iTunes.PlayerPosition + $step
}

bel het met

powershell.exe -file itunesForward.ps1 -step 15

Meerdere parameters syntaxis (opmerkingen zijn optioneel, maar toegestaan):

<#
    Script description.
    Some notes.
#>
param (
    # height of largest column without top bar
    [int]$h = 4000,
    # name of the output image
    [string]$image = 'out.png'
)

Antwoord 2, Autoriteit 57%

U kunt ook de $argsvariabele gebruiken (dat is net als Position Parameters):

$step = $args[0]
$iTunes = New-Object -ComObject iTunes.Application
if ($iTunes.playerstate -eq 1)
{
  $iTunes.PlayerPosition = $iTunes.PlayerPosition + $step
}

Dan kan het worden genoemd als:

powershell.exe -file itunersforward.ps1 15

Antwoord 3, Autoriteit 3%

Bel het script uit een batchbestand (* .bat) of 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"

bel van PowerShell

PowerShell Core of Windows PowerShell

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

Script.ps1 – Scriptcode

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

Antwoord 4

Laat PowerShell het gegevenstype analyseren en bepalen. Het gebruikt hiervoor intern een ‘Variant’.

En over het algemeen doet het het goed…

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

Of als u meerdere parameters moet doorgeven:

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

Antwoord 5

Maak een PowerShell-script met de volgende code in het bestand.

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

Hiermee wordt een script gemaakt met een padparameter. Het zal alle symbolische links binnen het opgegeven pad weergeven, evenals het gespecificeerde doel van de symbolische link.


Antwoord 6

U kunt een variabele ook rechtstreeks in de PowerShell-opdrachtregel definiëren en vervolgens het script uitvoeren. De variabele wordt daar ook gedefinieerd. Dit hielp me in het geval dat ik een ondertekend script niet kon wijzigen.

Voorbeeld:

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

met iTunesForward.ps1 als

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

Antwoord 7

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

Other episodes