Store username of system [duplicate]

天大地大妈咪最大 提交于 2020-02-07 04:14:06

问题


Currently I use this to get the username of the remote system:

Get-WmiObject win32_computersystem -Computer $tag1 | Format-Table -Property @{Name="Username";Expression={$_.username}} -Autosize;

This will output:

Username
--------
GHS_NTDOMAIN\AJSTEPANIK

I want to store the AJSTEPANIK part to a variable so I can use it in another part of the script, but I am unsure how to trim it or if there is another command to get just the name.


回答1:


You'll want to split the string into two parts (the domain and the username), using the backslash as a delimiter. That will split the string into an array that looks like this:

array[0] = "GHS_NTDOMAIN"

array[1] = "AJSTEPANIK"

We'll be taking the username portion of the array, and storing it in a variable, like so:

$userName = (Get-WmiObject win32_computersystem -Computer $tag1).UserName.Split("\")[1]


来源:https://stackoverflow.com/questions/20749920/store-username-of-system

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