Split a string on multiple lines.

This topic contains 2 replies, has 1 voice, and was last updated by Profile photo of Gary Smith Gary Smith 5 months ago.

Viewing 3 posts - 1 through 3 (of 3 total)
  • Author
    Posts
  • #38365
    Profile photo of Gary Smith
    Gary Smith
    Participant

    Can someone help me split a string on each new line?
    I am using an input box to provide a way to copy and paste a block of DL's (could contain spaces in the name) to extract info from.
    My issue is that I can not work out how to split that string on each new line, i have tried everything i can find but i think my problem is 2 fold. 1 i don't know the right syntax and 2 I don't know at what point it should be done. This means that I just can't manage to get it right through trial and error.

    $object = Read-MultiLineInputBoxDialog -Message "Please enter distribution list names. It can be multiple lines" -WindowTitle "Get Owner of multiple DL's" -DefaultText "Enter one Distribution Group per line..."
    $Split = $object.Split("`n`r")
    		if ($object -eq $null) { 
    		Write-Host "You clicked Cancel" }
    				
    		else {
    		
    		Foreach ($DL in $Split){
    			$ManagedBy = Get-ADGroup -Identity $DL -Properties ManagedBy 
    			$Name = $ManagedBy.ManagedBy
    			$Full = Get-ADUser $Name -Properties DisplayName, EmailAddress, Title | select DisplayName, EmailAddress
    			$NewObject = New-Object PSObject -Property @{
    				"Name"=$Full.Displayname;
    				"Email"=$Full.EmailAddress;
    				"Distribution Group"=$DL}
    								}
    		$NewObject
    			}
    
    #38369
    Profile photo of Gary Smith
    Gary Smith
    Participant

    Actually, I think I have this backwards. I think my split is OK but it breaks in the foreach.

    $object = Read-MultiLineInputBoxDialog -Message "Please enter distribution list names. It can be multiple lines" -WindowTitle "Get Owner of multiple DL's" -DefaultText "Enter one Distribution Group per line..."
    $Split = $object.Split("`r")
    
    Foreach ($line in $Split){ Get-ADGroup -Identity $line -Properties ManagedBy}
    

    I think the second line is coming in as a Newline, then the second DL below it.

    Error looks like this:
    Get-ADGroup : Cannot find an object with identity: '
    Dl-Name-Here' under: 'DomainNameHere'.
    At C:\Users\User\AppData\Local\Temp\fc9cc011-247e-4051-b7bf-2ccbe8298e19.ps1:4 char:28
    + ... ($line in $Split){ Get-ADGroup -Identity $line -Properties ManagedBy}

    #38371
    Profile photo of Gary Smith
    Gary Smith
    Participant

    I worked it out anyway.
    Seems to be that when it was splitting it was adding an extra row. I still don't understand why it was doing that but this solution seems to work for my purposes..

    For completeness, the script is below

    $object = Read-MultiLineInputBoxDialog -Message "Please enter distribution list names. It can be multiple lines" -WindowTitle "Get Owner of multiple DL's" -DefaultText "Enter one Distribution Group per line..."
    $split = $object -split "`r`n|`r|`n"
    		if ($object -eq $null) { Write-Host "You clicked Cancel" }
    				
    		else {
    			$collection = @()
    		
    			Foreach ($line in $split){
    			$manager = Get-ADGroup -Identity $line -Properties ManagedBy 
    			$name = $Manager.ManagedBy
    			$userproperties = Get-ADUser $name -Properties DisplayName, EmailAddress, Title | select DisplayName, EmailAddress
    			
    			$newobject = New-Object PSObject -Property @{
    					"Name"	=$userproperties.displayname;
    					"Email"	=$userproperties.emailaddress;
    					"DL"	=$line}
    					
    			$collection += $newobject
    									}
    			$collection
    			}
    
Viewing 3 posts - 1 through 3 (of 3 total)

You must be logged in to reply to this topic.