by Porthos at 2013-01-02 13:39:06
Hi Everyone,by DonJ at 2013-01-02 13:49:38
I have a question about using the -split function
I currently have a text file that has the following info among a bunch of other things…ALIAS : Domain1\D-Servers-Local Administrators
ALIAS : Domain1\D-Server Administrators
GROUP : Domain2\Enterprise Admins
GROUP : Domain2\U-Admin
GROUP : Domain2\U-Power Users
GROUP : Domain1\Domain Admins
GROUP : Domain4\Domain Users
GROUP : Domain1\OUAdmins
GROUP : Domain1\SMSAdmins
GROUP : Domain3\Operators
USER : Domain2\User-admin
USER : Domain1\User1
USER : Domain1\User2
So basically I am trying to use the following line to fetch me that list from the text file and pull out just the group,alias,and user namesGet-Content $Path\LocalGroups\Groups.txt | Sort-Object -Unique | Select-String -SimpleMatch "Domain1","Domain2","Domain3","Domain4","Domain5" | %{$-split(": ")}
However I want to just get the names after the colon. When I run that line of code I get something like the following:
ALIAS
Domain1\D-Servers-Local Administrators
ALIAS
Domain1\D-Server Administrators
GROUP
Domain2\Enterprise Admins
etc...
What do I need to just get the second half of the split then write it to a text file?
Thanks!
-Split produces an array. Because you’re feeding it one line at a time, you’re getting several arrays.by Porthos at 2013-01-02 13:59:12
You might try:
ForEach { ($-split(": "))[1] }
(Sorry, can’t abide the % alias)
DonJ,by DonJ at 2013-01-02 14:11:32
That seems to just return the info from the text file. It doesnt split the information at all
Sorry, I don’t have a copy of the shell I can play with. The general idea is that -split always creates an array. Since your string is being split into two parts, it’ll be a two-element array. [0] will be the bit before the : and [1] will be the bit after. That’s what you’ll need to work with.by Porthos at 2013-01-02 17:15:58
Hey DonJ,by DonJ at 2013-01-02 17:28:23
I understand what you are saying however, I am still getting the same results using 0 or 1…here is what I have so far…Get-Content $Path\LocalGroups\Groups.txt | Sort-Object -Unique | Select-String -SimpleMatch "Domain1","Domain2","Domain3","Domain4","Domain5" | ForEach{($_ -split(":"))[1]}
I am fairly new to this so not sure what I may be doing wrong, am I possibly trying to string too many things together? Sorting, selecting and splitting?
Yeah possibly. I’d probably try to write this over several lines instead of in a one liner. Easier to debug.by Jules at 2013-01-02 23:56:04
Instead of doingby Porthos at 2013-01-03 06:31:18ForEach { ($_ -Split(": "))[1] }
tryForEach { $_.Split(": ")[1] }
Unfortunately still get the same result.by Klaas at 2013-01-03 07:54:19
If I change it to 0, it returns the left side of the ":" on one line and the right side on the next line.
ALIAS
Domain1\D-Servers-Local Administrators
ALIAS
Domain1\D-Server Administrators
GROUP
Domain2\Enterprise Admins
GROUP
Domain2\U-Admin
GROUP
Domain2\U-Power Users
GROUP
Domain1\Domain Admins
GROUP
Domain4\Domain Users
GROUP
Domain1\OUAdmins
GROUP
Domain1\SMSAdmins
GROUP
Domain3\Operators
If I change it to 1, it returns the entire line as if its not splitting it at all.
ALIAS : Domain1\D-Servers-Local Administrators
ALIAS : Domain1\D-Server Administrators
GROUP : Domain2\Enterprise Admins
GROUP : Domain2\U-Admin
GROUP : Domain2\U-Power Users
GROUP : Domain1\Domain Admins
GROUP : Domain4\Domain Users
GROUP : Domain1\OUAdmins
GROUP : Domain1\SMSAdmins
GROUP : Domain3\Operators
Can you post the exact code you’re using now, please?