相关疑难解决方法(0)

Powershell scriptblock变量范围和模块的奇怪行为,任何建议?

注意:我在Windows Vista上使用PowerShell 2.0.

我正在尝试添加对为psake指定构建参数的支持,但我遇到了一些奇怪的PowerShell变量作用域行为,专门处理使用Export-ModuleMember导出的调用函数(这是psake公开它的主要方法的方式).以下是一个简单的PowerShell模块来说明(名为repoCase.psm1):

function Test {
    param(
        [Parameter(Position=0,Mandatory=0)]
        [scriptblock]$properties = {}
    )

    $defaults = {$message = "Hello, world!"}

    Write-Host "Before running defaults, message is: $message"

    . $defaults

    #At this point, $message is correctly set to "Hellow, world!"
    Write-Host "Aftering running defaults, message is: $message"

    . $properties

    #At this point, I would expect $message to be set to whatever is passed in,
    #which in this case is "Hello from poperties!", but it isn't.  
    Write-Host "Aftering running properties, message …
Run Code Online (Sandbox Code Playgroud)

powershell scope psake

16
推荐指数
2
解决办法
5282
查看次数

标签 统计

powershell ×1

psake ×1

scope ×1