使用PowerShell在字符串中找到字符?

发布于 2025-01-20 10:39:04 字数 1719 浏览 3 评论 0原文

我知道我可以使用包含找到它,但它不起作用。

全文:
我必须从sql db中获取partno,ver,ver,并检查它们是否出现在文本文件的第一行中。我获得了文件的第一行,然后将其存储在$ eiacontent中。 partno与mafn相关联,如$ partno = select partno,其中mafn = xxx。大多数时候mafn返回一个partno。但是在某些情况下,对于一个MAFN,可能会有多个partno。因此,查询返回多个partno(partno_1,partno_2,partno_3和partno_4),但其中只有一个在文本文件中。

问题在于这些参与中的每一个。被视为Powershell中的单个角色。 $ partno.length是4。我可以看到文件中提到了其中一个。另外,包含,如果有一个partno。我使用喜欢,如中的if($ eiacontent -like -like“*$ partno*”)匹配partno,并且在有多个partno时起作用,但是它不起作用。

$ partno的数据类型是字符串,因此$ eiacontent。 partno的数据类型。在SQL中是Varchar(50)整理Colate sql_latin1_general_cp1_ci_as 我使用的是PowerShell Core 7.2和SQL 2005

代码:

    $EiaContent = (Get-Content $aidLibPathFolder\$folderName\$fileName  -TotalCount 1)
    Write-host $EiaContent
    #Sql query to get the Part Number 
    $partNoQuery = "SELECT PartNo FROM [NML_Sidney].[dbo].[vMADL_EngParts] Where MAFN = $firstPartTrimmed"
    $partNoSql = Invoke-Sqlcmd -ServerInstance $server -Database $database -Query $partNoQuery
    #Eliminate trailing spaces
    $partNo = $partNoSql.PartNo.Trim()
    If ($EiaContent.Contains("*$partNo*")) {
       Write-Host "Part Matches"
    }
    Else {
       #Send an email stating the PartNo discrepancy 
    }

谢谢您尝试提供帮助的人。

编辑 截屏 [1]: https://i.sstatic.net/hiqjb.png

A1023 A1023MD C0400 C0400MD 是变量$ partnoO40033(C0400 Rev N Ver 004,37 dia 4.5 diam 4.5制动器op3)的输出变量$ eiacontent

I know I could use Contains to find it but it doesn't work.

Full Story:
I have to get the PartNo, Ver, Rev from SQl db and check if they occur in the first line of the text file. I get the first line of the file and store it in $EiaContent.
The PartNo is associated with MAFN as in $partNo=Select PartNo Where MAFN=xxx. Most of the time MAFN returns one PartNo. But in some cases for one MAFN there could be multiple PartNo. So the query returns multiple PartNo(PartNo_1,PartNo_2,PartNo_3,and PartNo_4) but only one of these will be in the text file.

The issue is that each of these PartNo. is treated as a single character in PowerShell. $partNo.Length is 4. Therefore, my check If ($EiaContent.Contains("*$partNo*")) fails and it shouldn't in this case because I can see that one of the PartNo is mentioned in the file. Also, Contains wouldn't work if there was one PartNo. I use like as in If ($EiaContent -like "*$partNo*") to match the PartNo and it worked but it doesn't work when there are multiple PartNo.

Data type of $partNo is string and so is $EiaContent. The data type of PartNo. in SQL is varchar(50) collation is COLLATE SQL_Latin1_General_CP1_CI_AS
I am using PowerShell Core 7.2 and SQL 2005

Code:

    $EiaContent = (Get-Content $aidLibPathFolder\$folderName\$fileName  -TotalCount 1)
    Write-host $EiaContent
    #Sql query to get the Part Number 
    $partNoQuery = "SELECT PartNo FROM [NML_Sidney].[dbo].[vMADL_EngParts] Where MAFN = $firstPartTrimmed"
    $partNoSql = Invoke-Sqlcmd -ServerInstance $server -Database $database -Query $partNoQuery
    #Eliminate trailing spaces
    $partNo = $partNoSql.PartNo.Trim()
    If ($EiaContent.Contains("*$partNo*")) {
       Write-Host "Part Matches"
    }
    Else {
       #Send an email stating the PartNo discrepancy 
    }

Thank you in advance to those who try to help.

EDIT
Screenshot
[1]: https://i.sstatic.net/hIqJB.png

A1023 A1023MD C0400 C0400MD is the output of the variable $partNo and O40033( C0400 REV N VER 004, 37 DIA 4.5 BRAKE DRUM OP3 ) is the output of the variable $EiaContent

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

香橙ぽ 2025-01-27 10:39:04

因此查询返回多个 PartNo(PartNo_1、PartNo_2、PartNo_3 和 PartNo_4),但文本文件中只有其中之一。

A1023 A1023MD C0400 C0400MD 是变量 $partNoO40033( C0400 REV N VER 004, 37 DIA 4.5 BRAKE DRUM OP3 ) 的输出 code> 是变量 $EiaContent

的输出

所以你首先必须分割 $partNo,然后对于 $partNo 的每个子字符串,在 $EiaContent 中搜索它:

If ($partNo -split ' ' | Where-Object { $EiaContent.Contains( $_ ) }) {
   Write-Host "Part Matches"
}

这是大多数人习惯的通用形式。我们可以使用-split的一元形式(因为我们在默认分隔符上拆分)来简化查询,并使用内部数组方法.Where() 速度更快因为不涉及管道开销。

If ((-split $partNo).Where{ $EiaContent.Contains( $_ ) }) {
   Write-Host "Part Matches"
}

正如评论中正确指出的,通配符是不支持 href="https://learn.microsoft.com/en-us/dotnet/api/system.string.contains?view=net-6.0" rel="nofollow noreferrer">.Contains() 字符串方法。

仅 PowerShell -like 运算符支持通配符。以下示例仅用于教育目的,我不会在您的情况下使用它,因为 .Contains() 字符串方法更简单、更快。

If ((-split $partNo).Where{ $EiaContent -like "*$EiaContent*" }) {
   Write-Host "Part Matches"
}

请注意,-contains 在这里合适。一个常见的误解是,当 LHS 操作数是字符串时,-contains 会执行子字符串搜索。事实并非如此!该运算符测试左侧的集合(例如数组)是否包含右侧给出的值。

So the query returns multiple PartNo(PartNo_1,PartNo_2,PartNo_3,and PartNo_4) but only one of these will be in the text file.

A1023 A1023MD C0400 C0400MD is the output of the variable $partNo and O40033( C0400 REV N VER 004, 37 DIA 4.5 BRAKE DRUM OP3 ) is the output of the variable $EiaContent

So you first have to split $partNo and then for each sub string of $partNo, search for it in $EiaContent:

If ($partNo -split ' ' | Where-Object { $EiaContent.Contains( $_ ) }) {
   Write-Host "Part Matches"
}

This is the generic form that most people are used to. We can simplify the query using the unary form of -split (as we split on the default separator) and use the intrinsic array method .Where() which is faster as it does not involve pipeline overhead.

If ((-split $partNo).Where{ $EiaContent.Contains( $_ ) }) {
   Write-Host "Part Matches"
}

As correctly noted in comments, wildcards are not supported by the .Contains() string method.

Wildcards are supported only by the PowerShell -like operator. The following example is just for educational purposes, I wouldn't use it in your case as .Contains() string method is simpler and faster.

If ((-split $partNo).Where{ $EiaContent -like "*$EiaContent*" }) {
   Write-Host "Part Matches"
}

Note that -contains would not be suitable here. A common misconception is that -contains does a substring search, when the LHS operand is a string. It doesn't! The operator tests whether a collection (such as an array) on the LHS contains the value given on the RHS.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文