我有一个INI文件。我想将字符串与INI文件中的节点匹配。 我想循环检查字符串。 我尝试了这个,但是我发现了一个问题,虽然它不匹配,但是会循环,但是当我用要匹配的字符串修改INI文件匹配时,它仍然会循环并且无法为结果返回true。 这是INI文件的样子
[State]
downloadstage=123
function CPTGM {
Write-Host "Capturing"
}
function Check_Stage {
$INI_Path = "D:\FILE.ini"
$INI_File = @{}
Get-Content $INI_Path | ForEach-Object {
$_.Trim()
} | Where-Object {
$_ -notmatch '^(;|$)'
} | ForEach-Object {
if ($_ -match '^\[.*\]$') {
$section = $_ -replace '\[|\]'
$INI_File[$section] = @{}
} else {
$key, $value = $_ -split '\s*=\s*', 2
$INI_File[$section][$key] = $value
}
}
$String = "123"
$n = 0
while (-not ($INI_File["State"]["downloadstage"] -eq "$String")) {
Start-Sleep -s 1
$n++
Write-Host "Attempt no $n"
}
Write-Host ">>String found after $n attempts"
Write-Output "Continue to Capture"
CPTGM
}
Check_Stage
答案 0 :(得分:1)
问题是您只读取一次* .ini文件,并在while
循环的每次迭代中测试相同的固定内容。如果第一次出现搜索字符串,它将立即退出while循环,但是如果第一次没有出现,它将不再读取该文件以查看是否已更改。
您需要在while循环内移动“读取ini文件”代码,以便每次刷新$INI_File
并获取外部程序所做的任何更改。
此外,您可以使用devblogs.microsoft.com的此功能来解析ini文件,然后查询结构化结果,而不是自己抓取ini文件。
https://devblogs.microsoft.com/scripting/use-powershell-to-work-with-any-ini-file/
function Get-IniContent ($filePath)
{
$ini = @{}
switch -regex -file $FilePath
{
“^\[(.+)\]” # Section
{
$section = $matches[1]
$ini[$section] = @{}
$CommentCount = 0
}
“^(;.*)$” # Comment
{
$value = $matches[1]
$CommentCount = $CommentCount + 1
$name = “Comment” + $CommentCount
$ini[$section][$name] = $value
}
“(.+?)\s*=(.*)” # Key
{
$name,$value = $matches[1..2]
$ini[$section][$name] = $value
}
}
return $ini
}
您的Check_Stage
随后变为:
function Check_Stage {
$INI_Path = "D:\FILE.ini"
$INI_File = Get-IniContent $INI_Path # use Get-IniContent rather than your own version
$String = "123"
$n = 0
while (-not ($INI_File.State.downloadstage -eq $String)) {
Start-Sleep -s 1
$n++
Write-Host "Attempt no $n"
$INI_File = Get-IniContent $INI_Path # read the ini file again to pick up any changes
}
Write-Host ">>String found after $n attempts"
Write-Output "Continue to Capture"
CPTGM
}