问题
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