如果循环在powershell函数中不起作用[关闭]
Posted
技术标签:
【中文标题】如果循环在powershell函数中不起作用[关闭]【英文标题】:If loop not working inside powershell function [closed] 【发布时间】:2021-12-15 20:47:57 【问题描述】:Function RemoveDataFromFile ([string] $FileType,[string] $RecordToRemove)
$stupidShit=$RecordToRemove
Write-Host $stupidShit
$RemovedLines = @()
Write-Host "Entering Remove File Proc"
$FolderName="C:\Temp\GTR"
$typeToCheck = switch ($FileType)
"PDT" "pdtcode" ; break
"DET" "detnumber"; break
"ADR" "detnumber"; break
"POS" "detnumber"; break
"PYD" "detnumber"; break
"SMN" "detnumber"; break
"REL" "pdtcode"; break
"DED" "detnumber"; break
"ALW" "detnumber"; break
"TER" "detnumber"; break
"RTN" "rtnparam01"; break
default "Something else happened"; break
$FileList = Get-ChildItem -Path $FolderName -Filter "*$FileType*"
#write-host $FileType "_" $FileList "++" $recCheck
foreach($singleFiles in $FileList)
Write-Host "Checking -" $singleFiles.Name "++ " $recCheck
$data = foreach($line in Get-Content $singleFiles.FullName)
Write-Host "checiking"
$recCheck="$typeToCheck=""$RecordToRemove"""
Write-Host "Record Trying to check " $RecordToRemove
Write-Host "Entering COntent to Check - " $recCheck
Write-Host $line
$what=($line -match $recCheck )
Write-Host "out before loop" $what
if( $what )
Write-Host "Match Found"
$object = New-Object -TypeName PSObject
$object | Add-Member -Name 'Type' -MemberType NoteProperty -Value $FileType
$object | Add-Member -Name 'FileName' -MemberType NoteProperty -Value $singleFiles.Name
$object | Add-Member -Name 'Info' -MemberType NoteProperty -Value $line
$RemovedLines+=$object
else
$line
# $data | Set-Content $singleFiles.FullName
return $RemovedLines
# call
$Det_RemovedLines=RemoveDataFromFile "DET" $DetRecord.EmployeeID.Trim()
($line -match $recCheck ) is false - but is correct!!!
($line -match $recCheck ) 为假 但我在一行中检查 detnumber="303353y" cbr="detadd",detnumber="303355y",dettitle="Mrs",detsurname="Awesome123",detdatejnd= 所以条件应该返回 true 但不是。 任何帮助将不胜感激。
【问题讨论】:
截图清楚地显示了为什么不匹配——$RecordToRemove
的值是303353y
,但是$line
的值包含303355y
——倒数第二个字符是5
,而不是 3
【参考方案1】:
~我没有足够的声誉,无法发表评论
我的 PowerShell 有点生锈了,会不会是 $line 或 $recCheck 有隐藏的小数?
另外,它们是相同的数据类型吗?也许尝试将它们都转换为字符串,看看会发生什么?
如果它返回为假,那么其中一定有某种东西使它们!=
编辑: 忘记这一点,它们不一样: 查看提供的屏幕截图 - 似乎 $rcCheck == "303353y" $line == "303355y"
【讨论】:
天哪!我不敢相信我错过了——真是个愚蠢的错误。谢谢@anton。 别担心,最简单的错误有时最难发现以上是关于如果循环在powershell函数中不起作用[关闭]的主要内容,如果未能解决你的问题,请参考以下文章
字符串在我的函数中不起作用作为参数powershell [重复]