从 powershell 中提取 NT 用户 ID [英] Pull NT user ID from powershell

查看:39
本文介绍了从 powershell 中提取 NT 用户 ID的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

get-wmiobject -class win32_computersystem -computername c73118 | format-table username

将输出类似于:

username
--------
GHS_NTDOMAIN\amacor

是否可以只输出 amacor 部分?

Is it possible to only output the amacor part only?

推荐答案

首先,我不认为你真的想要 FT.请改用 Select -Expand.这样做我们得到字符串GHS_NTDOMAIN\amacor.一旦你有了它,你可以做 .Split("\") 将其拆分为一个字符串数组,并 [-1] 指定数组中的最后一个字符串.所以它看起来像:

first, you don't really want FT for this I don't think. Use Select -Expand instead. So doing that we get back the string GHS_NTDOMAIN\amacor. Once you have that, you can do .Split("\") to split it into an array of strings, and [-1] to specify the last string in the array. So it would look like:

(get-wmiobject -class win32_computersystem -computername c73118 | Select -ExpandProperty username).Split("\")[-1]

这将导致:

amacor

或者如果你想更详细一点,你可以这样做:

Or if you wanted to be a bit more verbose about it, you can do:

$Data = get-wmiobject -class win32_computersystem -computername c73118
$DomainUser = $Data.Username
$UserName = $DomainUser.Split("\")[-1]

然后 $UserName = "amacor"

Then $UserName = "amacor"

根据 Andy Arismendi 的出色建议进行了更新.

Updated per Andy Arismendi's excellent suggestion.

这篇关于从 powershell 中提取 NT 用户 ID的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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