How to pass an argument to a PowerShell script?

浪尽此生 提交于 2019-11-26 04:58:24

问题


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

Is it possible to pass an argument from the command line and have it applied in the script instead of hardcoded 30 seconds value?


回答1:


Tested as working:

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
}

Call it with

powershell.exe -file itunesForward.ps1 -step 15



回答2:


You can use also $args variable (that's like position parameters):

$step=$args[0]

$iTunes = New-Object -ComObject iTunes.Application

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

then it can be call like:

powershell.exe -file itunersforward.ps1 15



回答3:


let Powershell analyze and decide the data type
Internally uses a 'Variant' for this...
and generally does a good job...

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

or if you need to pass multiple parameters

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



回答4:


Call Script from bath file (*.bat) or 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"

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"


Call from powershell

Powershell Core or Windows Powershell

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

Script.ps1 - Script Code

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



回答5:


Create a powershell script with the following code in the file.

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

This creates a script with a path parameter. It will list all symboliclinks within the path provided as well as the specified target of the symbolic link.




回答6:


You can also define a variable directly in the PowerShell command line and then execute the script. The variable will be defined there, too. This helped me in a case where I couldn't modify a signed script.

Example:

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

with iTunesForward.ps1 being

$iTunes = New-Object -ComObject iTunes.Application

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


来源:https://stackoverflow.com/questions/5592531/how-to-pass-an-argument-to-a-powershell-script

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!