Split function

This topic contains 0 replies, has 1 voice, and was last updated by Profile photo of Forums Archives Forums Archives 5 years, 3 months ago.

  • Author
    Posts
  • #5393

    by Porthos at 2013-01-02 13:39:06

    Hi Everyone,

    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 names

    Get-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!

    by DonJ at 2013-01-02 13:49:38

    -Split produces an array. Because you're feeding it one line at a time, you're getting several arrays.

    You might try:

    ForEach { ($_ -split(": "))[1] }

    (Sorry, can't abide the % alias)

    by Porthos at 2013-01-02 13:59:12

    DonJ,

    That seems to just return the info from the text file. It doesnt split the information at all

    by DonJ at 2013-01-02 14:11:32

    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,

    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?

    by DonJ at 2013-01-02 17:28:23

    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 doing
    ForEach { ($_ -Split(": "))[1] }
    try
    ForEach { $_.Split(": ")[1] }

    by Porthos at 2013-01-03 06:31:18

    Unfortunately still get the same result.

    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

    by Klaas at 2013-01-03 07:54:19

    Can you post the exact code you're using now, please?

You must be logged in to reply to this topic.