PowerShell高级功能很难正确管道

时间:2014-07-23 13:54:23

标签: function powershell

我无法理解管道上的东西。我已经阅读了Get-Help和Jon Dones他的例子,但我无法理解它。所以我开始使用我在网上找到的功能来执行特定文化(区域设置)中的脚本块。

我设法让它正确处理这些参数:

Use-Culture de-DE {Get-Date}, {Get-TimeStamp}
Use-Culture nl-BE {Get-Date}, {Get-TimeStamp}

但是当我尝试输入Use-Culture时,它根本就不起作用了:

de-DE {Get-Date}, {Get-TimeStamp} | Use-Culture
nl-BE {Get-Date} | Use-Culture

我觉得我错过了这里的基本概念。 StackOverlfow还有另一个关于这个话题的问题。一个人说要使用Foreach而其他人说要与-Inputobject合作。然后,Don Jones在他的高级函数中使用了没有这样的东西,并声明Process块被迭代为每个对象。

将多个参数传递给函数的最佳做法是什么?在这种情况下,要对同一Script blocks执行多个Culture

一如既往地感谢您的帮助。

Function Use-Culture { 
    param( 
            [Parameter(
            Mandatory=$true,Position=0,
            ValueFromPipeline=$true,
            ValueFromPipelineByPropertyName=$true)] 
            [System.Globalization.CultureInfo]$culture, 
            [Parameter(
            Mandatory=$true,Position=1,
            ValueFromPipeline=$true,
            ValueFromPipelineByPropertyName=$true)]  
            [ScriptBlock[]]$code 
    )
    process { 
            ForEach ($_ in $code) {
             trap { 
                   [System.Threading.Thread]::CurrentThread.CurrentCulture = $currentCulture 
             } 
             $currentCulture = [System.Threading.Thread]::CurrentThread.CurrentCulture 
             [System.Threading.Thread]::CurrentThread.CurrentCulture = $culture 
             Invoke-Command $_ 
             [System.Threading.Thread]::CurrentThread.CurrentCulture = $currentCulture 
             }
    }
}  

0 个答案:

没有答案
相关问题