如何将 Get-WMIObject 查询中的数据解析为字符串? [英] How do I parse data from a Get-WMIObject query into a string?

查看:44
本文介绍了如何将 Get-WMIObject 查询中的数据解析为字符串?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有以下代码行...

get-wmiobject -class win32_computersystem | select-object username

它返回(用占位符编辑)...

It returns (redacted with placeholders)...

@{username=DOMAIN\jsmith}

需要做些什么来移除填充并为我提供 DOMAIN\jsmith 的普通"读数?

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

对于奖励积分,我如何将该值解析为 jsmith?

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

推荐答案

你需要展开属性来获取username的值,而不是一个带有username.试试

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]

根据您的操作系统,您可能需要在末尾使用 [1] 而不是 [2].在 Windows 8 中,您需要 2,而在 Windows 7(我认为和更早的版本)中,您需要 1.

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.

这篇关于如何将 Get-WMIObject 查询中的数据解析为字符串?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆