选择字符串以仅显示返回行的特定部分
我试图在 Powershell 中获取管道选择字符串命令,以仅返回我正在搜索的内容,并在最后添加一些额外的信息。这是我到目前为止所得到的:
PS H:\> gwmi win32_LoggedOnUser -computer server | select-string -pattern "domain" | select-string -pattern "Admin" -NotMatch
\\server\root\cimv2:Win32_LoggedOnUser.Antecedent="\\\\.\\root\\cimv2:Win32_Account.Domain=\"domain\",Name=\"user1\"",Dependent="\\\\.\\root\\cimv2:Win32_LogonSession.LogonId=\"44946387\""
\\server\root\cimv2:Win32_LoggedOnUser.Antecedent="\\\\.\\root\\cimv2:Win32_Account.Domain=\"domain\",Name=\"user2\"",Dependent="\\\\.\\root\\cimv2:Win32_LogonSession.LogonId=\"41485153\""
\\server\root\cimv2:Win32_LoggedOnUser.Antecedent="\\\\.\\root\\cimv2:Win32_Account.Domain=\"domain\",Name=\"user3\"",Dependent="\\\\.\\root\\cimv2:Win32_LogonSession.LogonId=\"46401036\""
\\server\root\cimv2:Win32_LoggedOnUser.Antecedent="\\\\.\\root\\cimv2:Win32_Account.Domain=\"domain\",Name=\"user4\"",Dependent="\\\\.\\root\\cimv2:Win32_LogonSession.LogonId=\"40161073\""
\\server\root\cimv2:Win32_LoggedOnUser.Antecedent="\\\\.\\root\\cimv2:Win32_Account.Domain=\"domain\",Name=\"user5\"",Dependent="\\\\.\\root\\cimv2:Win32_LogonSession.LogonId=\"46557830\""
所以我已经得到了我正在寻找的东西,但我想缩短命令实际显示的内容。最好我只想看到:
Domain\user1
Domain\user2
但我不知道该怎么做。我最接近的是通过管道将另一个命令“选择匹配”放在末尾。我读到正则表达式可能是我的麻烦的答案。
预先感谢您的任何帮助。
I am trying to get piped select-string commands in Powershell to return only what I'm searching for, plus some extra information at the end. Here is what I have so far:
PS H:\> gwmi win32_LoggedOnUser -computer server | select-string -pattern "domain" | select-string -pattern "Admin" -NotMatch
\\server\root\cimv2:Win32_LoggedOnUser.Antecedent="\\\\.\\root\\cimv2:Win32_Account.Domain=\"domain\",Name=\"user1\"",Dependent="\\\\.\\root\\cimv2:Win32_LogonSession.LogonId=\"44946387\""
\\server\root\cimv2:Win32_LoggedOnUser.Antecedent="\\\\.\\root\\cimv2:Win32_Account.Domain=\"domain\",Name=\"user2\"",Dependent="\\\\.\\root\\cimv2:Win32_LogonSession.LogonId=\"41485153\""
\\server\root\cimv2:Win32_LoggedOnUser.Antecedent="\\\\.\\root\\cimv2:Win32_Account.Domain=\"domain\",Name=\"user3\"",Dependent="\\\\.\\root\\cimv2:Win32_LogonSession.LogonId=\"46401036\""
\\server\root\cimv2:Win32_LoggedOnUser.Antecedent="\\\\.\\root\\cimv2:Win32_Account.Domain=\"domain\",Name=\"user4\"",Dependent="\\\\.\\root\\cimv2:Win32_LogonSession.LogonId=\"40161073\""
\\server\root\cimv2:Win32_LoggedOnUser.Antecedent="\\\\.\\root\\cimv2:Win32_Account.Domain=\"domain\",Name=\"user5\"",Dependent="\\\\.\\root\\cimv2:Win32_LogonSession.LogonId=\"46557830\""
So I've got what I'm looking for but I want to shorten up what the command actually shows me. Preferably I would like to see only:
Domain\user1
Domain\user2
But I do not know how to do this. The closest I have come is by piping another command 'Select Matches' onto the end. I have read that regex may be the answer to my troubles.
Thanks in advance for any help.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
请使用如下所示的内容:
避免使用太多
select-string
- 这就像 bash 与所有 grep、sed、awk、cut voodoo 一样,我相信 Powershell 会避免使用高保真对象。尝试尽可能多地使用对象及其属性。是的,并不总是可以使用它们,我们求助于 select-string 或其他字符串操作,但在最后可能的步骤之前尽量避免使用它们。在我看来,使 Powershell 编程整洁、简单且无错误。Please use something like below:
Avoid using so many
select-string
- that is like bash with all the grep, sed, awk, cut voodoo which I believe Powershell avoids with high fidelity objects. Try to use the Objects and their properties as much as possible. Yes it is not always possible to use them and we resort toselect-string
or other string manipulations, but try to avoid them till the last possible step. Makes Powershell programming neat, simple and bug-free IMO.看看这是否给你你想要的:
See if this gives you what you want: