正如我在標題中提到的,我創建了一個批處理腳本,它會觸發一些 Powershell 命令來替換組態檔中的一行,該.ovpn組態檔基本上是文本檔案,使用所需的行,但它沒有按預期作業。
這是 .ovpn 檔案的當前內容,我正在嘗試修改:
.....
.....
auth-user-pass "C:\\path\\to\\.old-auth"
....
....
檔案的所需內容是這樣的:
.....
.....
auth-user-pass "C:\\path\\up\\to\\.new-auth"
....
....
為此,我撰寫了這個腳本,它應該在沒有任何問題的情況下更改確切的行,但顯然這沒有發生。在確保批處理腳本中的每個變數和字串操作都正常作業之后,我不知道 Powershell 到底出了什么問題:
@echo off && setlocal EnableDelayedExpansion
set "VpnCfgDir=C:\path\up\to\"
set "DestFile=!VpnCfgDir!\.new-auth"
set "VpnAuth=!DestFile!" && set "VpnAuth=!VpnAuth:\=\\!"
PowerShell -ExecutionPolicy AllSigned -NoExit -NoLogo -NonInteractive -NoProfile -WindowStyle Maximized -Command "$line = Get-Content \"!VpnCfgDir!\<vpn_name>.ovpn\" | Select-String 'auth-user-pass' | Select-Object -ExpandProperty Line; $content = Get-Content \"!VpnCfgDir!\<vpn_name>.ovpn\"; $content = $content | ForEach-Object {$_ -replace $line, 'auth-user-pass \"%VpnAuth%\"'} | Set-Content \"!VpnCfgDir!\\<vpn_name>.ovpn\""
任何人都可以幫助修復和實作預期的功能嗎?
uj5u.com熱心網友回復:
我認為這樣的事情可以奏效。在不知道您遇到的確切錯誤或問題的情況下,很難說,但我認為使用拆分對文本檔案進行簡單決議是可行的。
$contentPath = \"!VpnCfgDir!\<vpn_name>.ovpn\"; Set-Content -Path $contentPath -Value ((Get-Content $contentPath | Select-String \"auth-user-pass\").ToString().Split(\".\")[0] \".NewAuthHere\")
更容易閱讀的格式:
$contentPath = \"!VpnCfgDir!\<vpn_name>.ovpn\"
Set-Content -Path $contentPath -Value ((Get-Content $contentPath |
Select-String \"auth-user-pass\").ToString().Split(\".\")[0] \".NewAuthHere\")
我們還可以.new-auth使用變數使您的檔案名動態化
$newAuthFile = \".new-auth\";$contentPath = \"!VpnCfgDir!\<vpn_name>.ovpn\";Set-Content $contentPath -Value ((Get-Content $contentPath | Select-String \"auth-user-pass\").ToString().Split(\".\")[0] $newAuthFile)
更容易閱讀的格式:
$newAuthFile = \".new-auth\"
$contentPath = \"!VpnCfgDir!\<vpn_name>.ovpn\"
Set-Content $contentPath -Value ((Get-Content $contentPath | Select-String \"auth-user-pass\").ToString().Split(\".\")[0] $newAuthFile)
轉載請註明出處,本文鏈接:https://www.uj5u.com/qiye/412084.html
標籤:
