powershell子字符串从Select-string变量中提取数字,但获取错误

发布于 2025-02-05 21:58:11 字数 999 浏览 1 评论 0 原文

因此,我运行了第一组命令,一切正常,它返回“ com”。

$subdomain = "USA.website.com"
$firstref = $subdomain.IndexOf(".")
$lastref = $subdomain.LastIndexOf(".")
$subdomain.Substring($lastref+1)

如果我运行此操作,

$firstref = $global:Order.IndexOf(".")
$lastref = $global:Order.LastIndexOf("#")
$global:Order.Substring($lastref+1)

我会得到此错误

Method invocation failed because [Microsoft.PowerShell.Commands.MatchInfo] does not contain a method named 'IndexOf'.
At line:1 char:1
+ $firstref = $global:Order.IndexOf(".")
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
    + CategoryInfo          : InvalidOperation: (:) [], RuntimeException
    + FullyQualifiedErrorId : MethodNotFound

$ global:订单来自一个选择的串线,其中

PS C:\Windows\system32> $global:Order

C:\temp1\A1\{A114E31A-7A77-4F17-BD6E-74B8E85C9739}.eml:17:New Order: #1820


PS C:\Windows\system32>

包含它的全局变量有所不同吗?那就是错误所暗示的。还是变量中的空白? 我最终试图实现的是在线路结束时获得该订单号。

So I run this first set of commands and everything works fine, it returns "com".

$subdomain = "USA.website.com"
$firstref = $subdomain.IndexOf(".")
$lastref = $subdomain.LastIndexOf(".")
$subdomain.Substring($lastref+1)

If I run this

$firstref = $global:Order.IndexOf(".")
$lastref = $global:Order.LastIndexOf("#")
$global:Order.Substring($lastref+1)

I get this error

Method invocation failed because [Microsoft.PowerShell.Commands.MatchInfo] does not contain a method named 'IndexOf'.
At line:1 char:1
+ $firstref = $global:Order.IndexOf(".")
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
    + CategoryInfo          : InvalidOperation: (:) [], RuntimeException
    + FullyQualifiedErrorId : MethodNotFound

$global:Order comes from a select-string that contains this

PS C:\Windows\system32> $global:Order

C:\temp1\A1\{A114E31A-7A77-4F17-BD6E-74B8E85C9739}.eml:17:New Order: #1820


PS C:\Windows\system32>

Is there something different about a global variable? Thats what the error would suggest. Or is it the white space in the variable?
What I'm ultimately trying to achieve is getting that order number at the end of the line.

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

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

发布评论

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

评论(1

情栀口红 2025-02-12 21:58:11

输出 matchInfo 的实例,此实例没有 .indexof(..)方法也是如此.lastIndexof(..) .substring(..)

   TypeName: Microsoft.PowerShell.Commands.MatchInfo

Name               MemberType Definition
----               ---------- ----------
Equals             Method     bool Equals(System.Object obj)
GetHashCode        Method     int GetHashCode()
GetType            Method     type GetType()
RelativePath       Method     string RelativePath(string directory)
ToEmphasizedString Method     string ToEmphasizedString(string directory)
ToString           Method     string ToString(), string ToString(string directory)

这是您从基本上告诉您的错误消息。

您在控制台中看到的作为 select-string 的输出实际上是对象的字符串表示 在Windows PowerShell和 。在powerShell core中强调string(string)

如果要引用匹配图案的,则可以参考

$Order.Line.IndexOf(".")
$Order.Line.LastIndexOf.IndexOf("#")
$Order.Line.Substring($lastref + 1)

顺便说一句,在大多数情况下,您可以使用 $ script: scoped变量,而不是 $ $ global: scoped ocped。参见 关于_scopes 以获取更多信息。

Select-String outputs an instance of MatchInfo, this instance doesn't have a .IndexOf(..) method same goes for .LastIndexOf(..) and .SubString(..):

   TypeName: Microsoft.PowerShell.Commands.MatchInfo

Name               MemberType Definition
----               ---------- ----------
Equals             Method     bool Equals(System.Object obj)
GetHashCode        Method     int GetHashCode()
GetType            Method     type GetType()
RelativePath       Method     string RelativePath(string directory)
ToEmphasizedString Method     string ToEmphasizedString(string directory)
ToString           Method     string ToString(), string ToString(string directory)

This is what the error message you were getting is telling you basically.

What you see in the console as output from Select-String is actually the string representation of the object, .ToString() in Windows PowerShell and .ToEmphasizedString(String) in PowerShell Core.

If you want to reference the line where the pattern was matched you can refer to the .Line property from this instance:

$Order.Line.IndexOf(".")
$Order.Line.LastIndexOf.IndexOf("#")
$Order.Line.Substring($lastref + 1)

As aside, in most cases, you can use a $script: scoped variable instead of a $global: scoped one. See about_Scopes for more info.

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