Author Posts

April 28, 2016 at 4:54 pm

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
			}

April 28, 2016 at 7:22 pm

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}

April 28, 2016 at 8:28 pm

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
			}