【问题标题】:Powershell URL replacePowershell URL 替换
【发布时间】:2012-12-20 01:41:55
【问题描述】:

尝试使用下面的代码更新一行。

 $dburl = 70.186.192.52
 $ptdb  = 3388

 $Se = "C:\File\location\edit.me"
(Get-Content $Se) |
   ForEach-Object { $_ -replace ("http://127.0.0.1:8190/storage/server"), 'http://$dburl:$ptdb/storage/server'  } | Set-Content $Se

输出是:

http://$dburl:$ptdb/storage/server or http://\70.186.192.52\:3388/storage/server

我已经尝试转义 // 和 : 但没有运气解决这个问题。任何人都有更好的方法来做到这一点。我浏览了该站点,但我发现的所有内容都没有解决这种直接情况。我这样说是为了不会因为不研究代码而受到任何负面评价。

powershell replace special characters

这个给了我我得到的东西。而不是被替换的变量,它们只是完全按照输入的方式打印出来。我试过双引号,它打印端口而不是地址。如果我放了任何转义 / 或 \ 它只会在前面打印出来。

谢谢!

【问题讨论】:

    标签: powershell replace


    【解决方案1】:

    我很惊讶这完全有效。尝试在替换字符串周围使用双引号,例如:

    $dburl = '70.186.192.52'
    $ptdb  = '3388'
    ... -replace ("http://127.0.0.1:8190/storage/server"), "http://${dburl}:$ptdb/storage/server"
    

    另外,引用你的两个变量值。

    【讨论】:

    • 谢谢,我永远不会尝试的一件事。
    猜你喜欢
    • 2018-10-12
    • 2012-03-10
    • 2021-06-14
    • 2020-07-17
    • 2023-03-20
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多