首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >Powershell是否可以使用转换的字符串数据返回完整的插值值?

问Powershell是否可以使用转换的字符串数据返回完整的插值值?
EN

Stack Overflow用户
提问于 2020-07-25 18:18:12
回答 1查看 89关注 0票数 1

我有一个.properties文件,其中包含以下属性:

代码语言:javascript
复制
repository.host=hostname.com/nexus
repository.api.url=https://${repository.host}/service/rest/v1
repository.url=https://${repository.host}/repository

我能够使用以下powershell函数返回值:

代码语言:javascript
复制
    static [string] getProperty( [string] $property ){

        $properties = "../resources/vars/$([jenkins]::PROPERTIES_FILE)"
        $properties = get-content $properties | convertfrom-stringdata

        return $properties.$property

    }

当尝试返回属性repository.url powershell时,返回以下字符串:https://${repository.host}/repository/

我的问题是:是否可以通过powershell中已经存在的特性将返回的字符串设置为https://hostname.com/nexus/repository/

EN

回答 1

Stack Overflow用户

发布于 2020-08-04 17:08:09

@mclement0提供的原始解决方案非常有用,并指导我找到了一个更完整的解决方案。这个解决方案完成/纠正了以下几件事情:

  • 从文件源创建哈希表的能力。
  • 使用$ExecutionContext作用域从类方法中访问$global:变量的能力。
  • 彻底解析哈希表中所有键的能力。
代码语言:javascript
复制
    static [string] getProperties ( [string] $file, [string] $property ){

        $properties = get-content $file -raw | convertfrom-stringdata

        while ( $properties.values -match '\$\{([^}]+)\}' ){
            foreach ($key in @($properties.Keys)) {
                $properties[$key] = $global:ExecutionContext.InvokeCommand.ExpandString( ($properties[$key] -replace '\$\{([^}]+)\}', '$$($$properties[''$1''])') )
            }
        }

        return $properties[$property]
    }

注意:当while循环不存在并且搜索${*}的匹配项时,任何给定的返回值都不可能被完全内插或展开。例如,如果没有while循环,文件的输出可能如下所示:

代码语言:javascript
复制
/nexus
${nexus.protocol}://${nexus.hostname}:${nexus.port}${nexus.context}
${nexus.protocol}://${nexus.hostname}:${nexus.port}${nexus.context}/repository/installers/com/amazon/java/8.0.252/java-1.8.0-amazon-corretto-devel-1.8.0_252.b09-1.x86_64.rpm
${nexus.protocol}://${nexus.hostname}:${nexus.port}${nexus.context}
${nexus.protocol}://${nexus.hostname}:${nexus.port}${nexus.context}/repository/installers/com/oracle/tuxedo/12.1.3.0.0/p30596495_121300_Linux-x86-64.zip
443
https://hostname.com:443/nexus
https://hostname.com:443/nexus/repository/installers/com/oracle/java/jdk/8u251/jdk-8u251-linux-x64.rpm
https://hostname.com:443/nexus/repository/installers/com/oracle/weblogic/12.2.1.3.0/p30965714_122130_Generic.zip
hostname.com
https
https://hostname.com:443/nexus/repository/installers/com/oracle/weblogic/12.2.1.3.0/p30965714_122130_Generic.zip

如果要再次运行相同的脚本(仍然没有while循环),则如下所示:

代码语言:javascript
复制
hostname.com
https://hostname.com:443/nexus
/nexus
https://hostname.com:443/nexus
https://hostname.com:443/nexus
https://hostname.com:443/nexus/repository/installers/com/oracle/weblogic/12.2.1.3.0/p30965714_122130_Generic.zip
https://${nexus.hostname}:443/nexus/repository/installers/com/oracle/java/jdk/8u251/jdk-8u251-linux-x64.rpm
https://hostname.com:443/nexus/repository/installers/com/oracle/tuxedo/12.1.3.0.0/p30596495_121300_Linux-x86-64.zip
https://hostname.com:443/nexus/repository/installers/com/amazon/java/8.0.252/java-1.8.0-amazon-corretto-devel-1.8.0_252.b09-1.x86_64.rpm
443
https
https://${nexus.hostname}:443/nexus/repository/installers/com/oracle/weblogic/12.2.1.3.0/p30965714_122130_Generic.zip

有时不完全内插/展开字符串的原因是哈希表自然是无序的。引入while循环后,在解析所有内插/展开字符串之前,将不会返回结果。

官方产出如下:

代码语言:javascript
复制
hostname.com
https://hostname.com:443/nexus
/nexus
https://hostname.com:443/nexus
https://hostname.com:443/nexus
https://hostname.com:443/nexus/repository/installers/com/oracle/weblogic/12.2.1.3.0/p30965714_122130_Generic.zip
https://hostname.com:443/nexus/repository/installers/com/oracle/java/jdk/8u251/jdk-8u251-linux-x64.rpm
https://hostname.com:443/nexus/repository/installers/com/oracle/tuxedo/12.1.3.0.0/p30596495_121300_Linux-x86-64.zip
https://hostname.com:443/nexus/repository/installers/com/amazon/java/8.0.252/java-1.8.0-amazon-corretto-devel-1.8.0_252.b09-1.x86_64.rpm
443
https
https://hostname.com:443/nexus/repository/installers/com/oracle/weblogic/12.2.1.3.0/p30965714_122130_Generic.zip
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/63091931

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档