简体   繁体   English

使用部分名称查找文件然后字符串替换 powershell

[英]Find file using partial name then string replace powershell

This feels like it should be straightforward, however, I am coming a bit undone.这感觉应该是直截了当的,但是,我有点未完成。 I want to replace text in a file so that people's VPN configs point to a new address.我想替换文件中的文本,以便人们的 VPN 配置指向新地址。 As each config file is the user's name I first go to the uniform VPN path in windows, then find the VPN config file.由于每个config文件都是用户名我先go到windows中的统一VPN路径,然后找到VPN config文件。 From here it should just be a case of getting that content and then replacing it.从这里开始,应该只是获取该内容然后替换它的情况。 So my thinking was to get a path variable which will go to the OpenVPN config folder.所以我的想法是获取一个路径变量,它将 go 指向 OpenVPN 配置文件夹。 From there find where the.ovpn file is kept and get the name of that file.从那里找到 .ovpn 文件的保存位置并获取该文件的名称。 String replace the text in that file and overwrite said file.字符串替换该文件中的文本并覆盖该文件。 I have managed to successfully do this with manually entering my own file / config paths, I just want to automate it for the staff we have otherwise that's a lot of manual edits I need to do.我已经通过手动输入我自己的文件/配置路径成功地做到了这一点,我只想为我们的员工自动化它,否则我需要做很多手动编辑。

I'm aware that this will also need to be ran as admin.我知道这也需要以管理员身份运行。

This is what I have currently got.这就是我目前得到的。

$path="C:\Program Files\OpenVPN\config\"
$ConfigFile = Get-ChildItem -Path $path -Recurse -Filter "*.ovpn" | select name
(Get-Content $ConfigFile) -replace 'x.x.x.x', 'y.y.y.y' | Set-Content $ConfigFile

select name gives you an object with a single name property that contains only the file name without folder path. select name为您提供一个object ,它具有一个name属性,该属性仅包含没有文件夹路径的文件 Use select -expand fullname instead to get the value of the full file path.使用select -expand fullname来获取完整文件路径的

Also there might be multiple.ovpn files, so you have to write a loop to process them all.也可能有多个 .ovpn 文件,所以你必须写一个循环来处理它们。 In this case it will be easier to use property access using .在这种情况下,使用属性访问会更容易. operator instead of select -expand though.运算符而不是select -expand虽然扩展。 ;-) ;-)

Get-ChildItem -Path $path -Recurse -File -Filter '*.ovpn' | ForEach-Object {
    (Get-Content $_.Fullname) -replace 'x\.x\.x\.x', 'y.y.y.y' | Set-Content $_.Fullname
}
  • As we expect Get-ChildItem to only return files, I've added the -File switch.由于我们希望Get-ChildItem仅返回文件,因此我添加了-File开关。 Although it may look like the filter *.ovpn is enough, there could be folders named like *.ovpn (admittedly unlikely, but better safe than sorry).虽然看起来过滤器*.ovpn就足够了,但可能会有名为*.ovpn的文件夹(诚然不太可能,但安全总比抱歉好)。
  • As Theo noted, -replace uses RegEx and the dot has special meaning in a RegEx pattern.正如Theo所指出的, -replace使用RegEx并且点在 RegEx 模式中具有特殊含义。 To specify a dot literally , it has to be backslash-escaped.要从字面上指定一个点,它必须被反斜杠转义。 This isn't necessary in the replacement pattern (it does have other special characters like $ , which isn't relevant here).这在替换模式中不是必需的(它确实有其他特殊字符,如$ ,此处不相关)。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM