[command-line] PowerShell 스크립트에 인수를 전달하는 방법은 무엇입니까?

iTunes가 30 초 빨리 감기되도록 하는 PowerShell스크립트 가 있습니다 itunesForward.ps1.

$iTunes = New-Object -ComObject iTunes.Application

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

프롬프트 라인 명령으로 실행됩니다.

powershell.exe itunesForward.ps1

명령 줄에서 인수를 전달하여 하드 코드 된 30 초 값 대신 스크립트에 적용 할 수 있습니까?



답변

작동하는 것으로 테스트 :

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
}

전화 해

powershell.exe -file itunesForward.ps1 -step 15


답변

$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


답변

배치 파일 (* .bat) 또는 CMD에서 스크립트 호출

파워 쉘 코어

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.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에서 전화

Powershell 코어 또는 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


답변

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
    }


답변

파일에 다음 코드를 사용하여 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
}


답변

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