powershell 模板powershell函数,占位符解释了Powershell中发生的高级参数绑定。用法:见里面的评论

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了powershell 模板powershell函数,占位符解释了Powershell中发生的高级参数绑定。用法:见里面的评论相关的知识,希望对你有一定的参考价值。

Function Test-PipelineAndBinding {

  # Usage: 
  # 
  # . .\Test-PipelineAndBinding.ps1
  # Test-Self

  [CMDLETBINDING(PositionalBinding=$False)]
  [OUTPUTTYPE([String[]])]
  PARAM( 
      [PARAMETER( ValueFromPipeline, ValueFromPipelineByPropertyName )]
      [ALLOWEMPTYSTRING()] [ALLOWEMPTYCOLLECTION()] [ALLOWNULL()]
      [Object[]]
      $pipeItem = $null, 

      [PARAMETER( ValueFromRemainingArguments )]
      [ALLOWEMPTYSTRING()] [ALLOWEMPTYCOLLECTION()] [ALLOWNULL()]
      [Object[]]
      $restArgs
  )


  BEGIN{

    # Here we have: 
    #   $restArgs   - equals to all arguments 
    #       (not from the pipeline, but which are to the right of Cmdlet name)
    
    Write-Host -ForegroundColor Green "BEGIN block"
    Write-Host -ForegroundColor Green "input =    $input"
    Write-Host -ForegroundColor Green "pipeItem = $pipeItem"
    Write-Host -ForegroundColor Green "_ =        $_"
    Write-Host -ForegroundColor Green "restArgs = $restArgs"
  }

  PROCESS{

    # Here we have:
    #   $_          - single current item from the pipeline
    #   $pipeItem   - the same as above
    #   $input      - the same as above
    #
    #   $restArgs is here as well - it doesn't change unless we change it ourselves here or in BEGIN{}

    Write-Host -ForegroundColor Cyan "    PROCESS block"
    Write-Host -ForegroundColor Cyan "    input =    $input"
    Write-Host -ForegroundColor Cyan "    pipeItem = $pipeItem"
    Write-Host -ForegroundColor Cyan "    _ =        $_"
    Write-Host -ForegroundColor Cyan "    restArgs = $restArgs"
  }

  END{

    # Here we have:
    #   $_          - equals the last value of $_ from the PROCESS{} block
    #   $pipeItem   - the same as $_
    #
    #   $restArgs is here as well - it doesn't change unless we change it somewhere above


    Write-Host -ForegroundColor Yellow "END block"
    Write-Host -ForegroundColor Yellow "input =    $input"
    Write-Host -ForegroundColor Yellow "pipeItem = $pipeItem"
    Write-Host -ForegroundColor Yellow "_ =        $_"
    Write-Host -ForegroundColor Yellow "restArgs = $restArgs"
  }

}


function Test-Self {

  Write-Host -ForegroundColor Red "`nTest-PipelineAndBinding";                         Test-PipelineAndBinding
  Write-Host -ForegroundColor Red "`nTest-PipelineAndBinding 10 20 30 40";             Test-PipelineAndBinding 10 20 30 40
  Write-Host -ForegroundColor Red "`nTest-PipelineAndBinding 10,20,30,40";             Test-PipelineAndBinding 10,20,30,40
  Write-Host -ForegroundColor Red "`nTest-PipelineAndBinding 10 20,30 40";             Test-PipelineAndBinding 10 20,30 40
  Write-Host -ForegroundColor Red "`nTest-PipelineAndBinding 10 @(20,30) 40";          Test-PipelineAndBinding 10 @(20,30) 40
  Write-Host -ForegroundColor Red "`nTest-PipelineAndBinding 10 @(20,@(21,22),30) 40"; Test-PipelineAndBinding 10 @(20,@(21,22),30) 40
  Write-Host -ForegroundColor Red "`nTest-PipelineAndBinding 10,@(20,@(21,22),30),40"; Test-PipelineAndBinding 10,@(20,@(21,22),30),40

}

以上是关于powershell 模板powershell函数,占位符解释了Powershell中发生的高级参数绑定。用法:见里面的评论的主要内容,如果未能解决你的问题,请参考以下文章

powershell [PS:脚本模板]只是PowerShell脚本要遵循的模板。 #PowerShell #Template

PowerShell\WPF:使用数据模板的 PowerShell 中的空单选按钮对象

powershell PowerShell脚本模板

powershell PowerShell:脚本模板版本2(无需记录)

powershell Powershell模板

powershell PowerShell脚本模板