powershell子字符串从Select-string变量中提取数字,但获取错误
因此,我运行了第一组命令,一切正常,它返回“ 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>
包含它的全局变量有所不同吗?那就是错误所暗示的。还是变量中的空白? 我最终试图实现的是在线路结束时获得该订单号。
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
输出
matchInfo
的实例,此实例没有.indexof(..)
方法也是如此.lastIndexof(..)
和.substring(..)
:这是您从基本上告诉您的错误消息。
您在控制台中看到的作为
select-string
的输出实际上是对象的字符串表示, 在Windows PowerShell和。在powerShell core中强调string(string)
。如果要引用匹配图案的行,则可以参考
顺便说一句,在大多数情况下,您可以使用
$ script:
scoped变量,而不是$ $ global:
scoped ocped。参见 关于_scopes 以获取更多信息。Select-String
outputs an instance ofMatchInfo
, this instance doesn't have a.IndexOf(..)
method same goes for.LastIndexOf(..)
and.SubString(..)
: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:As aside, in most cases, you can use a
$script:
scoped variable instead of a$global:
scoped one. See about_Scopes for more info.