【发布时间】:2018-05-23 21:32:48
【问题描述】:
我正在尝试获取和修改 .ini 文件的内容,但是我需要确保我要查找的文本前面没有分号,这将指定该行“已注释掉” ”。
.ini文件包含一个参数块,没有用到的块被注释掉,如下。
; use the following 2 settings for Sierra external GPS
;GPSType=Sierra
;Interface=PPP
; use the following 3 settings for internal NMEA GPS
GPSType=NMEA
Interface=Serial
Com=4
我已经在我的管道中尝试了一个 If 语句,但是这是不允许的。
$EOF = {
Write-Host "Ending Program"
break
}
Write-Host "test Application Internal/External Modem Selector" -BackgroundColor Green -ForegroundColor Black
Write-Host
Write-Host "*** Please ensure test is not running ***" -ForegroundColor Yellow
Write-Host
if (!(Test-Path "C:\Program Files (x86)\Rogers\test\test.ini"))
{
Write-Host "Warning - test.INI not present" -BackgroundColor Black -ForegroundColor Red |
Return(0)
}
Write-Host
Write-Host "1. Convert for Internal modem (COM4)"
write-Host "4. Convert for External modem"
Write-Host "Q. Exit"
Write-Host
$menuresponse = Read-Host 'Choose the option >'
if ($menuresponse -eq "q") {
&$EOF
}
elseif ($menuresponse -eq "1")
{
(Get-Content "C:\Program Files (x86)\Rogers\test\test.ini") |
If (($_).notcontains(';Com'))
{
{
ForEach-Object {$_ -replace 'GPSType=Sierra' , ';GPSType=Sierra' } |
ForEach-Object {$_ -replace 'Interface=PPP' , ';Interface=PPP' } |
ForEach-Object {$_ -replace ';GPSType=NMEA' , 'GPSType=NMEA' } |
ForEach-Object {$_ -replace ';Interface=Serial' , 'Interface=Serial' } |
ForEach-Object {$_ -replace ';Com=.*' , 'Com=4' } |
Set-Content "C:\Program Files (x86)\Rogers\test\test.ini"
}
else
{
ForEach-Object {$_ -replace 'Com=.*' , 'Com=4'
Set-Content "C:\Program Files (x86)\Rogers\test\test.ini"
}
}
}}
elseif ($menuresponse -eq "4")
{
(Get-Content "C:\Program Files (x86)\Rogers\test\test.ini") |
If (($_).notcontains(';Com'))
{
{
ForEach-Object {$_ -replace ';GPSType=Sierra' , 'GPSType=Sierra' } |
ForEach-Object {$_ -replace ';Interface=PPP' , 'Interface=PPP' } |
ForEach-Object {$_ -replace 'GPSType=NMEA' , ';GPSType=NMEA' } |
ForEach-Object {$_ -replace 'Interface=Serial' , ';Interface=Serial' } |
ForEach-Object {$_ -replace 'Com=.*' , ';Com=6' } |
Set-Content "C:\Program Files (x86)\Rogers\test\test.ini"
}
else
{
Write-Host "Modem already set to External"
&$EOF
}
}
}
else
{ write-host "Invalid Choice"
}
Break
有没有办法通过在指令传递之前识别它前面是否有分号来确保我不会在 .ini 文件中“双重注释”一个参数?
【问题讨论】:
-
尝试使用带有
^锚点的正则表达式作为replace的第一个参数。
标签: powershell