How do I parse data from a Get-WMIObject query into a string?

风格不统一 提交于 2020-01-03 18:37:11

问题


I have the following line of code...

get-wmiobject -class win32_computersystem | select-object username

It returns (redacted with placeholders)...

@{username=DOMAIN\jsmith}

What needs to be done to remove the padding and give me a "plain" readout of DOMAIN\jsmith?

For bonus points, how do I parse that value into just jsmith?


回答1:


You need to expand the property to get the value of username instead of a custom object with the property username. Try

get-wmiobject -class win32_computersystem | select-object -expand username

To get the username only, try:

(get-wmiobject -class win32_computersystem | select-object -expand username).Split("\")[2]

You may need to use [1] instead of [2] at the end depending on your OS. In Windows 8, you need 2, while in Windows 7(and older I think), you need 1.




回答2:


try this

Get-WmiObject -Class Win32_UserAccount | where -property name -eq jsmith | select Name


来源:https://stackoverflow.com/questions/15511398/how-do-i-parse-data-from-a-get-wmiobject-query-into-a-string

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!