首页 文章

尝试从PowerShell中的函数传回一个字符串

提问于
浏览
0
function ExtractLocations([ref]$lp_Locations) {
    $lp_Locations.Value = "A STRING VALUE"

    return 0
}

...   

$Locations = ""
if (@(ExtractLocations([ref]$Locations)) -ne 0) {
    RecordErrorThenExit
}

$Locations 总是以空字符串结尾 .

1 回答

  • 0

    除了@ mklemen0所说的,设置值,你不需要像 $Variable.Value = 'Something' 那样做,它只是 $Variable = 'Something'

    使用 @() 表达式,您将输出转换为不需要的数组 . PowerShell中不建议声明与c#中的方法类似的函数 . 你可以像下面这样做 .

    function ExtractLocations{
        Param([ref]$lp_Locations)
        $lp_Locations = "A STRING VALUE"
    
        return 0
    }
    
    ExtractLocations -lp_Locations ([ref]$Locations)
    

相关问题