在Powershell中使用通过引用传递的字符串时,如何访问其值?

时间:2019-02-14 19:05:12

标签: string powershell types pass-by-reference

我的问题适用于通过引用PowerShell函数来传递String类型。

this question中,他们设置了变量的.Value,该变量通过引用传递,以便其值在函数退出后仍然存在。

但是,如果您要访问/读取引用指向的.Value,您是在函数中单独使用其参数名称,还是使用其.Value属性?

在下面的代码中,我希望$whichMenu字符串参数在退出函数后能够保留其值,但我也想在函数中访问/读取它。

我不知道.Value是否像C ++中的解引用运算符一样起作用。

Function displayMenu($funcHash, [String][ref]$whichMenu, $storeName, $startRange, $endRange) {

    do {
        # TODO: Display Reg Range: $Host.UI.RawUI.WindowTitle = ""
        # Display a Menu
        cls
        Write-Host "whichFunc: $($whichFunc)"           
        Write-Host "$($storeName) | $($startRange) - $($endRange)"
        Write-Host "-------------------------------------"
        Write-Host ""

        $funcHash[$whichMenu].Keys | sort | %{ 
            Write-Host -NoNewLine $_ 
            Write-Host -NoNewLine "."

            $subHash = $funcHash[$whichMenu][$_]

            Write-Host " " $subHash["label"]
        }

        Write-Host "-------------------------------------"
        Write-Host "1:common|2:drivers|3:netmask|4:printer|5:display"               
        Write-Host "-------------------------------------"

        $whichFunc = queryUserCommon $dasIntOptions "Pick a function: " 


        if($menuKeys -contains $whichMenu) {
          $whichMenu.Value = "" + $whichFunc 
        }

        Write-Host "whichFunc: $($whichFunc)"
        Write-Host "actionKeys $($actionKeys)"
                    Write-Host "$($actionKeys -contains $whichFunc)"

        Write-Host "whichMenu: $($whichMenu)"
        Write-Host "menuKeys: $($menuKeys)"
        Write-Host "$($menuKeys -notcontains $whichMenu)"

    } while($actionKeys -notcontains $whichFunc)

    return $whichFunc
}

0 个答案:

没有答案