如何在PowerShell中使用运算符“-replace"将文本字符串替换为特殊字符并成功替换 [英] How to use operator '-replace' in PowerShell to replace strings of texts with special characters and replace successfully

查看:79
本文介绍了如何在PowerShell中使用运算符“-replace"将文本字符串替换为特殊字符并成功替换的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个脚本,我基本上是在其中对几个文本字符串进行查找和替换.前几个字符串有效,但是当我处理帐户密钥时,它们不起作用.我该如何解决这个问题?

I have a script where I am basically doing a find and replace on several strings of text. The first couple of strings work, but when I do the account keys, they do not. How can I fix this problem?

脚本如下:

Get-ChildItem "[FILEPATH]" -recurse |
    Foreach-Object {
        $c = ($_ | Get-Content)
        $c = $c -replace 'abt7d9epp4','w2svuzf54f'
        $c = $c -replace 'AccountName=adtestnego','AccountName=zadtestnego'
        $c = $c -replace 'AccountKey=eKkij32jGEIYIEqAR5RjkKgf4OTiMO6SAyF68HsR/Zd/KXoKvSdjlUiiWyVV2+OUFOrVsd7jrzhldJPmfBBpQA==','DdOegAhDmLdsou6Ms6nPtP37bdw6EcXucuT47lf9kfClA6PjGTe3CfN+WVBJNWzqcQpWtZf10tgFhKrnN48lXA=='
        [IO.File]::WriteAllText($_.FullName, ($c -join "`r`n"))
    }

推荐答案

'-replace' 进行正则表达式搜索,最后一个中有特殊字符(如 +)所以你可以像这样使用非正则表达式替换版本:

'-replace' does a regex search and you have special characters in that last one (like +) So you might use the non-regex replace version like this:

<代码> $ C = $ c.replace( 'AccountKey = eKkij32jGEIYIEqAR5RjkKgf4OTiMO6SAyF68HsR/ZD/KXoKvSdjlUiiWyVV2 + OUFOrVsd7jrzhldJPmfBBpQA ==', 'DdOegAhDmLdsou6Ms6nPtP37bdw6EcXucuT47lf9kfClA6PjGTe3CfN + WVBJNWzqcQpWtZf10tgFhKrnN48lXA ==')

这篇关于如何在PowerShell中使用运算符“-replace"将文本字符串替换为特殊字符并成功替换的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆